Skip to content

Add sieve of atkin #12818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(web_programming): return '- ' when no <fin-streamer> tag found
  • Loading branch information
idrisibrahimerten committed Jul 4, 2025
commit 7958c446783da1de9dfd0df23781997714d90036
18 changes: 8 additions & 10 deletions maths/sieve_of_atkin.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
from typing import List
import math


def sieve_of_atkin(limit: int) -> List[int]:
def sieve_of_atkin(limit: int) -> list[int]:
"""
Compute all prime numbers up to the given limit using the Sieve of Atkin.

Parameterss
Parameters
----------
limit : int
Upper bound of primes to generate (inclusive).

Returns
-------
List[int]
list[int]
A list of prime numbers <= limit.

Raises
------
ValueError
If limit is not an integer or is less than 2.
If limit is not an integer >= 2.

References
----------
Expand All @@ -30,20 +30,18 @@ def sieve_of_atkin(limit: int) -> List[int]:
[2, 3, 5, 7]
>>> sieve_of_atkin(1)
Traceback (most recent call last):
...
...
ValueError: limit must be an integer >= 2
"""
if not isinstance(limit, int) or limit < 2:
raise ValueError("limit must be an integer >= 2")

# Initialize the sieve array
sieve = [False] * (limit + 1)
results: List[int] = []
results: list[int] = []

# Preliminary marking based on quadratic forms
from math import sqrt

sqrt_limit = int(sqrt(limit)) + 1
sqrt_limit = int(math.sqrt(limit)) + 1
for x in range(1, sqrt_limit):
for y in range(1, sqrt_limit):
n = 4 * x * x + y * y
Expand Down
6 changes: 5 additions & 1 deletion maths/test_sieve_of_atkin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# maths/test_sieve_of_atkin.py
import pytest

from maths.sieve_of_atkin import sieve_of_atkin


def test_small_primes():
assert sieve_of_atkin(10) == [2, 3, 5, 7]


def test_medium_primes():
assert sieve_of_atkin(20) == [2, 3, 5, 7, 11, 13, 17, 19]


def test_invalid_limit():
with pytest.raises(ValueError):
sieve_of_atkin(1)
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy