1+ from typing import List
2+
3+
4+ def sieve_of_atkin (limit : int ) -> List [int ]:
5+ if not isinstance (limit , int ) or limit < 2 :
6+ raise ValueError ("limit must be an integer >= 2" )
7+
8+ # Initialize the sieve array
9+ sieve = [False ] * (limit + 1 )
10+ results : List [int ] = []
11+
12+ # Preliminary marking based on quadratic forms
13+ from math import sqrt
14+
15+ sqrt_limit = int (sqrt (limit )) + 1
16+ for x in range (1 , sqrt_limit ):
17+ for y in range (1 , sqrt_limit ):
18+ n = 4 * x * x + y * y
19+ if n <= limit and n % 12 in (1 , 5 ):
20+ sieve [n ] = not sieve [n ]
21+ n = 3 * x * x + y * y
22+ if n <= limit and n % 12 == 7 :
23+ sieve [n ] = not sieve [n ]
24+ n = 3 * x * x - y * y
25+ if x > y and n <= limit and n % 12 == 11 :
26+ sieve [n ] = not sieve [n ]
27+
28+ # Mark all multiples of squares as non-prime
29+ for n in range (5 , sqrt_limit ):
30+ if sieve [n ]:
31+ step = n * n
32+ for k in range (step , limit + 1 , step ):
33+ sieve [k ] = False
34+
35+ # Compile the list of primes
36+ if limit >= 2 :
37+ results .extend ([2 , 3 ])
38+ results .extend ([i for i in range (5 , limit + 1 ) if sieve [i ]])
39+ return results
40+
41+
42+ if __name__ == "__main__" :
43+ import doctest
44+ doctest .testmod ()
45+ print ("All doctests passed!" )
0 commit comments