-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimes2.php
More file actions
28 lines (21 loc) · 607 Bytes
/
Copy pathPrimes2.php
File metadata and controls
28 lines (21 loc) · 607 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
declare(strict_types=1);
namespace drupol\primes;
use CallbackFilterIterator;
use Generator;
use Iterator;
final class Primes2 extends AbstractPrimes
{
protected static function sieve(Iterator $iterator): ?Generator
{
yield $primeNumber = $iterator->current();
$iterator = new CallbackFilterIterator(
$iterator,
static fn (int $a): bool => (($primeNumber ** 2) > $a) || (0 !== ($a % $primeNumber))
);
$iterator->next();
return $iterator->valid() ?
yield from self::sieve($iterator) :
null;
}
}