Thread: prime number generation, match 10001 prime number

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123

    Angry 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 */)
    Last edited by _jamie; 02-09-2020 at 10:47 PM. Reason: forgot tag

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    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;
    }
    Last edited by john.c; 02-09-2020 at 11:31 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123
    thanks john.c for the quick response, i am going over your code now and will formulate a lengthier response soon

  4. #4
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123

    Cool

    Quote Originally Posted by john.c View Post
    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. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Unlike Python, indentation means nothing to C. Statements are grouped together (a "compound statement") with braces.

  6. #6
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123

    Cool

    Quote Originally Posted by christop View Post
    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. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Quote Originally Posted by _jamie View Post
    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. #8
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123
    Quote Originally Posted by Hodor View Post
    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. #9
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123
    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. #10
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Quote Originally Posted by _jamie View Post
    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. #11
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    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).
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calculating if a number is a prime number or not
    By cooper1200 in forum C Programming
    Replies: 23
    Last Post: 06-08-2019, 11:58 AM
  2. Replies: 3
    Last Post: 12-02-2014, 10:11 AM
  3. Is a number Prime or not prime?
    By ImaCnoob in forum C Programming
    Replies: 27
    Last Post: 10-08-2012, 11:47 PM
  4. largest number prime number that can be produced...
    By ElemenT.usha in forum C Programming
    Replies: 8
    Last Post: 02-17-2008, 01:44 AM
  5. Replies: 3
    Last Post: 03-29-2005, 04:24 PM

Tags for this Thread