Beyond the specification map. Recursive function evaluation and recursion depth — recursion, no series content. It is kept in the library for extra maths practice, not as specification-point coverage.
Can we show g(n) is equal to the recursion depth of f(n)?
Question
Let f(n) be a function defined, for any integer n≥0, as follows: f(n)=⎩⎨⎧1(f(n/2))22f(n−1)if n=0,if n>0 and n even,if n>0 and n odd.
What is the value of f(5)?
The recursion depth of f(n) is defined to be the number of other integers m such that the value of f(m) is calculated whilst computing the value of f(n). For example, the recursion depth of f(4) is 3, because the values of f(2), f(1), and f(0) need to be calculated on the way to computing the value of f(4).
What is the recursion depth of f(5)?
Now let g(n) be a function, defined for all integers n≥0, as follows: g(n)=⎩⎨⎧01+g(n/2)1+g(n−1)if n=0,if n>0 and n is even,if n>0 and n is odd.
What is g(5)?
What is g(2k), where k≥0 is an integer? Briefly explain your answer.
What is g(2l+2k), where l>k≥0 are integers? Briefly explain your answers.
Explain briefly why the value of g(n) is equal to the recursion depth of f(n).
Solution
Let f(n) be a function defined, for any integer n≥0, as follows: f(n)=⎩⎨⎧1(f(n/2))22f(n−1)if n=0,if n>0 and n even,if n>0 and n odd.
What is the value of f(5)?
Working backwards, we have
f(5)=2f(4)=2(f(2))2=2(f(1))4=2(2f(0))4=2×24=32.
The recursion depth of f(n) is defined to be the number of other integers m such that the value of f(m) is calculated whilst computing the value of f(n). For example, the recursion depth of f(4) is 3, because the values of f(2), f(1), and f(0) need to be calculated on the way to computing the value of f(4).
What is the recursion depth of f(5)?
On our way to calculating f(5), we also had to calculate f(4), f(2), f(1) and f(0), so the recursion depth is 4.
If we write down the first few terms of the sequence f(n), it appears that f(n)=2n.
We have the sequence defined by u2n=(un)2,u2n−1=2un,u0=1, and it is easy to show using induction that un=2n.
So we could get this sequence more simply by defining un=2un−1 for all n, u0=1.
This definition, however, has a larger recursion depth than the one in the question.
To explain the above; when k=0, 2k=1 so we need to calculate g(2l−k+1). This has an odd input, so the output is 1+g(2l−k). We can then use our answer from (iv), finding g(2l−k)=(l−k)+1 and simplify our expression.
It is interesting to reflect on how g(n) works if n is written in binary.
The rule for g tells us that if n is even, g(n) is 1+g(n/2), that is, 1 + the binary number given by removing the 0 at the end of n.
If n is odd, g(n) is 1+g(n−1), that is, 1+ the binary number given by changing the final 1 to a 0.
Suppose n in binary has j 1s and k 0s. It is easy to see, therefore, that g(n) is 2j+k−1 (the −1 comes in because we don’t remove the final 0).
Imagine calculating f(n) and g(n) at the same time, so we take it in turns to carry out one step in the calculation.
With every step we carry out on f(n) we create an expression in terms of f(c) where c is an integer less than n.
With each corresponding step for g(n), we make g(n) the sum of g(c) and the total number of steps we’ve taken so far (this is equal to the number of different c values we’ve used so far).
Eventually we’ll reach an expression for f(n) in terms of f(0), and g(n)=g(0)+(number of values of c used)=0+(number of values of c used).