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.14 #9

Merged
merged 1 commit 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
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))))