Practice Recursion
- Karla G
- 28 mar 2015
- 1 Min. de lectura
Questions:
1.- What is a recursion?
2.- In this code:
function factorial (n is a non-negative integer)
if n is 0 then
return 1 [by the convention that 0! = 1]
else
return factorial(n – 1) times n [recursively invoke factorial with the parameter 1 less than n]
end if
end function
When n=4, how many times the function is re-executed? (Not counting the first time is called, only the recursions)
3.- State the operations that the last program makes, for example, the first line of operation would be:
factorial(4-1) times 4 --> 3!*4
Continue...
Answers:
1.- A function that calls itself as a part of a process dividing the tasks in more little and little tasks.
2.- 4 times.
3.-
factorial(4-1) times 4 --> 3!*4
factorial(3-1) times 3 --> 2!*3
factorial(2-1) times 2 --> 1!*2
factorial(1-1) times 1 --> 0!*1
Commenti