Created
June 30, 2015 17:39
-
-
Save theSage21/268b72da941c3a5c2584 to your computer and use it in GitHub Desktop.
Prime number generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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