How to check where a number is special number in PHP

1 Answer

0 votes
function factorial($num) {
    $fact = 1;
    
    while ($num != 0) {
        $fact = $fact * $num;
        $num--;
    }
    
    return $fact;
}

function isSpecial($num) {
    $sum = 0;
    $tmp = $num;
    
    while ($tmp != 0) {
        $sum += factorial($tmp % 10);
        $tmp = (int)($tmp / 10);
    }
    
    return $sum == $num;
}

$num = 145;
if (isSpecial($num)) {
    echo "yes";
}
else {
    echo "no";
}




/*
run:

yes

*/

 



answered Nov 26, 2023 by avibootz

Related questions

1 answer 107 views
1 answer 139 views
1 answer 104 views
1 answer 96 views
1 answer 106 views
1 answer 119 views
...