1)牛頓法求平方根:python
公式:(y + x/y) / 2,首先猜想爲1,而後逐漸逼近。spa
(defun sqrt-iter (guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x))) (defun improve (guess x) (average guess (/ x guess))) (defun average (x y) (/ (+ x y) 2)) (defun good-enough? (guess x) (< (abs (- (square guess) x)) 0.001)) (defun square (x) (* x x)) (defun sqrt (x) (sqrt-iter 1.0 x))
2)牛頓法求立方根code
公式:(x/(y^2) + 2y)/3it
(defun cube-iter (guess x) (if (good-enough? guess x) guess (cube-iter (improve guess x) x))) (defun improve (guess x) (average (* 2 guess) (/ x (* guess guess)))) (defun average (x y) (/ (+ x y) 3)) (defun good-enough? (guess x) (< (abs (- (cube guess) x)) 0.001)) (defun cube (x) (* x x x)) (defun cube-root (x) (cube-iter 1.0 x))