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

I'm having a problem with ps1 problem 2?

OpenStudy (anonymous):

http://dpaste.com/569885/

OpenStudy (anonymous):

I ran the same code in Java and it worked fine!

OpenStudy (anonymous):

for i in range(3,int(sqrt(prime))) The square root of 3 (your initial prime) is less than 3.

OpenStudy (anonymous):

print statements can help you figure it out, try this: http://dpaste.com/569910/ Python is a loosely typed language which will get you in trouble (sometimes)) when you try to perform operations on different types. look at the docs for the raw_input function and make sure it returns what you want i think your optimization, int(sqrt(prime)), is giving you trouble: look at the docs for the range function

OpenStudy (anonymous):

@Lifeburner: That doesn't matter because if you notice, in the if statement, flag registers as 0, so log(3) automatically gets added to log(2) in sum. Like I said, I ran this program on Java, and it worked perfectly :)

OpenStudy (anonymous):

@bwCA: I didn't quite understand what the problem was? Are you saying that the typecasting is causing the error? Thing is, just sqrt(prime) says float was found where int was expected :/

OpenStudy (anonymous):

Ok, the thing is, the "while" is going into an infinite loop despite the prime<=n condition.

OpenStudy (anonymous):

Yes, bwCA is right. Your n is not properly cast into int. I wrapped int() round your n prompt, and it worked.

OpenStudy (anonymous):

OMG Lifeburner! Genius! That was such a stupid mistake! I was comparing a string and an integer! Thank you a million times over!

OpenStudy (anonymous):

time to add Lifeburner to the elite tuple of enlightenedMinds then :) enlightenedMinds = enlightenedMinds + ('Lifeburner',)

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!