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!!!!!

 ``1234567891011121314151617181920`` `````` #include using namespace 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;` } } ``````
Last edited on
oh and still, i was just checking if it would work before going on.... this is not even 1%
 just checking if it would work before going on

That is a good approach

 then strange results happened!

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)
Last edited on
maybe my teacher wasn't wrong after all! i forgot to use endl! thank you so much :)
When you actually look for the primes you should use a sieve. The easiest is a sieve of Eratosthenes. Using a `std::vector<bool>` is also suggested. http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
http://www.cplusplus.com/forum/beginner/129775/#msg700669

I knew there is an easy way, but part of the challenge is to not use the internet, i used it once, i wont cheat again!
Using resources is not cheating. Also the a prime sieve is actually really common :P
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.
Last edited on
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!)

 ``12345678910111213141516171819202122232425262728`` ``````#include using namespace 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:
 ``123456789101112131415161718192021222324252627282930`` ``````//lower and upper bounds int const lower = 1; int const upper = 100; int lowerOdd = lower; if(lowerOdd == 2) //it is 2 (the first prime) { std::cout << 2 << ' '; lowerOdd = 3; } else if(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 << ' '; } }``````
Again though, this method is very slow.
Topic archived. No new replies allowed.