Tander

May 8, 2013, 11:43 AM

Hi guys,

I am busy learning C on my MacBook through xCode as the DE.

The reason I am doing it this way is due to also going into iOS development. So, to be a better iOS developer, I want to make sure I understand the C language better too, as well as Objective-C. :D

I have started reading the book: Learn C on Mac (4th edition) by David Mark's, I think.

Now, the following bit of code I am having issues with - I understand how this code works 99% - the challenge in the code is to make the program compute prime numbers 1 to 100.

I have having difficulty figuring out how / where to implement a for / while loop to count to 100 (candidate is the int I need to get from 1 to 100)

#include <stdio.h>

#include <math.h> //used for extra math functions

#include <stdbool.h> // Used for the definition of truth

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;

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 number afer: %d is %d, Happy?" , startingPoint, candidate);

return 0;

}

}

After trying a few methods, I am stuck.

Can anyone help a newbie to C out and show me how this is done?

Also, if you can explain a little and not just provide the code, that would be great!

Thanks guys!

I am busy learning C on my MacBook through xCode as the DE.

The reason I am doing it this way is due to also going into iOS development. So, to be a better iOS developer, I want to make sure I understand the C language better too, as well as Objective-C. :D

I have started reading the book: Learn C on Mac (4th edition) by David Mark's, I think.

Now, the following bit of code I am having issues with - I understand how this code works 99% - the challenge in the code is to make the program compute prime numbers 1 to 100.

I have having difficulty figuring out how / where to implement a for / while loop to count to 100 (candidate is the int I need to get from 1 to 100)

#include <stdio.h>

#include <math.h> //used for extra math functions

#include <stdbool.h> // Used for the definition of truth

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;

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 number afer: %d is %d, Happy?" , startingPoint, candidate);

return 0;

}

}

After trying a few methods, I am stuck.

Can anyone help a newbie to C out and show me how this is done?

Also, if you can explain a little and not just provide the code, that would be great!

Thanks guys!