# Thread: prime number generation, match 10001 prime number

1. ## prime number generation, match 10001 prime number

Please help. I've been playing around with this code most of the day, or for a couple days, trying to find a way to insert "10001" after program execution to find the matching answer (this is on project euler) and it's question #7 Problem 7 - Project Euler

Code:
```#include <stdio.h>
#include <stdbool.h>

int main ()
{
int p;
int d;
int input;
int ctr = 1;

bool isPrime;

printf("What prime number do you want to find: ");
scanf("%d", &input);  // ask for users input of prime number to find

for (p = 2; ; ++p) {  // cycle through integers 2 through infinite
isPrime = true;

for(d = 2; d < p; ++d)  // divide p by by -1
if(p % d == 0) // factor found (for example, 2)
isPrime = false;

if(isPrime != false) // isPrime is true (for example, no factors)
// var number total count number of isPrime != false variables (incrememnt ctr for each one)
printf("%d\n", p);
}

printf("\n");

return 0;
}```
Well, how do I access p from the first loop, is my first question, for the printf statement under the
Code:
```if(isPrime != false)
printf("%d\t%d\n", p);```
For example, i was thinking of doing something like this, my coding experience is beginner,

Code:
```if(isPrime != false)
if(p > 1)
++ctr;
if(ctr == input)
printf("%d\n", p);```

or my other option was
Code:
`if(isPrime != false && /* insert something else here */)`

2. Code:
```#include <stdio.h>
#include <stdbool.h>

int main ()
{
int nth_prime;
printf("What prime number do you want to find: ");
scanf("%d", &nth_prime);

int p = 2;
for (int cnt = 0; ; ++p) {

bool isPrime = true;

for(int d = 2; d * d <= p; ++d)
if (p % d == 0) {
isPrime = false;
break;
}

if (isPrime && ++cnt == nth_prime)
break;
}

printf("%d\n", p);
return 0;
}```
Or maybe:

Code:
```#include <stdio.h>
#include <stdbool.h>

int main()
{
int nth_prime;
printf("What prime number do you want to find: ");
scanf("%d", &nth_prime);

int p = 2;
for (int cnt = 0; cnt < nth_prime; ++p) {

bool isPrime = true;

for(int d = 2; d * d <= p; ++d)
if (p % d == 0) {
isPrime = false;
break;
}

if (isPrime)
++cnt;
}

printf("%d\n", p - 1);
return 0;
}```
Or maybe even:

Code:
```#include <stdio.h>#include <stdbool.h>

int main()
{
int nth_prime;
printf("What prime number do you want to find: ");
scanf("%d", &nth_prime);

if (nth_prime <= 1) {
printf("none\n");
return 0;
}

if (nth_prime == 1) {
printf("2\n");
return 0;
}

int p = 3;
for (int cnt = 1; cnt < nth_prime; p += 2) {

bool isPrime = true;

for(int d = 3; d * d <= p; d += 2)
if (p % d == 0) {
isPrime = false;
break;
}

if (isPrime)
++cnt;
}

printf("%d\n", p - 2);
return 0;
}```

3. thanks john.c for the quick response, i am going over your code now and will formulate a lengthier response soon

4. Originally Posted by john.c
Code:
```#include <stdio.h>
#include <stdbool.h>

int main ()
{
int nth_prime;
printf("What prime number do you want to find: ");
scanf("%d", &nth_prime);

int p = 2;
for (int cnt = 0; ; ++p) {

bool isPrime = true;

for(int d = 2; d * d <= p; ++d)
if (p % d == 0) {
isPrime = false;
break;
}

if (isPrime && ++cnt == nth_prime)
break;
}

printf("%d\n", p);
return 0;
}```
Or maybe:

Code:
```#include <stdio.h>
#include <stdbool.h>

int main()
{
int nth_prime;
printf("What prime number do you want to find: ");
scanf("%d", &nth_prime);

int p = 2;
for (int cnt = 0; cnt < nth_prime; ++p) {

bool isPrime = true;

for(int d = 2; d * d <= p; ++d)
if (p % d == 0) {
isPrime = false;
break;
}

if (isPrime)
++cnt;
}

printf("%d\n", p - 1);
return 0;
}```
Or maybe even:

