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.15 #10

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
28 changes: 28 additions & 0 deletions src/sicp/chapter_1/ex_1_15.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(ns sicp.chapter-1.ex_1_15)

; Exercise 1.15
; The sine of an angle (specified in radians) can be computed by making use of the approximation sinx≈x
; if x is sufficiently small, and the trigonometric identity sinx=3sinx3−4sin3x3
; to reduce the size of the argument of sin. (For purposes of this exercise an angle is considered “sufficiently
; small” if its magnitude is not greater than 0.1 radians.)

; These ideas are incorporated in the following procedures:

(defn cube
[x]
(* x x x))

(defn p
[x]
(- (* 3 x) (* 4 (cube x))))

(defn sine
[angle]
(if (not (> (abs angle) 0.1))
angle
(p (sine (/ angle 3.0)))))

; 1. How many times is the procedure p applied when (sine 12.15) is evaluated?

; 2. What is the order of growth in space and number of steps (as a function of a)
; used by the process generated by the sine procedure when (sine a) is evaluated?
7 changes: 7 additions & 0 deletions test/sicp/chapter_1/ex_1_15_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns sicp.chapter-1.ex_1_15-test
(:require [clojure.test :refer :all]
[sicp.chapter-1.ex_1_15 :refer [sine]]))

(deftest sine-test
(is (= -0.39980345741334 (sine 12.15))) ; 5 times, O(log3(n))
)