This commit is contained in:
NaiJi 2020-07-19 21:40:50 +03:00
parent 3d0a35684d
commit 8d27dd2d10
1 changed files with 23 additions and 0 deletions

23
sicp-ex-1.8.rkt Normal file
View File

@ -0,0 +1,23 @@
(define (square x) (* x x))
(define (cubic-iter guess guess-prev x)
(if (good-enough? guess guess-prev)
guess
(cubic-iter (improve guess x) guess
x)))
(define (improve guess x)
(average (* 2 guess) (/ x (square guess))))
(define (average x y)
(/ (+ x y) 3))
(define (good-enough? guess guess-prev)
(< (abs (- guess guess-prev)) 0.001))
(define (cubic-root x)
(if (= x 0) x (cubic-iter 1.0 999 x)))
(cubic-root 1892379832.0)
(cubic-root 8.0)
(cubic-root 27.0)