Prime number example

This is a discussion on Prime number example within the C Programming forums, part of the General Programming Boards category; Hi, I'm going through the "Learn C on the Mac" book and getting a bit stuck. I've read through this ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Belfast, Northern Ireland
    Posts
    9

    Prime number example

    Hi, I'm going through the "Learn C on the Mac" book and getting a bit stuck. I've read through this part of the book countless times now. The exercises at the end of the chapter are modify teh program to computer the prime numbers from 1-100 and another one is to compute the first 100 prime numbers.
    Code:
    #include <stdio.h>
    #include <stdbool.h>  //This is to bring in the define of true
    #include <math.h>  //This is to bring in the define of sqrt()
    
    int main (int argc, const char * argv[]) {
        bool    isPrime;
        int     startingPoint, candidate, last, i;
    	
    	startingPoint = 235;
    	
    	if ( startingPoint < 2 ) {
    		candidate = 2;
    	}
    	else if ( startingPoint == 2 ) {
    		candidate = 3;
    	}
    	else {
    		candidate = startingPoint;
    		if (candidate % 2 == 0)             /* Test only odd numbers */
    			candidate--;
    		do {
    			isPrime = true;                 /* Assume glorious success */
    			candidate += 2;                 /* Bump to the next number to test */
    			last = sqrt( candidate );       /* We'll check to see if candidate */
    			/* has any factors, from 3 to last */
    			/* Loop through odd numbers only */
    			for ( i = 3; (i <= last) && isPrime; i += 2 ) {
    				if ( (candidate % i) == 0 )
    					isPrime = false;
    			}
    		} while ( ! isPrime );
    	}
    	
    	printf( "The next prime after %d is %d. Happy?\n",
    		   startingPoint, candidate );
    	
    
        return 0;
    	
    	
    }
    I understand what's going on in the code, just not sure how to implement those features. I don't really want someone to do it for me, just give me a few pointers.

    Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you know how to repeat <X> from 1 to 100? Do you know how to do <X> until you have counted to 100? Do you know which of the above code is <X>?

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Belfast, Northern Ireland
    Posts
    9
    A loop like
    Code:
    for (i=1;i<=100;i++){
    		printf("number: %d\n",i);
    	}
    ?
    And I take it <X> is...
    Code:
    for ( i = 3; (i <= last) && isPrime; i += 2 ) {
    				if ( (candidate % i) == 0 )
    					isPrime = false;
    I think, where it's going through all the odd numbers to check if they are prime numbers or not?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. largest number prime number that can be produced...
    By ElemenT.usha in forum C Programming
    Replies: 8
    Last Post: 02-17-2008, 12:44 AM
  3. Prime Factor Fxn
    By alpha in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2003, 10:44 AM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. Replies: 3
    Last Post: 01-14-2003, 09:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21