Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 29 Online
OpenStudy (anonymous):

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?

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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!

OpenStudy (anonymous):

You're welcome.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!