Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercise 1.11 #6

Merged
merged 2 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/sicp/chapter_1/ex_1_11.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(ns sicp.chapter-1.ex-1-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.

(defn f [n] ; Recursive process
(cond (< n 3) n
:else (+ (f (- n 1))
(* 2 (f (- n 2)))
(* 3 (f (- n 3))))))

(defn f-v2-iter [a b c count]
(if (= count 0)
c
(f-v2-iter b
c
(+ c (* 2 b) (* 3 a))
(- count 1))))

(defn f-v2 [n] ; Iterative process
(cond (< n 3) n
:else (f-v2-iter 0 1 2 (- n 2))))
37 changes: 37 additions & 0 deletions test/sicp/chapter_1/ex_1_11_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
(ns sicp.chapter-1.ex-1-11-test
(:require [clojure.test :refer :all]
[sicp.chapter-1.ex-1-11 :refer [f f-v2]]))

(deftest f-test
; ~ O(3^n)
(time (is (= (f 1) 1))) ; "Elapsed time: 3.541558 msecs"
(time (is (= (f 2) 2))) ; "Elapsed time: 2.143086 msecs"
(time (is (= (f 3) 4))) ; "Elapsed time: 1.413709 msecs"
(time (is (= (f 4) 11))) ; "Elapsed time: 1.343298 msecs"
(time (is (= (f 5) 25))) ; "Elapsed time: 1.148649 msecs"
(time (is (= (f 6) 59))) ; "Elapsed time: 1.667051 msecs"
(time (is (= (f 7) 142))) ; "Elapsed time: 1.326258 msecs"
(time (is (= (f 8) 335))) ; "Elapsed time: 1.483437 msecs"
(time (is (= (f 9) 796))) ; "Elapsed time: 3.40105 msecs"
(time (is (= (f 10) 1892))) ; "Elapsed time: 2.025039 msecs"
(time (is (= (f 20) 10771211))) ; "Elapsed time: 32.969995 msecs
(time (is (= (f 25) 812934961))) ; "Elapsed time: 77.825729 msecs
(time (is (= (f 30) 61354575194))) ; "Elapsed time: 470.359975 msecs"
)

(deftest f-v2-test
; ~ O(n)
(time (is (= (f-v2 1) 1))) ; "Elapsed time: 3.209596 msecs"
(time (is (= (f-v2 2) 2))) ; "Elapsed time: 1.38101 msecs"
(time (is (= (f-v2 3) 4))) ; "Elapsed time: 2.122814 msecs"
(time (is (= (f-v2 4) 11))) ; "Elapsed time: 1.531897 msecs"
(time (is (= (f-v2 5) 25))) ; "Elapsed time: 1.403303 msecs"
(time (is (= (f-v2 6) 59))) ; "Elapsed time: 1.383425 msecs"
(time (is (= (f-v2 7) 142))) ; "Elapsed time: 1.335105 msecs"
(time (is (= (f-v2 8) 335))) ; "Elapsed time: 1.88524 msecs"
(time (is (= (f-v2 9) 796))) ; "Elapsed time: 2.091203 msecs"
(time (is (= (f-v2 10) 1892))) ; "Elapsed time: 3.245716 msecs"
(time (is (= (f-v2 20) 10771211))) ; "Elapsed time: 3.275056 msecs"
(time (is (= (f-v2 25) 812934961))) ; "Elapsed time: 1.989168 msecs"
(time (is (= (f-v2 30) 61354575194))) ; "Elapsed time: 2.462203 msecs"
)