// Function to check if a number is a palindrome in a given base
function isPalindrome(int $num, int $base): bool {
if ($base < 2) return false; // Invalid base
$strnumReversed = '';
$temp = $num;
// Convert number to string in given base
do {
$digit = $temp % $base;
$strnumReversed .= ($digit < 10) ? chr(ord('0') + $digit) : chr(ord('A') + ($digit - 10));
$temp = intdiv($temp, $base);
} while ($temp > 0);
// Check palindrome
return $strnumReversed === strrev($strnumReversed);
}
$limit = 1000;
echo "Numbers that are palindromes in both base 10 and base 2:\n";
for ($i = 1; $i <= $limit; $i++) {
if (isPalindrome($i, 10) && isPalindrome($i, 2)) {
echo "$i (binary: ";
// Print binary representation
$binary = '';
$temp = $i;
do {
$binary .= ($temp % 2) ? '1' : '0';
$temp = intdiv($temp, 2);
} while ($temp > 0);
$binary = strrev($binary);
echo "$binary)\n";
}
}
/*
run:
Numbers that are palindromes in both base 10 and base 2:
1 (binary: 1)
3 (binary: 11)
5 (binary: 101)
7 (binary: 111)
9 (binary: 1001)
33 (binary: 100001)
99 (binary: 1100011)
313 (binary: 100111001)
585 (binary: 1001001001)
717 (binary: 1011001101)
*/