2020-05-14 22:33:50 +00:00
|
|
|
import math
|
2022-07-11 08:19:52 +00:00
|
|
|
from collections.abc import Generator
|
2020-05-14 22:33:50 +00:00
|
|
|
|
|
|
|
|
2022-10-13 14:23:59 +00:00
|
|
|
def slow_primes(max_n: int) -> Generator[int, None, None]:
|
2020-05-14 22:33:50 +00:00
|
|
|
"""
|
|
|
|
Return a list of all primes numbers up to max.
|
|
|
|
>>> list(slow_primes(0))
|
|
|
|
[]
|
|
|
|
>>> list(slow_primes(-1))
|
|
|
|
[]
|
|
|
|
>>> list(slow_primes(-10))
|
|
|
|
[]
|
|
|
|
>>> list(slow_primes(25))
|
|
|
|
[2, 3, 5, 7, 11, 13, 17, 19, 23]
|
|
|
|
>>> list(slow_primes(11))
|
|
|
|
[2, 3, 5, 7, 11]
|
|
|
|
>>> list(slow_primes(33))
|
|
|
|
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
|
|
|
|
>>> list(slow_primes(10000))[-1]
|
|
|
|
9973
|
|
|
|
"""
|
2022-10-13 14:23:59 +00:00
|
|
|
numbers: Generator = (i for i in range(1, (max_n + 1)))
|
2020-05-14 22:33:50 +00:00
|
|
|
for i in (n for n in numbers if n > 1):
|
|
|
|
for j in range(2, i):
|
|
|
|
if (i % j) == 0:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
yield i
|
2019-10-29 20:28:51 +00:00
|
|
|
|
|
|
|
|
2022-10-13 14:23:59 +00:00
|
|
|
def primes(max_n: int) -> Generator[int, None, None]:
|
2019-10-29 20:28:51 +00:00
|
|
|
"""
|
2019-10-29 22:54:31 +00:00
|
|
|
Return a list of all primes numbers up to max.
|
2019-12-07 05:39:08 +00:00
|
|
|
>>> list(primes(0))
|
|
|
|
[]
|
|
|
|
>>> list(primes(-1))
|
|
|
|
[]
|
|
|
|
>>> list(primes(-10))
|
|
|
|
[]
|
|
|
|
>>> list(primes(25))
|
2019-10-29 20:28:51 +00:00
|
|
|
[2, 3, 5, 7, 11, 13, 17, 19, 23]
|
2019-12-07 05:39:08 +00:00
|
|
|
>>> list(primes(11))
|
|
|
|
[2, 3, 5, 7, 11]
|
|
|
|
>>> list(primes(33))
|
|
|
|
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
|
|
|
|
>>> list(primes(10000))[-1]
|
|
|
|
9973
|
2019-10-29 20:28:51 +00:00
|
|
|
"""
|
2022-10-13 14:23:59 +00:00
|
|
|
numbers: Generator = (i for i in range(1, (max_n + 1)))
|
2019-12-07 05:39:08 +00:00
|
|
|
for i in (n for n in numbers if n > 1):
|
2020-05-14 22:33:50 +00:00
|
|
|
# only need to check for factors up to sqrt(i)
|
|
|
|
bound = int(math.sqrt(i)) + 1
|
|
|
|
for j in range(2, bound):
|
2019-12-07 05:39:08 +00:00
|
|
|
if (i % j) == 0:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
yield i
|
2019-10-29 20:28:51 +00:00
|
|
|
|
|
|
|
|
2022-10-13 14:23:59 +00:00
|
|
|
def fast_primes(max_n: int) -> Generator[int, None, None]:
|
2021-10-26 07:21:44 +00:00
|
|
|
"""
|
|
|
|
Return a list of all primes numbers up to max.
|
|
|
|
>>> list(fast_primes(0))
|
|
|
|
[]
|
|
|
|
>>> list(fast_primes(-1))
|
|
|
|
[]
|
|
|
|
>>> list(fast_primes(-10))
|
|
|
|
[]
|
|
|
|
>>> list(fast_primes(25))
|
|
|
|
[2, 3, 5, 7, 11, 13, 17, 19, 23]
|
|
|
|
>>> list(fast_primes(11))
|
|
|
|
[2, 3, 5, 7, 11]
|
|
|
|
>>> list(fast_primes(33))
|
|
|
|
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
|
|
|
|
>>> list(fast_primes(10000))[-1]
|
|
|
|
9973
|
|
|
|
"""
|
2022-10-13 14:23:59 +00:00
|
|
|
numbers: Generator = (i for i in range(1, (max_n + 1), 2))
|
2021-10-26 07:21:44 +00:00
|
|
|
# It's useless to test even numbers as they will not be prime
|
2022-10-13 14:23:59 +00:00
|
|
|
if max_n > 2:
|
2021-10-26 07:21:44 +00:00
|
|
|
yield 2 # Because 2 will not be tested, it's necessary to yield it now
|
|
|
|
for i in (n for n in numbers if n > 1):
|
|
|
|
bound = int(math.sqrt(i)) + 1
|
|
|
|
for j in range(3, bound, 2):
|
|
|
|
# As we removed the even numbers, we don't need them now
|
|
|
|
if (i % j) == 0:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
yield i
|
|
|
|
|
|
|
|
|
2019-10-29 20:28:51 +00:00
|
|
|
if __name__ == "__main__":
|
2019-12-07 05:39:08 +00:00
|
|
|
number = int(input("Calculate primes up to:\n>> ").strip())
|
|
|
|
for ret in primes(number):
|
|
|
|
print(ret)
|
2020-05-14 22:33:50 +00:00
|
|
|
|
|
|
|
# Let's benchmark them side-by-side...
|
|
|
|
from timeit import timeit
|
2020-05-16 06:49:56 +00:00
|
|
|
|
2021-10-26 07:21:44 +00:00
|
|
|
print(
|
|
|
|
timeit(
|
|
|
|
"slow_primes(1_000_000_000_000)",
|
|
|
|
setup="from __main__ import slow_primes",
|
|
|
|
number=1_000_000,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
timeit(
|
|
|
|
"primes(1_000_000_000_000)",
|
|
|
|
setup="from __main__ import primes",
|
|
|
|
number=1_000_000,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
timeit(
|
|
|
|
"fast_primes(1_000_000_000_000)",
|
|
|
|
setup="from __main__ import fast_primes",
|
|
|
|
number=1_000_000,
|
|
|
|
)
|
|
|
|
)
|