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. I am having trouble with the recursive process. I wrote the iterative process form just fine: (define (f-iter n) (if (< n 3) n (f-iter (- n 1))))
in which language should i write it? Any way Here is the algorithm, you should be able to convert it to any language of your choice: Procedure RecurFunc(int n); Start; int ans; if (n<3) then ans = n; else ans = RecurFunc(n-1) + (2 * RecurFunc(n-2)) + (3 * RecurFunc(n-3)); Print ans; Stop. That's it. The function just keeps on calling itself till it reaches a value of n<3 for which it assigns n. But it solving this, i assumed the function is calculated for n>3, you didn't tell what happens when n=3.
(define (f n) (if (< n 3) n (+ (f (- n 1)) (* 2 (f (- n 2))) (* 3 (f (- n 3))))))
Join our real-time social learning platform and learn together with your friends!