mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-31 14:43:43 +00:00
improved prime number generator to check only up to sqrt(n) instead of n (#1984)
* improved prime number generator to check only up to sqrt(n) instead of n * added old version as slow_primes() and named new, faster version primes() * fixed docstring in slow_primes * Add a timeit benchmark * Update prime_numbers.py Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
48bb14d4b2
commit
c8fbdee229
|
@ -1,4 +1,32 @@
|
||||||
from typing import Generator
|
from typing import Generator
|
||||||
|
import math
|
||||||
|
|
||||||
|
|
||||||
|
def slow_primes(max: int) -> Generator[int, None, None]:
|
||||||
|
"""
|
||||||
|
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
|
||||||
|
"""
|
||||||
|
numbers: Generator = (i for i in range(1, (max + 1)))
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
def primes(max: int) -> Generator[int, None, None]:
|
def primes(max: int) -> Generator[int, None, None]:
|
||||||
|
@ -21,7 +49,9 @@ def primes(max: int) -> Generator[int, None, None]:
|
||||||
"""
|
"""
|
||||||
numbers: Generator = (i for i in range(1, (max + 1)))
|
numbers: Generator = (i for i in range(1, (max + 1)))
|
||||||
for i in (n for n in numbers if n > 1):
|
for i in (n for n in numbers if n > 1):
|
||||||
for j in range(2, i):
|
# only need to check for factors up to sqrt(i)
|
||||||
|
bound = int(math.sqrt(i)) + 1
|
||||||
|
for j in range(2, bound):
|
||||||
if (i % j) == 0:
|
if (i % j) == 0:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
@ -32,3 +62,8 @@ if __name__ == "__main__":
|
||||||
number = int(input("Calculate primes up to:\n>> ").strip())
|
number = int(input("Calculate primes up to:\n>> ").strip())
|
||||||
for ret in primes(number):
|
for ret in primes(number):
|
||||||
print(ret)
|
print(ret)
|
||||||
|
|
||||||
|
# Let's benchmark them side-by-side...
|
||||||
|
from timeit import timeit
|
||||||
|
print(timeit("slow_primes(1_000_000)", setup="from __main__ import slow_primes"))
|
||||||
|
print(timeit("primes(1_000_000)", setup="from __main__ import primes"))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user