first of all: i use code blocks with gnu compiler, is that bad?! and creating problems?!
so i was just starting to make a program (my teacher says i am too dumb to make anything, so i challenged him to make a program way ahead of my time, as we now study f###ing qbasic!!) but then strange results happened! i tried with like 10 different ways and to no avail... i used for, changed it with while, i used x++, changed it with x += 1, and nothing different, try writing like 10 and 30.... help please!!!!!
#include <iostream>
usingnamespace std;
int main()
{
int N1, N2;
cout << "This program will find prime numbers between two of your specified number!" << endl << "please enter the first number: ";
cin >> N1;
cout << "please enter the second number: ";
cin >> N2;
int x = N1;
while (x < N2)
{
cout << x;
x += 1;`
}
}
Define "strange results". What did you expect? what did you observe?
As written (except for the stray apostrophe), it runs as you can see at http://ideone.com/nQ23fz , and outputs 10111213....2829 (all numbers from 10 to 29 with no other output)
Kinda silly requirement. I imagine they want you to come up with some creative solution, but if they want that then they need to come up with a creative problem. +1 for using the sieve.
i am still trying, and i will never use you cheaty resources :P but i have a problem in my code so far... take a look:
(once again, this is not even 40% i am still trying to test before going on! try inputting 10 and 12 and it will stop working!)
#include <iostream>
usingnamespace std;
int main()
{
int N1, N2;
cout << "This program will find prime numbers between two of your specified number!" << endl << "please enter the first number: ";
cin >> N1;
cout << "please enter the second number: ";
cin >> N2;
N2++;
int x = N1;
for (x; x < N2; x++)
{
for (int y = 0; y < 1000; y++ )
{
int z;
z %= x/y;
if (z == 0)
{
cout << x << endl;
}
}
}
}
You need better variable names. What are x y, z, N1, and N2 supposed to be?
Also, z is uninitialized on line 18 so how can you do z = z % (x/y);?
If you really want to do it the way you are (very slow and ineffective, will probably take several hours for finding about a billion primes instead of a matter of seconds). You probably want something like this:
//lower and upper bounds
intconst lower = 1;
intconst upper = 100;
int lowerOdd = lower;
if(lowerOdd == 2) //it is 2 (the first prime)
{
std::cout << 2 << ' ';
lowerOdd = 3;
}
elseif(lowerOdd > 2 && lowerOdd % 2 == 0) //greater than 2 and even
{
++lowerOdd;
}
for(int i = lowerOdd; i < upper; i += 2) //2 is the only even prime
{
bool prime = i % 2; //if it is odd it will be true
for(int j = 3; j <= std::sqrt(i); j += 2) //only need to check until the sqrt
{
if(i % j == 0)
{
prime = false;
break;
}
}
if(prime)
{
std::cout << i << ' ';
}
}