1 2 3 4 5 6 7 8 9 10 |
|
Which of the following is equivalent to the
fun2()
function shown above?
Assumptions
are non-negative integers.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
You can easily deduce the purpose of a recursive function by looking at how it combines the result of a sub solution.
For example,
fun1(a,b)
will keep addinga
untilb
goes to0
. Each timeb
is reduced by1
, the result is added witha
. Hence it is equivalent to a sum itself b times, which is just a b .Then,
fun2(a,b)
is very similar except each timeb
is reduced by1
, the result is multiplied bya
. This is equivalen to a multiplies itself b times, which is just a b .