Skip to content

Instantly share code, notes, and snippets.

@hayd
Created September 14, 2012 11:54
Show Gist options
  • Save hayd/3721517 to your computer and use it in GitHub Desktop.
Save hayd/3721517 to your computer and use it in GitHub Desktop.
Primes generator function
class primes():
def __init__(self):
self.primes_list = [2,3]
self.generator = self.generator()
def _append_if_no_prime_divides(self,q):
is_prime = not any( q % p == 0 for p in self.primes_list)
if is_prime:
self.primes_list.append(q)
return is_prime
def generator(self):
yield 2; yield 3
m = 1
while True:
q1 = 6*m - 1
q2 = 6*m + 1
if self._append_if_no_prime_divides(q1):
yield q1
if self._append_if_no_prime_divides(q2):
yield q2
m+=1
@hayd
Copy link
Author

hayd commented Sep 14, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment