Skip to content
Advertisement

How to make a script which outputs the ratio of the factorials of two numbers (N! / M!)?

I have a code that find n factorial:

    $n = 5;
    $factorial = 1;

    function fact($n)
   { 
       for ($i = 1; $i <= $n; $i++) {
           $factorial *= $i;
       }
   }
   echo $factorial;

but, how can I find the ratio of the factorials of two numbers (N! / M!)?

Advertisement

Answer

You can add a return in you function and call her 2 times:

$n = 5;
$m = 2;

function fact(Int $n) : Int{ 
   $factorial = 1;
   for ($i = 1; $i <= $n; $i++) {
       $factorial *= $i;
   }
   return $factorial;
}

$factorialOfTwoNumbers = fact($n) / fact($m);
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement