Skip to content

Instantly share code, notes, and snippets.

@r3
Created June 25, 2013 05:14
Show Gist options
  • Save r3/5856093 to your computer and use it in GitHub Desktop.
Save r3/5856093 to your computer and use it in GitHub Desktop.
Teaching generators
from math import sqrt
def count(start, step):
"""Happens to be included in the standard library"""
number = int(start)
while True:
yield number
start += number
def primes(start=2):
def is_prime(number):
if number == 1:
return False
if number == 2:
return True
if number % 2 == 0:
return False
maximum = int(sqrt(number)) + 1
for divisor in range(3, maximum, 2):
if number % divisor == 0:
return False
return True
for potential_prime in count(start):
if is_prime(potential_prime):
yield potential_prime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment