Created
November 11, 2019 14:40
-
-
Save vlad-bezden/3fffdb30729e269473ea5fafab430736 to your computer and use it in GitHub Desktop.
Prime numbers generator. Creates infinite list of prime numbers.
This file contains hidden or 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
from math import sqrt | |
from itertools import count | |
NUMBER_OF_PRIMES = 10 | |
def primes() -> int: | |
"""Primes generator.""" | |
yield 2 | |
for x in count(3, step=2): | |
if not any(x % y == 0 for y in range(3, int(sqrt(x) + 1), 2)): | |
yield x | |
for i, p in zip(range(NUMBER_OF_PRIMES), primes()): | |
print(f"{i}: {p:>2}") | |
# output | |
# 0: 2 | |
# 1: 3 | |
# 2: 5 | |
# 3: 7 | |
# 4: 11 | |
# 5: 13 | |
# 6: 17 | |
# 7: 19 | |
# 8: 23 | |
# 9: 29 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment