Skip to content

Instantly share code, notes, and snippets.

@lcherone
Created August 15, 2018 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lcherone/8bd1e33dd90c5cc3d7d044f1a3660ad6 to your computer and use it in GitHub Desktop.
Save lcherone/8bd1e33dd90c5cc3d7d044f1a3660ad6 to your computer and use it in GitHub Desktop.
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