I am entering my code for problem set 1 problem 1, (http://pastebin.com/nzG9Nva7, under v:3.0), and recieve 7921 instead of what it should be (which is 7919). Can someone point me in the right direction as to what I'm doing wrong?
The error is with your code at line 57 prime_number_candidate=prime_number_candidate+2 So even though your loop ends at the correct iteration, you actually incremented prime_number_candidate before the last iteration ends. That's why you get your last prime number (7919) + 2. There are several ways to solve this, however each solution have different merits/faults. 1. subtract 2 from your prime_number_candidate after you exit the outer while loop. To get your last prime number. 2. use another variable (lastPrime) to store the last prime number found (not the next prime_number_candidate), and print the variable lastPrime . 3. initialize your loop differently such that you increment prime_number_candidate as the first (or one of the first) instructions in your outer while loop. This would cause prime_number_candidate to be equal to the prime number (if the number is prime).
Man, It's always the little things that I miss...oh well. I chose number three, and have implemented it. Thanks for the help stuyboys!
You're welcome.
Join our real-time social learning platform and learn together with your friends!