Created
November 3, 2017 10:55
-
-
Save HoangPV/3744bd48712361e92474d6e4ae5d0761 to your computer and use it in GitHub Desktop.
Gap in Primes
https://www.codewars.com/kata/gap-in-primes/train/php
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 | |
/* | |
* @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