Last active
August 29, 2015 14:04
-
-
Save rohit-jamuar/9a6bcd5f7bc550a753e4 to your computer and use it in GitHub Desktop.
Prime number generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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