Note that

N! = 1 * 2 * ... * (n - 1) * n = (n - 1)! * N

For n>1 factorial of n is equal to the factorial of n-1 multiplied by n. This way of defining the value of the function by the same function calculated for the modified (usually reduced) values of the argument of this function is called recursion. In other words, the recursive definition of a function occurs when the same function appears in the definition. Of course, calculating the factorial of some integer, we can not indefinitely call factorial function for subsequent algorithms. The algorithm must be implemented in a finite number of steps. There must be an argument value for which we will get some specific value instead of the next function call. In the case of the factorial function this value is 1, 1! = 1.

  The definition of the factorial function we wrote at the outset we must supplement the condition interrupting recursion, we will finally get:

N! = 1 * 2 * ... * (n - 1) * n = (n - 1)! * N for n> 1

1! = 1

fact rec en

Factorial in Pascal - recursive
Factorial in C++ - recursive
Factorial in Java - recursive

   
© jtrzeciak@alg24.com