The evaluation process of the iterative/recursive version of plus(a, b):
Exercise 1.10
The evaluation of the Ackermann’s function:
output:
Mathematical definitions for the provided functions:
function 1:
The math expression of f(n) is: 2n
function 2:
Function expansion:
Therefore, the math expression of g(n) is: 2n
function 3:
Function expansion:
Therefore, the math expression of h(n) is: (n−1)2=222⋅⋅2
function 4:
The math expression of k(n) is: 5n2
Exercise 1.11
The JavaScript version of the math function f in both recursive and iterative methods
output:
Exercise 1.12
Pascal’s triangle:
output:
Exercise 1.13
Step 1
We assume that Fib(n)=rn, and the recursive format of Fib(n) can be expressed as: rn=rn−1+rn−2
Assuming that r=0, we divide both sides by rn−2, and get:
r2=r+1
Given that in the quadratic formula ax2+bx+c=0:
x=2a−b±b2−4ac
We can solving the equation:
r=21±5
Then we can denote each solution as:
ϕ=21+5,ψ=21−5
Step 2
The Fibonacci sequence if a linear homogeneous recurrence relation of the second degree, its general solution is a linear combination of the solutions found:
Fib(n)=A(ϕn)+B(ψn)
For n=0,Fib(0)=0 and n=1,Fib(1)=1, we can find the values of A and B:
A+B=0Aϕ+Bψ=1A=51,B=−51
Step 3
Now we subsitute A and B back into the general solution:
Fib(n)=51ϕn−51ψn=5ϕn−ψn
With n increasing, ψn will gradually approaching 0, therefore Fib(n) is the closest integer to ϕn/5
Exercise 1.14
The tree-recursive process generated in computing count_change(11) function:
The step has Θ(2N) order of growth, where N is the target amount;
The order of growth of the space is Θ(N⋅m), where N is the target amount and m is the number of coin dominations, because each combination of amount and coin denomication is calculated only once.
Exercise 1.15
Compute the sine value of an angle:
output:
The number of steps of sine(angle) is Θ(log3mN), where N is the angle, and m is the precision;
The space complexity is also Θ(log3mN), reflecting the depth of the recursive calls.
Exercise 1.16
The iterative version of the fast-exponentiation algorithm:
output:
Exercise 1.17
A faster version of times(a, b) function with Θ(logn) complexity:
output:
Exercise 1.18
The iterative version of the fastTimes(a, b):
output:
Exercise 1.19
Applying the transformation matrix in computing fibonacci numbers to achieve Θ(logn) complexity. This exercise is a bit tricky, please refer to Kaldewaji 1990, page 88 for the details of the expressions of the transformation matrix: