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

Can someone take a look at my code from ps1a: I think I am close, but am not sure why it sometimes spits out non-prime numbers. http://dpaste.com/577796/

OpenStudy (anonymous):

ok, figured out your problem because you add 2 after you find your prime when you print x outside your loop you print the last prime+2 second your loop ends one prime to early because your while itsleft>0, your expecting the behavoir of itsleft>=0, there a piles of ways to fix all of this its up to your style one way has you print x before you add 2 http://dpaste.com/577806/ the other way moves where you add to the begining of your loop, and i changed the >=0 bit just to show you what i ment http://dpaste.com/577809/ hope that made sense

OpenStudy (anonymous):

Awesome, thank you so much! Your first solution would print every single prime, though. The second solution works great.

OpenStudy (anonymous):

something you'll want on a long program, something popping up on screen to tell you that your program didn't crash, also i think it looks nice

OpenStudy (anonymous):

Forgive me for playing devil's advocate, but I spent many long hours on problem 1 in search of finding a way to define prime numbers and my prime tests did not work right.. Please help me spot my error, and then I'll demonstrate a simpler solution I developed. http://dpaste.com/577985/ By doing this, I only generated a list of odd numbers, not a list of prime numbers. For this reason, I don't believe that dividend%i==0 is a good test for finding a prime number. I believe my error was that once the computer ran a test for dividend%i==0 and found it false for any i, it immediately resorted to the "else" column and added the dividend to my list of primes. Therefore, a number like 15 (not prime) would be added to my list of primes because 15%2!=0. For those who had success using dividend%i==0 as a prime test, how did you avoid this? What I ended up using instead (much simpler) was a mathematician's definition of prime, specifically the Fermat primality test: if ((2**dividend)-2)%dividend==0, then number is prime. This prime check worked, which meant I only needed to control the flow until my prime list was created. I was able to generate the prime list, and my code is very short and simple. SO MUCH EASIER! Is this cheating? I didn't eliminate positive numbers as an initial step (as suggested on the assignment), but why should I have to if Fermat's test will do so for me automatically? Anyways, eliminating positive numbers could easily have been included, simply by testing for dividend%i==0 :) So my (final) question is why generate new methods for defining prime numbers when mathematicians have already done the work for us? But that's just my take on it. Somebody set me straight :)

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!