-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.45.rkt
95 lines (74 loc) · 2.11 KB
/
1.45.rkt
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#lang sicp
(define (compose f g) (lambda (x) (f (g x))))
(define (square x) (* x x))
(define (repeated f n)
(if (= n 1)
f
(compose (repeated f (- n 1)) f)))
(define (average a b)
(/ (+ a b) 2))
(define (average-damp f)
(lambda (x) (average x (f x))))
(define (iterate start improve good-enough?)
(if (good-enough? start)
start
(iterate (improve start) improve good-enough?)))
(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))
(define (root2 x)
(fixed-point (average-damp (lambda (y) (/ x y)))
1.0))
(define (root3 x)
(fixed-point (average-damp (lambda (y) (/ x (expt y 2))))
1.0))
; doesn't terminate without avg damp 2
(define (root4 x)
(fixed-point ((repeated average-damp 2)
(lambda (y) (/ x (expt y 3))))
1.0))
(define (root5 x)
(fixed-point ((repeated average-damp 2)
(lambda (y) (/ x (expt y 4))))
1.0))
(define (root6 x)
(fixed-point ((repeated average-damp 2)
(lambda (y) (/ x (expt y 5))))
1.0))
(define (root7 x)
(fixed-point ((repeated average-damp 2)
(lambda (y) (/ x (expt y 6))))
1.0))
; doesnt terminate with 2 repeats
(define (root8 x)
(fixed-point ((repeated average-damp 3)
(lambda (y) (/ x (expt y 7))))
1.0))
(define (root9 x)
(fixed-point ((repeated average-damp 2)
(lambda (y) (/ x (expt y 8))))
1.0))
(define (root15 x)
(fixed-point ((repeated average-damp 3)
(lambda (y) (/ x (expt y 14))))
1.0))
; doesn't terminate with 3 repeats
(define (root16 x)
(fixed-point ((repeated average-damp 4)
(lambda (y) (/ x (expt y 15))))
1.0))
(define (log2 x)
(/ (log x) (log 2)))
(define (nth-root n x)
(fixed-point ((repeated average-damp (floor (log2 n)))
(lambda (y) (/ x (expt y (- n 1)))))
1.0))
(nth-root 16 1024)
(root16 1024)