Hey there... I'm new to C and this forum, I'm going through Learn C on a Mac by Mark Davis. In his book he has this code to search for prime numbers between 1 and 100. The code works, its just that I don't really understand why and its driving me crazy!!!!! I don't know what I'm missing.
Here's the code,
headers.....
Code:printf("Prime numbers from 1 to 100 are, 2,"); for(candidate=3;candidate<=100;candidate+=2){ isPrime=true last=sqrt(candidate); for(i=3:(i<=last) && isPrime;i+=2){ if((candidate%i) == 0) isPrime=false } if(isPrime) printf("%d,",candidate); } return 0; }
In the second for he has i=3 (initialization) and i<=last && isPrime (termination). On the first run, if the cadidate is 3 and i=3 then the sqrt(3) will never be bigger than 3(candidate)....What am I missing??????
Thank you in advance for your help...



LinkBack URL
About LinkBacks



