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

Prime number generator help needed.... okay, here is my next try...does anyone have a suggestion? I hope I am getting closer. prime = 3 count = 2 while count < 1001: prime = prime + 2 divide = prime * 0.5 for prime in range(3,prime,2): if prime % divide > 0: divide = divide - 2 else: count +=1 prime +=1 print prime

OpenStudy (anonymous):

def primefinder(x): ''' to find the xth prime no.''' if x == 1 : return 2 else : primeNo = [] # list of prime no.s primeChecker = 3 primeConfirm = True #confirmation of prime no. count = 1 while count <= x : while primeConfirm == True : for i in range ((len(primeNo)+1)/2): # check for a prime no if (primeChecker%primeNo[i]) == 0 : primeConfirm = False break if primeConfirm == True : primeNo = primeNo + [primeChecker] primeChecker += 2 break primeChecker += 2 primeConfirm = True count += 1 print x,'th prime no. is',primeNo[-1] Better code

OpenStudy (anonymous):

I know I don't write the best code just yet, but I can only think the way I think. I guess I'm wondering if anyone has the ability to help with the code the way I am framing it, or if I should quit and copy someone elses code, and pretend I did it. That's kind of where I'm at.

OpenStudy (anonymous):

i am not asking u to copy it but learn the idea(when to stop checking for prime no.) behind this code. May be just going through it u may get a better insight of the problem. anyways, for prime in range(3,prime,2): # u cann't use same name for two different variables. if prime % divide > 0 # remainder is always positive irrespective of all and please first try ur codes on smaller values of which u know what is the answer is going to be try to debug it. any if still there is any problem post it here again.

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!