I have a python class that generates the nth prime number by starting at the n-1th prime and increments. Then divides by all the primes already in the list up to floor(sqrt(candidate)). But my class just gets into an infinite loops somewhere and I can't figure out why.
class prime_list():
def __init__(self):
self.primelst = [1]
self.n = 1
def increment(self):
self.n+=1
candidate = self.primelst[-1]+1
limit = int(math.floor(math.sqrt(candidate)))
prime = True
while True:
for p in self.primelst:
if p>limit:
break
if (candidate % p) == 0:
prime = False
break
if prime:
self.primelst.append(candidate)
return
else:
candidate += 1
limit = int(math.floor(math.sqrt(candidate)))
prime = True
if __name__=="__main__":
p = prime_list():
p.increment()