Coverage for pycommons / math / primes.py: 100%

33 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-11 03:04 +0000

1"""Tools for working with prime numbers.""" 

2 

3from collections import deque 

4from typing import Final, Generator 

5 

6from pycommons.types import type_error 

7 

8 

9def primes(maximum: int = 2 ** 32) -> Generator[int, None, None]: 

10 """ 

11 Provide a sequence of prime numbers. 

12 

13 This function is a generator that returns the prime numbers in their 

14 natural order starting at `2`. It will return numbers at most up to 

15 the given `maximum` value. For this purpose, it iteratively builds 

16 something like the Sieve of Eratosthenes. 

17 

18 :param maximum: the maximum number to consider 

19 :returns: the prime numbers 

20 

21 >>> list(primes(-1)) 

22 [] 

23 

24 >>> list(primes(0)) 

25 [] 

26 

27 >>> list(primes(1)) 

28 [] 

29 

30 >>> list(primes(2)) 

31 [2] 

32 

33 >>> list(primes(3)) 

34 [2, 3] 

35 

36 >>> list(primes(4)) 

37 [2, 3] 

38 

39 >>> list(primes(5)) 

40 [2, 3, 5] 

41 

42 >>> list(primes(6)) 

43 [2, 3, 5] 

44 

45 >>> list(primes(7)) 

46 [2, 3, 5, 7] 

47 

48 >>> list(primes(8)) 

49 [2, 3, 5, 7] 

50 

51 >>> list(primes(9)) 

52 [2, 3, 5, 7] 

53 

54 >>> list(primes(10)) 

55 [2, 3, 5, 7] 

56 

57 >>> list(primes(11)) 

58 [2, 3, 5, 7, 11] 

59 

60 >>> list(primes(12)) 

61 [2, 3, 5, 7, 11] 

62 

63 >>> list(primes(13)) 

64 [2, 3, 5, 7, 11, 13] 

65 

66 >>> list(primes(14)) 

67 [2, 3, 5, 7, 11, 13] 

68 

69 >>> list(primes(15)) 

70 [2, 3, 5, 7, 11, 13] 

71 

72 >>> list(primes(16)) 

73 [2, 3, 5, 7, 11, 13] 

74 

75 >>> list(primes(17)) 

76 [2, 3, 5, 7, 11, 13, 17] 

77 

78 >>> list(primes(18)) 

79 [2, 3, 5, 7, 11, 13, 17] 

80 

81 >>> list(primes(19)) 

82 [2, 3, 5, 7, 11, 13, 17, 19] 

83 

84 >>> list(primes(20)) 

85 [2, 3, 5, 7, 11, 13, 17, 19] 

86 

87 >>> list(primes(199)) 

88 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, \ 

8971, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, \ 

90151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199] 

91 

92 >>> list(primes(200)) 

93 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, \ 

9471, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, \ 

95151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199] 

96 

97 >>> list(primes(201)) 

98 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, \ 

9971, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, \ 

100151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199] 

101 

102 >>> list(primes(1000)) 

103 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, \ 

10471, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, \ 

105151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, \ 

106233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, \ 

107317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, \ 

108419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, \ 

109503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, \ 

110607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, \ 

111701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, \ 

112811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, \ 

113911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997] 

114 

115 >>> try: 

116 ... for t in primes(1.0): 

117 ... pass 

118 ... except TypeError as te: 

119 ... print(te) 

120 maximum should be an instance of int but is float, namely 1.0. 

121 """ 

122 if not isinstance(maximum, int): 

123 raise type_error(maximum, "maximum", int) 

124 if maximum <= 1: 

125 return 

126 

127 yield 2 

128 if maximum <= 2: 

129 return 

130 

131 yield 3 

132 if maximum <= 3: 

133 return 

134 

135 current: int = 5 

136 check_primes: Final[list[int]] = [] # the numbers <= sqrt(current) 

137 next_primes: Final[deque[int]] = deque([3]) # larger primes 

138 check_limit: int = 0 # the square of the largest number in check_primes 

139 while current <= maximum: 

140 is_prime: bool = True 

141 

142 # check all odd primes <= sqrt(current) 

143 for check in check_primes: 

144 if (current % check) == 0: 

145 is_prime = False 

146 break 

147 

148 # ...well, there might be one more to check, maybe we need to step the 

149 # sqrt up by one? 

150 if check_limit <= current: 

151 check = next_primes.popleft() 

152 check_limit = check * check 

153 is_prime &= (current % check) != 0 

154 check_primes.append(check) 

155 

156 if is_prime: 

157 yield current 

158 next_primes.append(current) 

159 current += 2