Thread: Unsure as to why Prime must be defined.

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    1

    Unsure as to why Prime must be defined.

    Morning,



    I bought the book, "Learn C on the Mac"
    So far have been enjoying it but have become stumped at one section.
    I was hoping someone could help.

    Its to do with the exercise on prime numbers in chap 6.
    Regarding the loop to determine whether a number is prime - to me it sounds like the program is able to make out what a prime number is yet we still need to prove a number is a prime.
    am i wrong in thinking this.

    Code:
    int main (int argc, const char * argv[]) {
        bool    isPrime;
        int     startingPoint, candidate, last, i;
        
        startingPoint = 24;
        
        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 ) {     
    
    
                 //  Here it looks as  
                //though the program can deduce whether a number is prime 
                //yet we still need to tell it what a prime number is.
               //    - the "&& isPrime" sounds as though it can deduce   
               //  whether a number is prime
    
                    if ( (candidate % i) == 0 )                       
                                                                      
                        isPrime = false;
                }
            } while ( ! isPrime );
        }
        
        printf( "The next prime after %d is %d. Happy?\n",
               startingPoint, candidate );
        return 0;
    }

    Thanks,
    /84.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Thing about C is that it knows nothing you don't teach it... and it's a major idiot, you have to tell it literally everything in little tiny steps and in the correct order. By the time it's on line 11, line 10 is forgotten...

    So by the time your code gets to the if(candidate % i) part it doesn't even know what it's actually testing, all it can tell you is True or False.

    Trust me, it has absolutely no freaking clue what a prime number is.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    C doesn't know what a prime number is, but because of the testing done in this part of the code, your program now can tell:

    Code:
    /* 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;
                }
    At the end of that loop, every odd number from 3 to last, has been divided into your candidate number. If nothing divides evenly into it, then it's prime. By that test alone, the program "knows" what a prime number is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unsure what this is actually for
    By Fixxxer in forum C Programming
    Replies: 3
    Last Post: 04-25-2010, 06:48 AM
  2. Need help with C. Unsure what to do next.
    By zenghoong in forum C Programming
    Replies: 1
    Last Post: 10-11-2009, 12:36 PM
  3. unsure
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 08-13-2002, 09:37 AM
  4. Little Unsure?
    By kas2002 in forum C Programming
    Replies: 8
    Last Post: 06-12-2002, 08:18 PM
  5. Help unsure
    By Joel in forum C++ Programming
    Replies: 1
    Last Post: 02-12-2002, 12:00 PM