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

For the first assignment in 6.00. (The 1000th prime, and output for the log(x) problem) Would like some feedback on this. I think there's a smarter way to do it, but it should be readable. There are a few shortcuts that I would love to iron out, like the n2 = n.

OpenStudy (anonymous):

OpenStudy (anonymous):

You can get rid of PrimeCandidates and you can test the divisors from 2 until the square root of the number you are currently testing ^^

OpenStudy (anonymous):

Right, I can just increase the number to test by 2. And I saw the squareroot solution after writing the code, but I'll try and tidy it up :)

OpenStudy (anonymous):

Eh, I ended up just doing a validation on the square root before doing the divisor-test. The code isn't pretty, but it works, and I'm happy enough with it for now.

OpenStudy (anonymous):

Don't worry about having a "pretty code". You'll learn that as your progress. It's good that you really try to improve your code. :) By the way, for your loop, you can do this: for div in range(2, int(Primetest**0.5) + 1): - x**n gets the nth power of x - int(data) converts the data type of data to int. It doesn't do rounding. It just removes the fractional components, ie. int(4.236273) = 4, int(2.968457) = 2 -The purpose for +1 is because when you use range(a,b), it only has the list [a, a+1, ...., b-1]

OpenStudy (anonymous):

Thanks! That's awesome :)

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!