Skip to content

Commit ff0b3a4

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 46432d4 commit ff0b3a4

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

maths/sieve_of_sundaram.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313
def sieve_of_sundaram(limit: int) -> List[int]:
1414
"""
1515
Generate all prime numbers up to the given limit using Sieve of Sundaram.
16-
16+
1717
The algorithm works by creating a list of integers and marking composite numbers,
1818
then extracting the remaining unmarked numbers which represent primes.
19-
19+
2020
Args:
2121
limit: Upper bound (exclusive) for finding primes
22-
22+
2323
Returns:
2424
List of all prime numbers less than the limit
25-
25+
2626
Raises:
2727
ValueError: If limit is negative
28-
28+
2929
Examples:
3030
>>> sieve_of_sundaram(30)
3131
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
@@ -46,42 +46,42 @@ def sieve_of_sundaram(limit: int) -> List[int]:
4646
"""
4747
if limit < 0:
4848
raise ValueError("limit must be non-negative")
49-
49+
5050
if limit <= 2:
5151
return []
52-
52+
5353
# Calculate the range for Sundaram sieve
5454
# We need to find primes up to 'limit', so we work with (limit-1)//2
5555
n = (limit - 1) // 2
56-
56+
5757
# Create a boolean array and initialize all entries as not marked (False)
5858
# marked[i] represents whether (2*i + 1) is composite
5959
marked = [False] * (n + 1)
60-
60+
6161
# Mark numbers using Sundaram's formula: i + j + 2*i*j
6262
# where i <= j and i + j + 2*i*j <= n
6363
for i in range(1, n + 1):
6464
j = i
6565
while i + j + 2 * i * j <= n:
6666
marked[i + j + 2 * i * j] = True
6767
j += 1
68-
68+
6969
# Collect unmarked numbers and transform them to get primes
7070
primes = [2] # 2 is the only even prime
71-
71+
7272
for i in range(1, n + 1):
7373
if not marked[i]:
7474
primes.append(2 * i + 1)
75-
75+
7676
return primes
7777

7878

7979
if __name__ == "__main__":
8080
print("Sieve of Sundaram Demo")
8181
print("-" * 20)
82-
82+
8383
test_limits = [10, 30, 50, 100]
84-
84+
8585
for limit in test_limits:
8686
primes = sieve_of_sundaram(limit)
8787
print(f"Primes up to {limit}: {primes}")

0 commit comments

Comments
 (0)