Here's my solution with a nested loop. Quick and dirty.
$result = array();
for ($i = 1000; $i < 5000; $i++) {
for ($m = 2; $m < 10; $m++) {
if ($i*$m == (int)strrev($i)) {
$result[] = array($i, $m);
}
}
}
var_dump($result);
I'd like to expand on this line:
if ($i*$m == (int) strrev($i)) {
One side is $i*$m
, easy, the multipication.
On the other, we have (int)strrev($i)
, which means "Take $i
, cast it to a string, and reverse that. Then cast it back into an int
.
If that evaluates to true, an array containing $i
and m
is inserted into the $result
array.