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

Please help, am i anywhere close with ps1? count = 0 n = 1 while (count<=1000) if n<=2: n+1 count+1 while n>2: if n/2: break if n%>n: else: count+1 print n

OpenStudy (anonymous):

your indentation is messed up. pls use a code pasting site: dpaste.com, pastebin,com, codepad.org, ideone.com

OpenStudy (anonymous):

totalNumberOfPrimeNumbers=2 #exclude prime number 1 & 3 from the list n = 3 while totalNumberOfPrimeNumbers != 1000: n += 2 #next odd integer to test if it's prime number prime = True #assume it is a prime number for i in range(3, n, 2): #check for only odd number as divisor if (n % i) == 0: prime = False #if prime number test fails, change boolean to False break #no reason to continue testing if (prime == True): totalNumberOfPrimeNumbers += 1 print ('1000th Prime Number is {0}.'.format(n))

OpenStudy (anonymous):

@bwCA http://codepad.org/nlB6CSYa hope this links it

OpenStudy (anonymous):

@ paul30lee Thanks for posting this, but im not sure how the part where you "check for only odd number as divisor" works. I've been trying to use it and see how it works but I'm lost there. This is what i had but I couldn't get it to exclude some non primes. http://codepad.org/Sntd9hpw

OpenStudy (anonymous):

jswtella - at line ten you cannot combine the % operator and the < operator. you need to keep track of three things that are changing through a range of values - count (the number of primes you have found), n (the number you are testing) and div (? - the number you are testing n with) one way to do this is to have two loops: oan outer loop that 'generates' and tests numbers to see if they are prime an inner loop that 'generates' divisors to test n with

OpenStudy (anonymous):

@ ks11 I was trying to put a #n is not divisible by anything less than n# in there somewhere but wasn't sure if that was possible or would actaully calculate accuratley.Thanks for having a look. following bwCA's comments i will have to have a little re-think, i don't think i'm a million miles away!

OpenStudy (anonymous):

you might want to consider using a while loop to 'keep track of' count (like you are doing) and generating new n's then use a for loop with the range function for generating divisors http://docs.python.org/tutorial/controlflow.html#for-statements http://codepad.org/n2s0a7Kh

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!