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

PS 1a I am attempting to complete PS 1a. I got it to work the LONG way (manually checking each division and concatenating result). I was trying to get a bit more efficient and automatically calculate each division, but I can't get it to work. Any suggestions? My code is below: x=2 count=0 count_max=1000 while count

OpenStudy (anonymous):

i tweaked it and put in some comments but didn't test it http://dpaste.com/740808/

OpenStudy (anonymous):

Here's another way to do it: http://pastebin.com/4FgBPcq8 Also, in@ bwCA 's function, it can be a bit faster if you start with 5 instead and increament x by 2 instead of 1.

OpenStudy (anonymous):

You have a problem with your inner while loop: while y > 1: if x % y == 0: # not prime - stop checking #count=count break else: y = y - 1 if y == 1: #if you got here it must be prime count = count + 1 ********* The problem is that you will not be able to execute the part where I marked *********. This is because the condition of your while loop is "while y>1". Therefore, the result of "if y == 1" inside this while loop will always be False since all the y's that are tested here are all greater than 1. Hence, count = count + 1 will never be executed. My suggestion is to take this "if" block outside of the while loop since after the while loop finishes, the value of y is surely 1 if it is prime. Go test it. You have to add some more lines to make it work but you have to figure that out on your own. :)

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!