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

Hi guys, I'm trying to do the prime number generator thingy. I managed to write something that prints the prime numbers as well as about two hundred other random numbers. Anyone mind taking a look at it for me, see if you can tell where I messed up?? This is what I have so far d=2 c=2 while c<7920: if c%2==0: c=c+1 else: while (d<(c/2)): if c%2==0: c=c+1 elif c%d==0: c=c+1 else: d=d+1 d=2 print c c=c+1

OpenStudy (anonymous):

ausdan: It would be much better if you attached your code as well as post it. Copy / pastingint into the editor results in one very long line that would have to be formatted and indented.

OpenStudy (anonymous):

ausdan: Notably missing in your solution is a variable to hold whether the number is a prime or not. You need to print it only if it is actually a prime. Another consideration is that if a number is not divisible by 2 than it is automatically not divisible by all even numbers. If you fact this into your logic your code runs faster (is more efficient). At http://openstudy.com/users/chribonn#/updates/4f859edae4b0505bf085aced I posted my answers to this problem set. Hope this helps.

OpenStudy (anonymous):

Tricky, on first glance it appears to be working correctly. I think Chribonn is correct, you need a variable to hold whether the number is prime. So you could set up a variable isPrime = True, but if you find c is divisible by d, then set that to False. Then you can set a condition so that you only print c if isPrime is true. Have a look at the attached and see if that works. (I'm working through the same assignment so not an expert)

OpenStudy (anonymous):

start with candidat 2 which you know is prime so nbrOfFoundPrimes = 1 while nbrOfFoundPrimes is less than 1000 increment candidate test candidate with divisors between 2 and candidate - 1 if it is evenly divisible by any divisor it is not prime if it is not evenly divisible by any divisor it is prime if it is prime, increment nbrOfFoundPrimes

OpenStudy (vaboro):

How did you know that c should be less than 7920?

OpenStudy (anonymous):

i kinda cheated with the c<7920, i just looked at the link to the list of primes on the sheet and saw which one was the 1000th one :P

OpenStudy (anonymous):

treerock I was looking at your code, seems to work alright but what does the break command mean??

OpenStudy (anonymous):

I think I figured it out, does it just mean that you skip the else bit?

OpenStudy (vaboro):

http://pastebin.com/3F3SiqaR

OpenStudy (anonymous):

Hey Ausdan, yep kinda. Break means you skip the rest of the processing within the **current loop**. So in this example, the while loop comes to an end when it finds the first value that breaks the primality test.

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!