Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

midnight14

macrumors newbie
Original poster
May 16, 2010
23
0
Saudi Arabia
Please don't disappoint me because my mind has got tired of this problem.
Now I have reached to page #128 in a book named "Learn C On The Mac" by Dave Mark. Until this page everything was fine I've implemented all the exercises and understood each single line of codes until that page. but when I encountered a program's code called nextPrime then the problem has began. I know what the prime number is but I didn't understand it as it is described in the book. The code is attached

So please someone describe the code clearly step by step or guide to me any resources such as a link or "video tutorial"<-- this is preferred.

I will not give up till I understand this entire code.


#include <stdio.h>
#include <stdbool.h>
#include <math.h>

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)
candidate--;
do {
isPrime = true;
candidate += 2;
last = sqrt( candidate );

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;
}

thank you
 

Attachments

  • nextPrime.txt
    1.8 KB · Views: 65
Code:
#include <stdio.h>
#include <stdbool.h>  
#include <math.h> 

int main (int argc, const char * argv[]) {
    bool    isPrime;
    int     startingPoint, candidate, last, i;
	
    startingPoint = 235; //Looks like this is a "seed" to start from. Normally this would be entered by a user or as a command line parameter. I'd consider this "test data"
	
    if ( startingPoint < 2 ) { //I guess this program doesn't want to test negatives?
        candidate = 2; //I don't understand the logic, but i guess this program considers 2 the first prime above anything less than 2
    }
    else if ( startingPoint == 2 ) {
        candidate = 3; //Three is the first prime above 2
    }
    else { //If the starting point is 3 or above, run the algorithm
        candidate = startingPoint; //Start testing with the starting point
        if (candidate % 2 == 0)  //If the candidate is even, subtract 1           
            candidate--;
        do { //Run the algorithm in a loop until a prime is found
            isPrime = true;  //Assume the candidate is prime              
            candidate += 2;   //Increment the previous, known-odd candidate to the next odd value to test             
            last = sqrt( candidate );  //This seems sloppy. sqrt accepts and returns a double.  The assignment from double to int will truncate, which i guess is the desired behavior, but this should be clearer
           
            for ( i = 3; (i <= last) && isPrime; i += 2 ) { //Loop from 3 to the sqrt of the candidate, stopping if the candidate is found not to be prime. Increment i, the control variable, by 2 each iteration (since it's known that the candidate is odd, so won't be divisible by an even)
                if ( (candidate % i) == 0 ) //If there is no remainder of integer division of candidate by the current control variable, meaning this value is not prime
                    isPrime = false; //set isPrime to false, indicating that the candidate is not prime. Try again.
            }
        } while ( ! isPrime ); //If a prime is found, we're done. Hooray!
    }
	
    printf( "The next prime after %d is %d. Happy?\n",
           startingPoint, candidate ); //Print the result we found
    return 0; //Nothing failed! Huzzah. 
}

This is an awful algorithm for testing prime-ness. Were there particular parts of this program that didn't make sense to you? Do the comments added clear it up, or are there still things that don't make sense?

-Lee
 
Code:
#include <stdio.h>
#include <stdbool.h>  
#include <math.h> 

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; 
    }...
    
This is an awful algorithm for testing prime-ness. Were there particular parts of this program that didn't make sense to you? Do the comments added clear it up, or are there still things that don't make sense?

-Lee[/QUOTE]

Thanks for replying... Why should we shack if startingPoint is less than 2 while it is set to be 235?

And why should we find the next prime after 2 while we are looking for the next prime after 235?

I mean why we begin  with small numbers whereas the number that we set to nextPrime and the number we are looking for is grater than them all?
 
Thanks for replying... Why should we shack if startingPoint is less than 2 while it is set to be 235?

And why should we find the next prime after 2 while we are looking for the next prime after 235?

I mean why we begin with small numbers whereas the number that we set to nextPrime and the number we are looking for is grater than them all?

I don't have the book, but perhaps because in a few pages the author will say "So, now let's let adapt the program we wrote on page 128 and let it accept a parameter when we run the program."? Or it will be encapsulated in a function?

Anway, it's probably just a copy-paste backwards that doesn't make much sense, but got overlooked during the book editing.
 
As-is, the code only finds the next prime > 235. If that was the real point of the program it could consist of a single print statement saying that 239 is the first prime after 235, and all the logic could be dispensed with. I'm confident that using that set starting value is just test data to avoid the complexity of input on the first crack. This isn't too uncommon, once you get code working and can test a number of inputs then you can wire them up to an interface instead.

-Lee
 
I'm reading the same book, and I didn't fully understand this exercise either. However, I did fully understand all the other content in the chapter. So I thought: "Do I really need to understand this (at this moment) to become a good programmer?" My answer was no, so I just moved on. I've almost finished the book, and I was able the understand everything. My advise: Just skip this exercise. You won't need it.
 
I'm reading the same book, and I didn't fully understand this exercise either. However, I did fully understand all the other content in the chapter. So I thought: "Do I really need to understand this (at this moment) to become a good programmer?" My answer was no, so I just moved on. I've almost finished the book, and I was able the understand everything. My advise: Just skip this exercise. You won't need it.

good, you are encouraging me, you almost touched my thoughts. But someday we must get back to resolve this issue. God willing.

Sincerely

thank you all
 

#include <stdio.h>
#include <stdbool.h>
#include <math.h>

int main (int argc, const char * argv[]) {
bool isPrime;
int startingPoint, candidate, last, i;

startingPoint = 235;

//The candidate variable will handle the fact that 1 is not a prime number
if ( startingPoint < 2 ) {
candidate = 2;
}
else if ( startingPoint == 2 ) {
candidate = 3;
}
else {
candidate = startingPoint;
if (candidate % 2 == 0) //Start off at an odd number
candidate--;
do {
isPrime = true;
candidate += 2; //Go to the next odd number
last = sqrt( candidate ); //No factor is greater than the sqrt of a number

for ( i = 3; (i <= last) && isPrime; i += 2 ) {
if ( (candidate % i) == 0 )
isPrime = false; //Check for factors
}
} while ( ! isPrime ); //Continue cycling through odd numbers until a prime number is found
}

printf( "The next prime after %d is %d. Happy?\n",
startingPoint, candidate );
return 0;
}

thank you

I think these comments should help you understand the logic.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.