Last active
May 3, 2019 23:59
-
-
Save meksor/91ccd13ace0e5da5a828ed096c5dd32b to your computer and use it in GitHub Desktop.
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
import itertools | |
import time | |
class Sieve: | |
primes = [2, 3, 5, 7] | |
def __init__(self, end=-1): | |
self.end = end | |
def __iter__(self): | |
for n in itertools.count(start=11): | |
if str(n)[-1:] in ['1', '3', '7', '9']: # Yes this is actually faster from 3 primes onwards | |
if not any(n % p == 0 for p in self.primes): | |
self.primes.append(n) | |
yield n | |
if n == self.end: | |
break | |
if __name__ == "__main__": | |
sieve = Sieve(end=100000) | |
start = time.time() | |
try: | |
for prime in sieve: | |
pass | |
except KeyboardInterrupt: | |
pass | |
print('\nFound {} Primes in {} seconds.'.format(len(sieve.primes), time.time() - start)) | |
print('Biggest: {}'.format(sieve.primes.pop(-1))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment