A Munchausen number is a natural number n the sum of whose digits (in base 10), each raised to the power of itself, equals n.

(Munchausen is also spelled: Münchhausen.)

For instance:    3435 = 33 + 44 + 33 + 55 



Task
Find all Munchausen numbers between   1   and   5000.



Also see

 The OEIS entry:  A046253
 The Wikipedia entry:  Perfect digit-to-digit invariant, redirected from Munchausen Number


 
<?php 
 
$pwr = array_fill(0, 10, 0);
 
function isMunchhausen($n)
{
    global $pwr;
    $sm = 0;
    $temp = $n;
    while ($temp) {
        $sm= $sm + $pwr[($temp % 10)];
        $temp = (int)($temp / 10);
    }
    return $sm == $n;
}
 
for ($i = 0; $i < 10; $i++) {
    $pwr[$i] = pow((float)($i), (float)($i));
}
 
for ($i = 1; $i < 5000 + 1; $i++) {
    if (isMunchhausen($i)) {
        echo $i . PHP_EOL;
    }
}