1 | <?php |
||
12 | class PrimeSieveOfAtkin extends Combinatorics implements \Iterator, \Countable { |
||
13 | |||
14 | /** |
||
15 | * The minimum limit. |
||
16 | * |
||
17 | * @var int |
||
18 | */ |
||
19 | protected $min; |
||
20 | |||
21 | /** |
||
22 | * The maximum limit. |
||
23 | * |
||
24 | * @var int |
||
25 | */ |
||
26 | protected $max; |
||
27 | |||
28 | /** |
||
29 | * The key. |
||
30 | * |
||
31 | * @var int |
||
32 | */ |
||
33 | protected $key; |
||
34 | |||
35 | /** |
||
36 | * The primes array. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $primesData; |
||
41 | |||
42 | /** |
||
43 | * The primes array. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $primes; |
||
48 | |||
49 | /** |
||
50 | * The primes array converted. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $data; |
||
55 | |||
56 | /** |
||
57 | * @var int |
||
58 | */ |
||
59 | private $last; |
||
|
|||
60 | |||
61 | private $count; |
||
62 | |||
63 | /** |
||
64 | * PrimeSieveOfAtkin constructor. |
||
65 | */ |
||
66 | public function __construct() { |
||
72 | |||
73 | private function generateNextPrime() { |
||
83 | |||
84 | /** |
||
85 | * @param $index |
||
86 | */ |
||
87 | private function toggle(&$primes, $index) { |
||
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | public function current() { |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | public function valid() { |
||
111 | |||
112 | public function findPrimes($limit) { |
||
113 | $sqrt = sqrt($limit); |
||
114 | $isPrime = array(); |
||
115 | for ($i = 1; $i <= $sqrt; $i++) { |
||
116 | for ($j = 1; $j <= $sqrt; $j++) { |
||
117 | $n = 4 * pow($i, 2) + pow($j, 2); |
||
118 | if ($n <= $limit && ($n % 12 == 1 || $n % 12 == 5)) { |
||
119 | $this->toggle($isPrime, $n); |
||
120 | } |
||
121 | $n = 3 * pow($i, 2) + pow($j, 2); |
||
122 | if ($n <= $limit && $n % 12 == 7) { |
||
123 | $this->toggle($isPrime, $n); |
||
124 | } |
||
125 | $n = 3 * pow($i, 2) - pow($j, 2); |
||
126 | if ($i > $j && $n <= $limit && $n % 12 == 11) { |
||
127 | $this->toggle($isPrime, $n); |
||
128 | } |
||
129 | } |
||
130 | } |
||
131 | |||
132 | for ($n = 5; $n <= $sqrt; $n++) { |
||
133 | if (isset($isPrime[$n])) { |
||
134 | $s = pow($n, 2); |
||
135 | |||
136 | for ($k = $s; $k <= $limit; $k += $s) { |
||
137 | $isPrime[$k] = FALSE; |
||
138 | } |
||
139 | } |
||
140 | } |
||
141 | |||
142 | $primes[2] = 2; |
||
143 | $primes[3] = 3; |
||
144 | |||
145 | for ($i = 0; $i < $limit; $i++) { |
||
146 | if (isset($isPrime[$i]) && $isPrime[$i]) { |
||
147 | $primes[$i] = $i; |
||
148 | } |
||
149 | } |
||
150 | |||
151 | return $primes; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * {@inheritdoc} |
||
156 | */ |
||
157 | public function rewind() { |
||
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | public function key() { |
||
167 | |||
168 | /** |
||
169 | * {@inheritdoc} |
||
170 | */ |
||
171 | public function next() { |
||
174 | |||
175 | /** |
||
176 | * Count elements of an object. |
||
177 | * |
||
178 | * @return int |
||
179 | * The number of element. |
||
180 | */ |
||
181 | public function count() { |
||
184 | |||
185 | /** |
||
186 | * Convert the iterator into an array. |
||
187 | * |
||
188 | * @return array |
||
189 | * The elements. |
||
190 | */ |
||
191 | public function toArray() { |
||
200 | |||
201 | /** |
||
202 | * Set the maximum limit. |
||
203 | * |
||
204 | * @param int $max |
||
205 | * The limit. |
||
206 | */ |
||
207 | public function setMaxLimit($max) { |
||
211 | |||
212 | /** |
||
213 | * Get the maximum limit. |
||
214 | * |
||
215 | * @return int |
||
216 | * The limit. |
||
217 | */ |
||
218 | public function getMaxLimit() { |
||
221 | |||
222 | /** |
||
223 | * Set the minimum limit. |
||
224 | * |
||
225 | * @param int $min |
||
226 | * The limit. |
||
227 | */ |
||
228 | public function setMinLimit($min) { |
||
231 | |||
232 | /** |
||
233 | * Get the minimum limit. |
||
234 | * |
||
235 | * @return int |
||
236 | * The limit. |
||
237 | */ |
||
238 | public function getMinLimit() { |
||
241 | |||
242 | /** |
||
243 | * Get a rough estimation of how many prime numbers there are. |
||
244 | * |
||
245 | * @return float |
||
246 | * The number of primes. |
||
247 | */ |
||
248 | public function pi() { |
||
251 | |||
252 | } |
||
253 |
This check marks private properties in classes that are never used. Those properties can be removed.