Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
next_primes in php
<?php
function isPrime($number) {
$n = abs($number);
$i = 2;
while ($i <= sqrt($n)) {
if ($n % $i === 0) {
return false;
}
$i++;
}
return true;
}
/**
* Get next primes
*
* @param int $from - Number to start from
* @param int $max - Max number to reach
* @param int $count - Primes to return
*
*/
function next_primes($from, $max = 0, $count = 10) {
$sieve = [];
$number = $from;
while (count($sieve) <= $count) {
if (isPrime($number) === true) {
if ($max > 0 && $number >= $max) {
break;
}
$sieve[] = $number;
}
$number++;
}
return $sieve;
}
print_r(next_primes(1, 0, 1000));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.