Last active
October 16, 2015 11:57
-
-
Save jsp1611/28ce8e05bcef28196f54 to your computer and use it in GitHub Desktop.
Exercise 1.11 in SICP. First attempt is linear recursive.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Define a function f such that: | |
;; f(n) = n if n<3 and f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) if n> 3 | |
;; This is the recursive implementation that doesn't scale well | |
(define (f n) | |
(if (< n 3) | |
n | |
(+ (f (- n 1)) | |
(* 2 (f (- n 2))) | |
(* 3 (f (- n 3)))) | |
) | |
) | |
;; Example call that will take some time to complete | |
(f 18) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment