Skip to content

Instantly share code, notes, and snippets.

@theSage21
Created June 30, 2015 17:39
Show Gist options
  • Save theSage21/268b72da941c3a5c2584 to your computer and use it in GitHub Desktop.
Save theSage21/268b72da941c3a5c2584 to your computer and use it in GitHub Desktop.
Prime number generator
class Primer:
def __init__(self, store_name='data'):
self.store_name = store_name
try:
fl = open(self.store_name, 'r')
fl.close()
except:
with open(self.store_name, 'w') as fl:
fl.write('2\n')
def primes(self):
with open(self.store_name, 'r') as fl:
for i in fl.readlines():
yield int(i)
def is_prime(self, no):
for i in self.primes():
if no % i == 0:
return False
return True
def add_prime(self, no):
with open(self.store_name, 'a') as fl:
fl.write(str(no) + '\n')
def generate(self):
no = [i for i in self.primes()][-1]
while True:
no += 1
if self.is_prime(no):
self.add_prime(no)
print(no, end='\r')
if __name__ == '__main__':
pr = Primer()
pr.generate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment