From 22d6f5ccc0612ea749eb2acc539e44be4051e586 Mon Sep 17 00:00:00 2001 From: sumit Nayak Date: Thu, 2 Oct 2025 19:13:15 +0530 Subject: [PATCH 1/2] Faster & Optimized sieve_of_eratosthenes.py --- maths/sieve_of_eratosthenes.py | 36 +++++++++++++++------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/maths/sieve_of_eratosthenes.py b/maths/sieve_of_eratosthenes.py index 3923dc3e1612..a50d2d9813ee 100644 --- a/maths/sieve_of_eratosthenes.py +++ b/maths/sieve_of_eratosthenes.py @@ -1,5 +1,5 @@ """ -Sieve of Eratosthones +Sieve of Eratosthenes The sieve of Eratosthenes is an algorithm used to find prime numbers, less than or equal to a given value. @@ -9,10 +9,10 @@ doctest provider: Bruno Simas Hadlich (https://github.com/brunohadlich) Also thanks to Dmitry (https://github.com/LizardWizzard) for finding the problem +optimized by : Sumit Nayak (https://github.com/Sumit210106/) """ from __future__ import annotations - import math @@ -38,29 +38,25 @@ def prime_sieve(num: int) -> list[int]: msg = f"{num}: Invalid input, please enter a positive integer." raise ValueError(msg) - sieve = [True] * (num + 1) - prime = [] - start = 2 - end = int(math.sqrt(num)) - - while start <= end: - # If start is a prime - if sieve[start] is True: - prime.append(start) + if num < 2: + return [] - # Set multiples of start be False - for i in range(start * start, num + 1, start): - if sieve[i] is True: - sieve[i] = False + sieve = [True] * (num + 1) + prime = [2] - start += 1 + # marked all even numbers as non-prime + for i in range(3, int(math.sqrt(num)) + 1, 2): + if sieve[i]: + for j in range(i * i, num + 1, 2 * i): + sieve[j] = False - for j in range(end + 1, num + 1): - if sieve[j] is True: - prime.append(j) + # collect odd primes + for k in range(3, num + 1, 2): + if sieve[k]: + prime.append(k) return prime if __name__ == "__main__": - print(prime_sieve(int(input("Enter a positive integer: ").strip()))) + print(prime_sieve(int(input("Enter a positive integer: ").strip()))) \ No newline at end of file From 77873f384897829b7ee7173fae47f8c7b213cb17 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 2 Oct 2025 13:47:14 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/sieve_of_eratosthenes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/sieve_of_eratosthenes.py b/maths/sieve_of_eratosthenes.py index a50d2d9813ee..f935c8d943b2 100644 --- a/maths/sieve_of_eratosthenes.py +++ b/maths/sieve_of_eratosthenes.py @@ -46,7 +46,7 @@ def prime_sieve(num: int) -> list[int]: # marked all even numbers as non-prime for i in range(3, int(math.sqrt(num)) + 1, 2): - if sieve[i]: + if sieve[i]: for j in range(i * i, num + 1, 2 * i): sieve[j] = False @@ -59,4 +59,4 @@ def prime_sieve(num: int) -> list[int]: if __name__ == "__main__": - print(prime_sieve(int(input("Enter a positive integer: ").strip()))) \ No newline at end of file + print(prime_sieve(int(input("Enter a positive integer: ").strip())))