# how can describe this for me?

Discussion in 'Mac Programming' started by midnight14, Sep 10, 2011.

1. ### midnight14 macrumors newbie

Joined:
May 16, 2010
Location:
Saudi Arabia
#1
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

File size:
1.8 KB
Views:
30
2. ### lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#2
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

Joined:
May 16, 2010
Location:
Saudi Arabia
#3

4. ### freeki macrumors newbie

Joined:
Sep 7, 2011
#4
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.

5. ### lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#5
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

6. ### rien333 macrumors regular

Joined:
Jun 29, 2010
Location:
The Netherlands
#6
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.

7. ### midnight14 thread starter macrumors newbie

Joined:
May 16, 2010
Location:
Saudi Arabia
#7
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

Joined:
Feb 11, 2009
#8