Recursion: simplify the problem, solve the simpler one
Example: the multiplication of all the numbers from N
to 1
1 * 2 * 3 * 4 * 5 * 6 * … * N
Simplification: the multiplication of all the numbers from N
to 1
is N
multiplied by the multiplication of all the numbers from N - 1
to 1 which is simpler than the example
N * 1 * 2 * 3 * 4 * 5 * 6 * … * (N-1)
in pseudo-code
factorial(N) = N * factorial(N - 1)
but with the simplest case in code
if (N == 1) return 1;
Put these statements in a function and you have the solution.
solved Can someone explain to me this works?