in reply to Prime list
Let's look at your first wrong result: 16
$i starts at 14 and is compared against all already found prime numbers:
the foreach loop ends and 16 is considered prime. 16 is not compared with 2 and 3 and because of that it's divisibility by 2 is not determined.
The problem is you're unconditionally pushing to the @primes array. Whenever the $i % $p == 0 condition is met, processing of the current $i should end, since it's non-prime. Then the next number should be compared with ALL the currently found primes.
|
|---|