Skip to content

Commit

Permalink
Exercise 1.14
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Smet committed Oct 15, 2023
1 parent 68461c4 commit 224b567
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/sicp/chapter_1/ex_1_13.clj
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
(ns sicp.chapter-1.ex-1-13)

; Exercise 1.13
; Prove that Fib(n) is the closest integer to φn/5⎯√, where φ=(1+5⎯√)/2.
; Hint: Let ψ=(1−5⎯√)/2.
; Use induction and the definition of the Fibonacci numbers (see 1.2.2) to prove that Fib(n)=(φn−ψn)/5⎯√

; Skipped. I'm too young for higher math.
24 changes: 24 additions & 0 deletions src/sicp/chapter_1/ex_1_14.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(ns sicp.chapter-1.ex-1-14)

; Exercise 1.14
; Draw the tree illustrating the process generated by the count-change procedure of 1.2.2 in making change for 11 cents.
; What are the orders of growth of the space and number of steps used by this process as the amount to be changed increases?

(defn first-denomination
[type-of-coins]
(cond (= type-of-coins 1) 1
(= type-of-coins 2) 5
(= type-of-coins 3) 10
(= type-of-coins 4) 25
(= type-of-coins 5) 50))

(defn change
[amount type-of-coins]
(cond (= amount 0) 1
(or (< amount 0) (= type-of-coins 0)) 0
:else (+ (change amount (- type-of-coins 1))
(change (- amount (first-denomination type-of-coins)) type-of-coins))))

(defn money-change
[amount]
(change amount 5))
8 changes: 8 additions & 0 deletions test/sicp/chapter_1/ex_1_14_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(ns sicp.chapter-1.ex-1-14-test
(:require [clojure.test :refer :all]
[sicp.chapter-1.ex-1-14 :refer [money-change]]))

(deftest count-change-test
(is (= 1 (money-change 1)))
(is (= 4 (money-change 10)))
(is (= 292 (money-change 100))))

0 comments on commit 224b567

Please sign in to comment.