Hi guys
Okay, so a lot to reply to here:
@Chown33:
Your reply really help me. I thought I understood the code 99% from the first example that I posted. I then took your advice and went step-by-step through the code on my own, with a calculator to see if I could match the computers result with my result.
I was around 60% accurate and thus I knew I was missing the point of some of the code. I tracked it down to the do-while, loop. It seems I was expecting it start at the top of the loop every time and increment candidate, even if it wasn't a prime.
So after some fiddling and putting printf statements all over the place so I could "see" when / where candidate and last (The sqrt of candidate ) were incremented and sometime why - it was only then I figured out exactly how it works.
So, to test this new found knowledge of how the code works I wrote down 15 random numbers between 20 and 600. I then calculated the next prime number for all those numbers and matched it to the computer 100% of the time!!
So, now I understood
exactly how this code was getting the next prime number I decided to tackle my original problem.
Looking at the second code I posted I realized it would never work for two reasons:
1. Having the for loop start within the else statement was not a good idea and this did not work too well. (Unpredictable results) (I'm going through the code again to see exactly what it does when I put it inside the else statement)
2. I had terminaled the loop with:
- this termination happened inside the for loop - so I was forcing the for loop to end - regardless of results - silly me!
I picked this up when I was the I was only getting one printf statement in the console - which, was correct and was the first next prime number after 3. So I figured my for loop was ending prematurely.
Once I realized these things - I moved the termination code (Code that tells the computer program ran okay, no errors, correct?) to outside of the loop, at the end of the main function:
I then moved the for loop to start at the very top and for it to initialize "startingPoint"
This is the modified code:
Code:
int main(int argc, const char * argv[])
{
bool isPrime;
int startingPoint, candidate, last, i;
for (startingPoint = 0; startingPoint <= 100; ++startingPoint){
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?\n" , startingPoint, candidate);
}
}
return 0;
}
The output is as expected:
Code:
The next prime number afer: 3 is 5, Happy?
The next prime number afer: 4 is 5, Happy?
The next prime number afer: 5 is 7, Happy?
The next prime number afer: 6 is 7, Happy?
The next prime number afer: 7 is 11, Happy?
The next prime number afer: 8 is 11, Happy?
The next prime number afer: 9 is 11, Happy?
The next prime number afer: 10 is 11, Happy?
The next prime number afer: 11 is 13, Happy?
The next prime number afer: 12 is 13, Happy?
The next prime number afer: 13 is 17, Happy?
The next prime number afer: 14 is 17, Happy?
The next prime number afer: 15 is 17, Happy?
The next prime number afer: 16 is 17, Happy?
The next prime number afer: 17 is 19, Happy?
The next prime number afer: 18 is 19, Happy?
The next prime number afer: 19 is 23, Happy?
The next prime number afer: 20 is 23, Happy?
The next prime number afer: 21 is 23, Happy?
The next prime number afer: 22 is 23, Happy?
The next prime number afer: 23 is 29, Happy?
The next prime number afer: 24 is 29, Happy?
The next prime number afer: 25 is 29, Happy?
The next prime number afer: 26 is 29, Happy?
The next prime number afer: 27 is 29, Happy?
The next prime number afer: 28 is 29, Happy?
The next prime number afer: 29 is 31, Happy?
The next prime number afer: 30 is 31, Happy?
The next prime number afer: 31 is 37, Happy?
The next prime number afer: 32 is 37, Happy?
The next prime number afer: 33 is 37, Happy?
The next prime number afer: 34 is 37, Happy?
The next prime number afer: 35 is 37, Happy?
The next prime number afer: 36 is 37, Happy?
The next prime number afer: 37 is 41, Happy?
The next prime number afer: 38 is 41, Happy?
The next prime number afer: 39 is 41, Happy?
The next prime number afer: 40 is 41, Happy?
The next prime number afer: 41 is 43, Happy?
The next prime number afer: 42 is 43, Happy?
The next prime number afer: 43 is 47, Happy?
The next prime number afer: 44 is 47, Happy?
The next prime number afer: 45 is 47, Happy?
The next prime number afer: 46 is 47, Happy?
The next prime number afer: 47 is 53, Happy?
The next prime number afer: 48 is 53, Happy?
The next prime number afer: 49 is 53, Happy?
The next prime number afer: 50 is 53, Happy?
The next prime number afer: 51 is 53, Happy?
The next prime number afer: 52 is 53, Happy?
The next prime number afer: 53 is 59, Happy?
The next prime number afer: 54 is 59, Happy?
The next prime number afer: 55 is 59, Happy?
The next prime number afer: 56 is 59, Happy?
The next prime number afer: 57 is 59, Happy?
The next prime number afer: 58 is 59, Happy?
The next prime number afer: 59 is 61, Happy?
The next prime number afer: 60 is 61, Happy?
The next prime number afer: 61 is 67, Happy?
The next prime number afer: 62 is 67, Happy?
The next prime number afer: 63 is 67, Happy?
The next prime number afer: 64 is 67, Happy?
The next prime number afer: 65 is 67, Happy?
The next prime number afer: 66 is 67, Happy?
The next prime number afer: 67 is 71, Happy?
The next prime number afer: 68 is 71, Happy?
The next prime number afer: 69 is 71, Happy?
The next prime number afer: 70 is 71, Happy?
The next prime number afer: 71 is 73, Happy?
The next prime number afer: 72 is 73, Happy?
The next prime number afer: 73 is 79, Happy?
The next prime number afer: 74 is 79, Happy?
The next prime number afer: 75 is 79, Happy?
The next prime number afer: 76 is 79, Happy?
The next prime number afer: 77 is 79, Happy?
The next prime number afer: 78 is 79, Happy?
The next prime number afer: 79 is 83, Happy?
The next prime number afer: 80 is 83, Happy?
The next prime number afer: 81 is 83, Happy?
The next prime number afer: 82 is 83, Happy?
The next prime number afer: 83 is 89, Happy?
The next prime number afer: 84 is 89, Happy?
The next prime number afer: 85 is 89, Happy?
The next prime number afer: 86 is 89, Happy?
The next prime number afer: 87 is 89, Happy?
The next prime number afer: 88 is 89, Happy?
The next prime number afer: 89 is 97, Happy?
The next prime number afer: 90 is 97, Happy?
The next prime number afer: 91 is 97, Happy?
The next prime number afer: 92 is 97, Happy?
The next prime number afer: 93 is 97, Happy?
The next prime number afer: 94 is 97, Happy?
The next prime number afer: 95 is 97, Happy?
The next prime number afer: 96 is 97, Happy?
The next prime number afer: 97 is 101, Happy?
The next prime number afer: 98 is 101, Happy?
The next prime number afer: 99 is 101, Happy?
The next prime number afer: 100 is 101, Happy?
YAY!!!
So happy when I made this realization - once I figured out exactly how the original code worked - it took a whole of 30 seconds to modify to to work as I wanted it to!
Once again, thank you Chown33!
@ ArtOfWarfare:
I wasn't guessing at first - I could only see the first result in the console and this was a clue that the loop only ran once or not at all. I later figured out adding more printf statements would help me track the problem down!
Thanks!
@ mrichmon
I realize now you actually provided me with a clue (indentation) which was the biggest problem with the modified code I posted. Tomorrow I will look at your own code and see if I can fill that function in - although I now understand my original code 100% - I must keep getting better at this and more practice!
@ DesertEagle
isPrime is set to true at the top of the do-while, loop - at the bottom we check to see if candidate really is prime - if it is not - we set isPrime to false - the loop will continue to run until we can not divide candidate
evenly by " i " and thus, leaving isPrime as true - terminating the do-while, loop.
"Last" will always have a value as it gets assigned the square root of candidate - and candidate gets its value from startingPoint, which always has a value:
So:
startingPoint is initilized to a value by the for loop at the top (Modified code)
half way down, we assign startingPoint's value to candidate:
Code:
candidate = startingPoint;
This is after we check and make sure candidate is higher than 3 - since we know, 2 is the next prime number after 1 and 3 is after 2.
Once that happens and we get into the d-while, loop (I am skipping a bit of code here, I know, but I don't think it needs me to explain? )
we then get the square root of candidate
Code:
last = sqrt( candidate );
So, as far as I understand it - last will always have a value.
Also, the do-while, loop is not conditioned upon last having a value - it's conditioned upon this:
I may have it wrong - but this is how I understand some of the code?
I didn't feel it was needed for me to explain the whole code, step by step?
I think i will give myself a pat on the back?
Appreciate all the help guys - I know this was a "simple" problem, but it has helped me a lot I will now make sure I understand the code 100% before trying to change it. It has also taught me more about loops and math functions than before. So I guess it's good progress?