Created
July 27, 2017 23:40
-
-
Save HoangPV/c58a48f6fe156e4c1b13dbe0a325726a to your computer and use it in GitHub Desktop.
return backward read 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 | |
/** | |
* return backward read primes | |
* | |
* Find all Backwards Read Primes between two positive given numbers | |
* (both inclusive), the second one being greater than the first one. | |
* The resulting array or the resulting string will be ordered | |
* following the natural order of the prime numbers. | |
* | |
* @param int $start | |
* @param int $end | |
* @return array $primes | |
* @author Phan Vu Hoang <[email protected]> | |
*/ | |
function backwardsPrime($start, $end) { | |
$res = []; | |
for ($i=$start; $i <=$end ; $i++) { | |
if(isPrimeNum($i) && isBwpNum($i)) { | |
//$res[] = $i; | |
array_push($res,$i); | |
} | |
} | |
return $res; | |
} | |
/** | |
* check whether a number is prime | |
*/ | |
function isPrimeNum($number) { | |
for ($i=2; $i < $number ; $i++) { | |
if($number % $i == 0) | |
return false; | |
} | |
return true; | |
} | |
/** | |
* check whether a number is backward read price | |
* | |
* @return boolean | |
*/ | |
function isBwpNum($number) { | |
$number_nd = intval(strrev((string)$number)); | |
return isPrimeNum($number_nd); | |
} | |
$primes = backwardsPrime(7000, 7100); | |
echo '<pre>'; | |
print_r($primes); | |
echo '</pre>'; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment