diff --git a/src/sicp/chapter_1/ex_1_15.clj b/src/sicp/chapter_1/ex_1_15.clj new file mode 100644 index 0000000..b926226 --- /dev/null +++ b/src/sicp/chapter_1/ex_1_15.clj @@ -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? diff --git a/test/sicp/chapter_1/ex_1_15_test.clj b/test/sicp/chapter_1/ex_1_15_test.clj new file mode 100644 index 0000000..15e929a --- /dev/null +++ b/test/sicp/chapter_1/ex_1_15_test.clj @@ -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)) +)