Skip to content

Instantly share code, notes, and snippets.

@HoangPV
Created July 27, 2017 23:40
Show Gist options
  • Save HoangPV/c58a48f6fe156e4c1b13dbe0a325726a to your computer and use it in GitHub Desktop.
Save HoangPV/c58a48f6fe156e4c1b13dbe0a325726a to your computer and use it in GitHub Desktop.
return backward read primes
<?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 <vu.hoang-phan@ekino.com>
*/
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