Created
August 15, 2018 02:30
-
-
Save lcherone/8bd1e33dd90c5cc3d7d044f1a3660ad6 to your computer and use it in GitHub Desktop.
next_primes in php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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