Categories
How to read code with recursion?
I am learning php. Got code:
function factor($n) { if ($n <= 0) return 1; else return $n * factor($n — 1); } echo factor(20);
$n * factor($n — 1);
Answer the question
In order to leave comments, you need to log in
Because the factorial is the product of all positive numbers up to the current one. Starting from the desired number, we multiply it by the previous one at each iteration until we get 0, which will return 1. For 5, these are the steps: 5 4 3 2 1 0
Didn't find what you were looking for?
Ask a Question
731 491 924 answers to any question