Finding the 10,001st prime number
I am not sure what is wrong with this code, I keep getting the answer as 1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
#include<iostream>
using namespace std;
int main()
{
int counter, number, divider;
number = 1;
counter = 0;
divider = 1;
while(divider < 150000)
{
if(divider > 150000)
{
divider = 1;
number++;
}
if(number % divider != 0)
{
counter++;
}
if(counter == 10001)
{
cout << "The 10001st prime number is: " << number << "\n";
}
divider++;
}
system("pause");
return 0;
}
|
This if never happens, it's impossible to:
1 2 3 4 5 6 7
|
while(divider < 150000)
{
if(divider > 150000)
{
divider = 1;
number++;
}
|
Here:
1 2 3 4
|
if(number % divider != 0)
{
counter++;
}
|
You're dividing 1 by 1, and 1 is not a prime number, I'm not quite sure what you're doing.
Your program is simple, but I believe it's TOO simple to do what you want it to do.
Topic archived. No new replies allowed.