Created
September 21, 2017 02:59
-
-
Save HoangPV/307bed1721851cc6cc638788bf8b1163 to your computer and use it in GitHub Desktop.
Steps in Primes
This file contains hidden or 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 step($g, $m, $n) { | |
$pairs = $primes = []; | |
if ($g >= 2 && $m >=2 && n >= 2) { | |
for ($i=$m; $i <=$n ; $i++) { | |
if (isPrime($i)) { | |
$primes[] = $i; | |
} | |
} | |
for ($i = 0; $i < count($primes) - 1; $i++) { | |
for ($j = $i+1; $j <= count($primes); $j++) { | |
if ( $primes[$j] - $primes[$i] == $g) { | |
$pairs[] = [$primes[$i],$primes[$j]]; | |
} | |
} | |
} | |
} | |
return (!empty($pairs)) ? $pairs[0] : []; | |
} | |
function isPrime( $n ) { | |
if($n < 2 || $n%2==0) return false; | |
if($n == 2) return true; | |
for ($i=3; $i <$n-1 ; $i+=2) { | |
if($n%$i==0) return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment