Skip to content

Commit c9baa24

Browse files
committed
Update optimised_sieve_of_eratosthenes.py
1 parent 40608c9 commit c9baa24

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

maths/optimised_sieve_of_eratosthenes.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1-
# Optimized Sieve of Eratosthenes: An efficient algorithm to compute all prime numbers up to limit.
2-
# This version skips even numbers after 2, improving both memory and time usage.
3-
# It is particularly efficient for larger limits (e.g., up to 10**8 on typical hardware).
1+
# Optimized Sieve of Eratosthenes: An efficient algorithm to compute all prime numbers
2+
# up to limit. This version skips even numbers after 2, improving both memory and time
3+
# usage. It is particularly efficient for larger limits (e.g., up to 10**8 on typical
4+
# hardware).
45
# Wikipedia URL - https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
56

67
from math import isqrt
78

9+
810
def optimized_sieve(limit: int) -> list[int]:
911
"""
10-
Compute all prime numbers up to and including `limit` using an optimized Sieve of Eratosthenes.
12+
Compute all prime numbers up to and including `limit` using an optimized
13+
Sieve of Eratosthenes.
1114
12-
This implementation skips even numbers after 2 to reduce memory and runtime by about 50%.
15+
This implementation skips even numbers after 2 to reduce memory and
16+
runtime by about 50%.
1317
1418
Parameters
1519
----------
1620
limit : int
1721
Upper bound (inclusive) of the range in which to find prime numbers.
18-
Expected to be a non-negative integer. If limit < 2 the function returns an empty list.
22+
Expected to be a non-negative integer. If limit < 2 the function
23+
returns an empty list.
1924
2025
Returns
2126
-------

0 commit comments

Comments
 (0)