Initial commit
This commit is contained in:
parent
dd8711dbd0
commit
48a8c1dcf6
27
SICP/exercise_1_7.janet
Normal file
27
SICP/exercise_1_7.janet
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
(defn square [x] (* x x))
|
||||
|
||||
(defn good-enough? [previous current]
|
||||
(>=
|
||||
(* current 0.001)
|
||||
(math/abs (- current previous))))
|
||||
|
||||
(defn average [x y]
|
||||
(/ (+ x y) 2))
|
||||
|
||||
(defn improve [guess x]
|
||||
(average guess (/ x guess)))
|
||||
|
||||
(defn sqrt-iter [prev guess x]
|
||||
(if (good-enough? prev guess)
|
||||
guess
|
||||
(sqrt-iter guess (improve guess x) x)))
|
||||
|
||||
(defn sqrt [x]
|
||||
(sqrt-iter 0.0 x x))
|
||||
|
||||
(var i 0.0000001)
|
||||
(while (<= i 1000000000000.0)
|
||||
(def meas (sqrt i))
|
||||
(def rel (/ (- meas (math/sqrt i)) meas))
|
||||
(print (string/format "Relative error %f=%f, %f" (math/sqrt i) meas rel))
|
||||
(set i (* i 3.141519)))
|
||||
26
SICP/exercise_1_8.janet
Normal file
26
SICP/exercise_1_8.janet
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
(defn square [x] (* x x))
|
||||
|
||||
(defn good-enough? [previous current]
|
||||
(>=
|
||||
(* current 0.0001)
|
||||
(math/abs (- current previous))))
|
||||
|
||||
(defn improve [guess x]
|
||||
(/
|
||||
(+ (* 2 guess) (/ x (square guess)))
|
||||
3))
|
||||
|
||||
(defn cbrt-iter [prev guess x]
|
||||
(if (good-enough? prev guess)
|
||||
guess
|
||||
(cbrt-iter guess (improve guess x) x)))
|
||||
|
||||
(defn cube-root [x]
|
||||
(cbrt-iter 0.0 x x))
|
||||
|
||||
(var i 0.0000001)
|
||||
(while (<= i 1000000000000.0)
|
||||
(def meas (cube-root i))
|
||||
(def rel (/ (- meas (math/cbrt i)) meas))
|
||||
(print (string/format "Relative error %f=%f, %f" (math/cbrt i) meas rel))
|
||||
(set i (* i 3.141519)))
|
||||
Loading…
Reference in New Issue
Block a user