Robson » Code » PHP » Factorial Sum

 
<?
   // factorial sum v1.0
 
   // returns the sum of the factorials of the digits of a number
   function factorial_sum($number)
   {
       $factorials = array(1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880);
       $numbers = str_split($number);
       foreach ($numbers as $digit)
           $total += $factorials[$digit];
       return $total;
   }
   
   // * example use - show factorial sums for numbers 1 to 150 * //
   for ($n = 1; $n < 150; $n++)
       echo $n . ' = ' . factorial_sum($n) . '<br />';
?>
 
© 2004-17 robson | cc unless stated