Code:
```#include <stdio.h>#include <stdbool.h>

int main()
{
int nth_prime;
printf("What prime number do you want to find: ");
scanf("%d", &nth_prime);

if (nth_prime <= 1) {
printf("none\n");
return 0;
}

if (nth_prime == 1) {
printf("2\n");
return 0;
}

int p = 3;
for (int cnt = 1; cnt < nth_prime; p += 2) {

bool isPrime = true;

for(int d = 3; d * d <= p; d += 2)
if (p % d == 0) {
isPrime = false;
break;
}

if (isPrime)
++cnt;
}

printf("%d\n", p - 2);
return 0;
}```
Can't get this to output correct prime number, i'm getting program output "2" for "10001" Thanks john.c for the continuous support on the forum, now i see accessing the p variable from the first for loop and the input variable from the scanf() function is possible with your second if statement

Code:
```#include <stdio.h>
#include <stdbool.h>

int main ()
{
int p;
int d;
int input;

bool isPrime;

printf("What prime number do you want to find: ");
scanf("%d", &input);  // ask for users input of prime number to find

for (p = 2; ; ++p) {  // cycle through integers 2 through infinite
isPrime = true;

for(d = 2; d * d <= p; ++d)  // divide p by by -1
if(p % d == 0) // factor found (for example, 2)
isPrime = false;
break;

if (isPrime && ++p == input) // isPrime is true (for example, no factors)
break;
}

printf("%d", p);

return 0;
}```
-

5. Unlike Python, indentation means nothing to C. Statements are grouped together (a "compound statement") with braces.

6. Originally Posted by christop
Unlike Python, indentation means nothing to C. Statements are grouped together (a "compound statement") with braces.

I missed my braces missing error in the source code, hows this look

Code:
```#include <stdio.h>
#include <stdbool.h>

int main ()
{
int p;
int d;
int input;

bool isPrime;

printf("What prime number do you want to find: ");
scanf("%d", &input);  // ask for users input of prime number to find

for (p = 2; ; ++p) {  // cycle through integers 2 through infinite
isPrime = true;

for(d = 2; d * d <= p; ++d)   // divide p by by -1
if(p % d == 0) { // factor found (for example, 2)
isPrime = false;
break;
}

if (isPrime && ++p == input)
break;

}

printf("%d", p);

return 0;
}```

7. Originally Posted by _jamie
I missed my braces missing error in the source code, hows this look

You'll need a count variable as well as 'p' because you don't want to compare the prime number itself ('p') to 'input'. This is because you want to know what the 10001st (for example) prime number is; i.e. line 31 should be if (isPrime && ++cnt == input)

8. Originally Posted by Hodor
You'll need a count variable as well as 'p' because you don't want to compare the prime number itself ('p') to 'input'. This is because you want to know what the 10001st (for example) prime number is; i.e. line 31 should be if (isPrime && ++cnt == input)
that fixed it, thanks Hodor I completely misssed it. I now have the working source code and it found & matched the 10001 prime number

9. This code works
Code:
```#include <stdio.h>
#include <stdbool.h>

int main ()
{
int p = 2;
int d;
int input;
int ctr;

bool isPrime;

printf("What prime number do you want to find: ");
scanf("%d", &input);  // ask for users input of prime number to find

for (ctr = 0; ctr < input; ++p) {  // cycle through integers 2 through infinite
isPrime = true;

for(d = 2; d * d <= p; ++d)   // divide p by by -1
if(p % d == 0) { // factor found (for example, 2)
isPrime = false;
break;
}

if (isPrime && ++ctr == input)
break;

}

printf("%d\n", p);

return 0;
}```

10. Originally Posted by _jamie
This code works
Another one ticked off the list then You could muck around with lines 30 and 31 because the break isn't 100% necessary because you're checking ctr in the main loop anyway but it's quite readable as-is so *shrug*

11. If you get rid of the break and let the loop condition stop the loop, then the loop increment will run once more so the final printf, after the loop, will need to print p - 1 (as in my second example program).