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

Finally problem 1 finished!! # Finding up to the 1000th prime x = 0 count = 0 div = [] primes = [] while count <= 1000: if x > 1 and not(x%2) == 0: for i in range(1, 1+x): if x%i == 0: div += [i] if div[1] == x: primes += [x] count += 1 x += 1 del div[:] However, I don't know how to account for 2.. Also, please critique the code. Thanks

OpenStudy (anonymous):

just add 2 to primes before you loop. If you're taking advantage of the fact that primes aren't even you're using some apriori knowledge of the problem, and doing so also implies that 2 is a prime.

OpenStudy (anonymous):

other things, try to avoid using variables like i, and x. Instead name them things that make sense i is a possibleDivsor, x is a possiblePrime. Also div = [] is a bit nicer than del div[:] in terms of expressing what you mean. Just move the initialization of div into your while loop at the top and you won't have to delete it at the bottom of the loop.

OpenStudy (anonymous):

Cool, thanks polak

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!