From 716beb32ed231217c05782be9323ef43f6c1a0ce Mon Sep 17 00:00:00 2001 From: Leoriem-code <73761711+Leoriem-code@users.noreply.github.com> Date: Tue, 26 Oct 2021 09:21:44 +0200 Subject: [PATCH] Improved prime_numbers.py (#5592) * Improved prime_numbers.py * update prime_numbers.py * Increase the timeit number to 1_000_000 Co-authored-by: Christian Clauss --- maths/prime_numbers.py | 55 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/maths/prime_numbers.py b/maths/prime_numbers.py index 38bebddee..183fbd393 100644 --- a/maths/prime_numbers.py +++ b/maths/prime_numbers.py @@ -58,6 +58,38 @@ def primes(max: int) -> Generator[int, None, None]: yield i +def fast_primes(max: int) -> Generator[int, None, None]: + """ + 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 + """ + numbers: Generator = (i for i in range(1, (max + 1), 2)) + # It's useless to test even numbers as they will not be prime + if max > 2: + 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 + + if __name__ == "__main__": number = int(input("Calculate primes up to:\n>> ").strip()) for ret in primes(number): @@ -66,5 +98,24 @@ if __name__ == "__main__": # 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")) + 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, + ) + )