Skip to content
61 changes: 61 additions & 0 deletions maths/optimised_sieve_of_eratosthenes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Optimized Sieve of Eratosthenes: An efficient algorithm to compute all prime numbers up to n.

Check failure on line 1 in maths/optimised_sieve_of_eratosthenes.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/optimised_sieve_of_eratosthenes.py:1:89: E501 Line too long (95 > 88)
# This version skips even numbers after 2, improving both memory and time usage.
# It is particularly efficient for larger n (e.g., up to 10**8 on typical hardware).
# Wikipedia URL - https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

from math import isqrt

Check failure on line 6 in maths/optimised_sieve_of_eratosthenes.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

maths/optimised_sieve_of_eratosthenes.py:6:1: I001 Import block is un-sorted or un-formatted

def optimized_sieve(n: int) -> list[int]:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

"""
Compute all prime numbers up to and including n using an optimized Sieve of Eratosthenes.

Check failure on line 10 in maths/optimised_sieve_of_eratosthenes.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/optimised_sieve_of_eratosthenes.py:10:89: E501 Line too long (93 > 88)

This implementation skips even numbers after 2 to reduce memory and runtime by about 50%.

Check failure on line 12 in maths/optimised_sieve_of_eratosthenes.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/optimised_sieve_of_eratosthenes.py:12:89: E501 Line too long (93 > 88)

Parameters
----------
n : int
Upper bound (inclusive) of the range in which to find prime numbers.
Expected to be a non-negative integer. If n < 2 the function returns an empty list.

Check failure on line 18 in maths/optimised_sieve_of_eratosthenes.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/optimised_sieve_of_eratosthenes.py:18:89: E501 Line too long (91 > 88)

Returns
-------
list[int]
A list of primes in ascending order that are <= n.

Examples
--------
>>> optimized_sieve(10)
[2, 3, 5, 7]
>>> optimized_sieve(1)
[]
>>> optimized_sieve(2)
[2]
>>> optimized_sieve(30)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
"""
if n < 2:
return []

# Handle 2 separately, then consider only odd numbers
primes = [2] if n >= 2 else []

# Only odd numbers from 3 to n
size = (n - 1) // 2
is_prime = [True] * (size + 1)
limit = isqrt(n)

for i in range((limit - 1) // 2 + 1):
if is_prime[i]:
p = 2 * i + 3
# Start marking from p^2, converted to index
start = (p * p - 3) // 2
for j in range(start, size + 1, p):
is_prime[j] = False

primes.extend(2 * i + 3 for i in range(size + 1) if is_prime[i])
return primes


if __name__ == "__main__":

print(optimized_sieve(50))
Loading