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