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

So I have a question about the prime question in problem set 1. Im trying to approach the question as a list problem. I already learned to test for primes in my code but Im having difficult asking for the 1000th term since pyscritpter keeps crashing, I think there is something wrong with my while loop... def search_primes(n): #I start by having an empty list terms=[] #Then we assigned i the value of 0 because we want to start counting. i = 0 #the loop would stop as soon as i is equal to n while i<=n: # My condition is that in order to append to the list the num

OpenStudy (anonymous):

def search_primes(n): #I start by having an empty list terms=[] #Then we assigned i the value of 0 because we want to start counting. i = 0 #the loop would stop as soon as i is equal to n while i<=n: # My condition is that in order to append to the list the number should # pass the Prime_Test which already has 0,1, and 2 in consideration. if Prime_Test(i) is True: #Here is the new assigned value of i gets added to the list terms.append(i) #just to make sure the list keeps going up i = i + 1 #We're interested on the final value of the terms return terms[n]

OpenStudy (anonymous):

Your logic is sound but I think I see where the problem occurs. Let's go over your steps in pseudo-code. # first you initialize a set of variables # terms is a list [] that will store your list of primes as you find them # n is the nth prime that you are looking for; in this case, it will be 1000 but you are writing as a function so you can generalize # i is the particular number you are testing for primality ### a problem that i see is that you are missing a variable that's keeping track of which prime number your are at; you will need to define a variable such as count # you then setup a while loop to cycle through the integers until you have find the 1000th prime. so your while loop needs to look like this: while count<=n: not while i<n: you will increase count by 1 when i is a prime. i is the number you are testing for primality. count is the count you are on in terms of your search for the nth prime. Hope that helps.

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!