From: Ramakrishnan Muthukrishnan Date: Sun, 30 May 2010 01:55:16 +0000 (+0530) Subject: solution to 1.45 X-Git-Url: https://git.rkrishnan.org/listings/COPYING.TGPPL.html?a=commitdiff_plain;h=6b83eaae6fe44cb1823614a025d2d7073e8f2bd2;p=sicp.git solution to 1.45 --- diff --git a/src/sicp/ex1_45.clj b/src/sicp/ex1_45.clj new file mode 100644 index 0000000..cf077d9 --- /dev/null +++ b/src/sicp/ex1_45.clj @@ -0,0 +1,60 @@ +(ns sicp.ex1_45 + (:use [clojure.contrib test-is] + [sicp utils] + [sicp ch1_3] + [sicp ex1_43])) + +(defn nthroot [x n] + (fixed-point ((repeated average-damp + (int (/ (Math/log n) (Math/log 2)))) + (fn [y] (/ x (Math/pow y (- n 1))))) + 1.0)) + +;; experiments show that average damp needs to e applied log2n times. +(comment +user> (nthroot (* 3 3 3) 3) +;;=> 2.9999972321057697 +user> (nthroot (* 3 3 3 3) 4) +; Evaluation aborted. +user> (use 'sicp.ex1_45 :reload) +;;=> nil +user> (nthroot (* 3 3 3 3) 4) +;;=> 3.000000000000033 +user> (nthroot (* 3 3 3 3 3) 5) +;;=> 3.0000008877496294 +user> (nthroot (* 3 3 3 3 3 3) 5) +;;=> 3.737194011460545 +user> (nthroot (* 3 3 3 3 3 3) 6) +;;=> 2.999996785898161 +user> (nthroot (* 3 3 3 3 3 3 3) 7) +;;=> 3.0000041735235947 +user> (nthroot (* 3 3 3 3 3 3 3 3) 8) +;;=> ; Evaluation aborted. +user> (use 'sicp.ex1_45 :reload) +;;=> nil +user> (nthroot (* 3 3 3 3 3 3 3 3) 8) +;;=> 3.0000000000173292 +user> (nthroot (* 3 3 3 3 3 3 3 3 3 3 3 3 3 3) 14) +;;=> 2.9999959148601363 +user> (nthroot (* 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3) 15) +;;=> 3.000004202219401 +user> (nthroot (* 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3) 16) +;;=> ; Evaluation aborted. +user> (Math/log2 10) +;;=> ; Evaluation aborted. +user> (/ (Math/log 10) (Math/log 2)) +;;=> 3.3219280948873626 +user> (/ (Math/log 16) (Math/log 2)) +;;=> 4.0 +user> (use 'sicp.ex1_45 :reload) +;;=> nil +user> (nthroot (* 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3) 16) +;;=> 3.0 +user> (nthroot (Math/pow 3 16) 16) +;;=> 3.0 +user> (nthroot (Math/pow 3 35) 35) +;;=> 3.000000146591681 +user> (nthroot (Math/pow 3 50) 50) +;;=> 2.9999967255008917 +user> +) \ No newline at end of file