-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathex1-45-nth-root.scm
67 lines (49 loc) · 1.63 KB
/
ex1-45-nth-root.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
; Ex. 1.45 N-th root
; The experiments I ran, to discover how many average damping
; operations need to be applied as n grows, showed that the
; former number is proportion to the natural logarithm of n.
; I've added a margin of safety--multiply log(n) by 2--that I hope
; will provide coverage for the full range of real numbers my PC
; can process.
(define (nth-root fx n)
(fixed-point
((repeated average-damp (* (round (log n)) 2))
(lambda (y) (/ fx (expt y (- n 1)))))
1.0))
(define (average-damp f)
(lambda (x) (average x (f x))))
; Procedure to repeatedly apply a function
(define (repeated f n)
(define (recurse result count)
(if (= count n)
result
(recurse (compose f result) (+ count 1))))
(recurse f 1))
(define (compose f g)
(lambda (x) (f (g x))))
(define (average x y)
(/ (+ x y) 2))
; Fixed-point function
(define tolerance 0.00001)
(define (fixed-point f first-guess)
(define (close-enough? v1 v2)
(< (abs (- v1 v2)) tolerance))
(define (try guess)
(let ((next (f guess)))
(if (close-enough? guess next)
next
(try next))))
(try first-guess))
; From Ex. 1-16 Iterative exponentiation process that grows by
; theta(log n) steps in constant space.
(define (successive-sq b n)
(if (= n 0)
1
(successive-iter 1 b n)))
(define (successive-iter a b n)
(cond ((= n 1) a)
((even? n) (successive-iter (* a (square b)) b (/ n 2)))
(else (successive-iter (* a b) b (- n 1)))))
(define (even? n)
(= (remainder n 2) 0))
(define (square n) (* n n))