-
-
Save alexanderpetkov/21c0f4a2361eed84d25f0109ef54fd5c to your computer and use it in GitHub Desktop.
Examply Python Code Snippet
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
import itertools | |
def iter_primes(): | |
# an iterator of all numbers between 2 and +infinity | |
numbers = itertools.count(2) | |
# generate primes forever | |
while True: | |
# get the first number from the iterator (always a prime) | |
prime = numbers.next() | |
yield prime | |
# this code iteratively builds up a chain of | |
# filters...slightly tricky, but ponder it a bit | |
numbers = itertools.ifilter(prime.__rmod__, numbers) | |
for p in iter_primes(): | |
if p > 1000: | |
break | |
print p |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment