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

I've been working on problem 2 for a while now trying to get it to print primes,but it's printing odd numbers instead, what's wrong with it? I'm sure I've made a stupid mistake but I just can't figure it out.. http://codepad.org/Ykb28Rox

OpenStudy (anonymous):

a couple of things i'm not sure lines 9 and 10 are indented correctely if you want to test the number 7 to see if it is prime, you want to try 7 % 6, 7 % 5, 7 % 4, 7 % 3, 7 % 2, i moved the print statement to illustrated that- http://codepad.org/deGyrB6O

OpenStudy (anonymous):

Also, using num to control the while loop won't get you the 1000th prime number, since the while loop breaks when num > 1000 and that's nowhere near the 1000th prime :-D you might want to use a separate variable to keep track of the number of primes you have found. Also, what are you really using the Prime boolean for? I've taken your code and done something a little differently with it: http://codepad.org/JSFHQ9S9

OpenStudy (anonymous):

Thanks, I honestly was just trying things, I don't completely understand programming hah,but I want to learn.

OpenStudy (anonymous):

Mariac- I have discovered that the problem sets can be solved with the limited knowledge you have at that particular point in the lectures & readings. However, those solutions can be inefficient and clumsy. If you go back to the early sets after learning more, you'll discover that you now have knowledge to solve them much quicker, easier, and more efficiently. So hang in there- it will all click into place given time.

OpenStudy (tonybaldwin):

You're not testing anywhere in that code for whether the number is prime or not, Mariac. You're only testing for a remainder of 1 (==1). You would have to test for every possible remainder in this configuration. You could have a remainder of 3, 5, 7, etc. Test to see if you have no remainder (num%x==0), in which case, you know it is NOT prime, to reject it.

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!