11
Exercise 1.11: A function f is defined by the rule that $| f(n)=n |$ if $| n<3 |$ and $| f(n)=f(n-1)+2f(n-2)+3f(n-3) |$ if $| n>=3 |$. Write a procedure that computes f by means of a recursive process. Write a procedure that computes f by means of an iterative process.
Recursive process
Iterative process
Use a tuple of integers a and b and c, initialized to $| f(0) = 0 |$, $| f(1) = 1 |$, and $| f(2) = 2 |$, and to repeatedly apply the simultaneous transformations
It is not head to show that, after applying this transformation n times, a, b, and c will be equal, respectively, to $| f(n+2) |$, $| f(n+1) |$ and $| f(n) |$. Thus, we can compute $| f(n) |$ iteratively using the procedure
Last updated
Was this helpful?