Skip to content

Instantly share code, notes, and snippets.

@HoangPV
Created September 21, 2017 02:59
Show Gist options
  • Save HoangPV/307bed1721851cc6cc638788bf8b1163 to your computer and use it in GitHub Desktop.
Save HoangPV/307bed1721851cc6cc638788bf8b1163 to your computer and use it in GitHub Desktop.
Steps in Primes
<?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