Skip to content

Instantly share code, notes, and snippets.

@HoangPV
Created November 3, 2017 10:55
Show Gist options
  • Save HoangPV/3744bd48712361e92474d6e4ae5d0761 to your computer and use it in GitHub Desktop.
Save HoangPV/3744bd48712361e92474d6e4ae5d0761 to your computer and use it in GitHub Desktop.
<?php
/*
* @see https://www.codewars.com/kata/gap-in-primes/train/php
*/
function gap( $g, $m, $n )
{
$prime = FALSE;
for ( $i = $m; $i < $n; $i ++ ) {
$isPrime = isprime($i);
if ( $isPrime && $prime && $i - $prime == $g ) {
return [ $prime, $i ];
}
if ( $isPrime ) {
$prime = $i;
}
}
return NULL;
}
function isprime( $n )
{
for ( $i = 2; $i <= sqrt($n); $i ++ ) {
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