Skip to content

Instantly share code, notes, and snippets.

@uchida
Created February 1, 2012 13:44
Show Gist options
  • Select an option

  • Save uchida/1717000 to your computer and use it in GitHub Desktop.

Select an option

Save uchida/1717000 to your computer and use it in GitHub Desktop.
naive prime numbers generator
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from itertools import count, takewhile, islice
def primes():
yield 2
primes = [2]
for i in count(3, step=2):
if all(i % prime != 0 for prime in primes):
yield i
primes.append(i)
# prime numbers less than 30
print list(takewhile(lambda x: x<30, primes()))
# 10 prime numbers
print list(islice(primes(), 10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment