Skip to content

Instantly share code, notes, and snippets.

@cowlicks
Last active December 25, 2015 02:29
Show Gist options
  • Save cowlicks/6902649 to your computer and use it in GitHub Desktop.
Save cowlicks/6902649 to your computer and use it in GitHub Desktop.
Simple python prime generator
def next_prime():
"""Prime generator.
>>> import primes
>>> a = primes.next_prime()
>>> a.next()
2
>>> a.next()
3
"""
def is_prime(n):
for i in range(2, n):
if n % i == 0:
return False
return True
i = 2
while True:
if is_prime(i):
yield i
i += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment