I will explain how my code works in detail first.
In my example code, the number is assumed to be prime unless proven otherwise in the following conditional statements. That's why the boolean variable is reset to true at the beginning of the while loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
bool isPrime = true; // Assume that the number is prime
if ( i == 2 ) // i is 2 and 2 is prime
isPrime = true; // You can simply leave this part blank with square brackets
// if you want to since isPrime is already set to true
else if ( i % 2 == 0 ) // i is an even number and at this point, we know that i is larger than 2
isPrime = false; // Any even number larger than 2 cannot be prime, so mark bool as false
else // At this point, i is not 2 and i is not even. i is an odd number
for // so, using for-loop, we need to test and find out if i is prime
.
.
.
|
Now, I will explain the for-loop
(1) Let's say that we have number 3, which is i = 3
|
for ( int a = 3 ; a <= sqrt ( i ) && isPrime ; a += 2 )
|
sqrt of 3 is 1.73... and 1.73 is smaller than a = 3, so the for-loop will never execute when i = 3. So for i = 3, the boolean variable will remain true. It works out since 3 is prime.
The last conditional statement,
will increment the value of x
Of course, the program will implement i as well since there is no condition set for incrementing i
(2) Now, i = 4
4 is even and because of the second conditional statement,
The boolean variable will be set to false.
x will not be incremented. i will be incremented
(3) Now, i = 5
|
for ( int a = 3 ; a <= sqrt ( i ) && isPrime ; a += 2 )
|
Square Root of 5 is 2.23, which is less than a = 3. The for-loop will not execute for i =5 either. And isPrime will remain true
(4) i = 6
We already know what will happen since 6 is even.
(5) i = 7
|
for ( int a = 3 ; a <= sqrt ( i ) && isPrime ; a += 2 )
|
Square Root of 7 is 2.64 and it is still smaller than a = 3, which means again, the for-loop will not execute and isPrime remains true
(6) i = 8
(7) i = 9
9 is an odd number but it's NOT prime.
|
for ( int a = 3 ; a <= sqrt ( i ) && isPrime ; a += 2 )
|
Square Root of 9 is 3. So, the for-loop will execute since 3 <= 3 is true.
1 2
|
if ( i % a == 0 )
isPrime = false;
|
9 % 3 == 0, so isPrime is set to false. The for-loop condition requires that isPrime to be "true" as well as a <= sqrt ( i ), so the program exits the for-loop
(8) i = 10
(9) i = 11
|
for ( int a = 3 ; a <= sqrt ( i ) && isPrime ; a += 2 )
|
Square Root of 11 is 3.31 and isPrime is set to true, so the program will enter the for-loop.
1 2
|
if ( i % a == 0 )
isPrime = false;
|
11 % 3 == 0 is FALSE, so isPrime remains true. a gets incremented to 4
4 <= sqrt ( i ) is false and the for-loop ends. Again, isPrime is still true for 11.
I hope this gives you a clear idea of how the for-loop in my example works
Now, let's take a look at yours.
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 31 32 33 34 35
|
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int N = 0;
int x = 0;
int i = 2;
cout << "Enter integer: " << endl;
cin >> N;
while ( x < N )
{
if ( i == 2 )
x ++;
else if ( i > 2 && i % 2 != 0 )
{
for ( int a = 3 ; a <= sqrt( i ) ; a += 2 )
{
if ( i % a != 0 )
x ++; // Incorrect
else if ( i % a == 0 )
break;
}
}
i ++;
}
cout << "The "<< N << "'th prime number is : " << i - 1 << endl;
}
|
Let's take i = 97 as example.
Square Root of 97 is 9.87
So, in the for-loop, 97 will be divided by the following a values
3, 5, 7, 9
97 % 3 != 0
97 % 5 != 0
97 % 7 != 0
97 % 9 != 0
So in your code, x gets incremented 4 times for 97. That was your mistake.
Bottom Line: With the setup I suggested, the boolean value is probably required. If you can figure out a way to set this up without the boolean, that'd be great.
Once you understand this, try learning Sieve of Erastosthenes
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes