Skip to content

Commit 4f52ef5

Browse files
test(maths): add doctests and pytest for Sieve of Atkin
1 parent c25cfcc commit 4f52ef5

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

maths/sieve_of_atkin.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,37 @@
22

33

44
def sieve_of_atkin(limit: int) -> List[int]:
5+
"""
6+
Compute all prime numbers up to the given limit using the Sieve of Atkin.
7+
8+
Parameters
9+
----------
10+
limit : int
11+
Upper bound of primes to generate (inclusive).
12+
13+
Returns
14+
-------
15+
List[int]
16+
A list of prime numbers <= limit.
17+
18+
Raises
19+
------
20+
ValueError
21+
If limit is not an integer or is less than 2.
22+
23+
References
24+
----------
25+
https://en.wikipedia.org/wiki/Sieve_of_Atkin
26+
27+
Examples
28+
--------
29+
>>> sieve_of_atkin(10)
30+
[2, 3, 5, 7]
31+
>>> sieve_of_atkin(1)
32+
Traceback (most recent call last):
33+
...
34+
ValueError: limit must be an integer >= 2
35+
"""
536
if not isinstance(limit, int) or limit < 2:
637
raise ValueError("limit must be an integer >= 2")
738

maths/test_sieve_of_atkin.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# tests/test_sieve_of_atkin.py
2+
import pytest
3+
from maths.sieve_of_atkin import sieve_of_atkin
4+
5+
def test_small_primes():
6+
assert sieve_of_atkin(10) == [2, 3, 5, 7]
7+
8+
def test_invalid_limit():
9+
with pytest.raises(ValueError):
10+
sieve_of_atkin(1)

0 commit comments

Comments
 (0)