1.6
死循环
1.7
1 #lang racket 2 (define (square x) (* x x)) 3 (define (sqrt-iter guess x) 4 (if (good-enough? guess x) 5 guess 6 (sqrt-iter (improve guess x) 7 x))) 8 (define (improve guess x) 9 (average guess (/ x guess))) 10 11 (define (average x y) 12 (/ (+ x y) 2)) 13 14 (define (good-enough? guess x) 15 (< (abs (- (/ (square guess) x) 1) ) 0.00001)) 16 17 (define (sqrt x) 18 (sqrt-iter 1.0 x)) 19 20 21 (sqrt 0.002)
1.8立方根的牛顿法
1 #lang racket 2 (define (power3 x) (* x x x)) 3 (define (square x) (* x x)) 4 (define (curo-iter guess x) 5 (if (good-enough? guess x) 6 guess 7 (curo-iter (improve guess x) 8 x))) 9 (define (improve guess x) 10 (average3 (* 2 guess) (/ x (square guess)))) 11 (define (average3 x y) 12 (/ (+ x y) 3)) 13 (define (good-enough? guess x) 14 (< (abs (- (/ (power3 guess) x) 1) ) 0.00001)) 15 (define (curo x) 16 (curo-iter 1.0 x)) 17 18 (curo 27)