Skip to content

Instantly share code, notes, and snippets.

@rohit-jamuar
Last active August 29, 2015 14:04
Show Gist options
  • Save rohit-jamuar/9a6bcd5f7bc550a753e4 to your computer and use it in GitHub Desktop.
Save rohit-jamuar/9a6bcd5f7bc550a753e4 to your computer and use it in GitHub Desktop.
Prime number generator
#!/usr/bin/python
from math import ceil
def prime_number_generator():
'''
Prime number generator.
Create an object, keep calling next() to fetch next prime.
'''
yield 2
num = 3
while True:
is_not_prime = False
for number in range(2, int( ceil((num) ** 0.5) + 1)):
if num % number == 0:
is_not_prime = True
break
if not is_not_prime:
yield num
num += 2
if __name__ == '__main__':
p = prime_number_generator()
for i in range(100):
print p.next(),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment