-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathex1-37-cont-frac-iter.scm
154 lines (108 loc) · 3.25 KB
/
ex1-37-cont-frac-iter.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
; Ex. 1.37 K-term Finite Generalized Continued Fractions
; https://en.wikipedia.org/wiki/Generalized_continued_fraction
; To solve this Iteratively, one may like to proceed "Bottom-up"
;http://www.cs.cas.cz/portal/AlgoMath/NumberTheory/ContinuedFractions/BasicDefinitions.htm
; The iterative transformation is defined thus:
; Suppose result = d(k) to begin with, then the next result is
; arrived at, by the transform: result |--> (d(k-1) + n(k)/result)
(define (cont-frac d n k)
(define (cont-frac-iter result count)
(if (= count 1)
(/ (n 1) result)
(cont-frac-iter (+ (d (- count 1)) (/ (n count) result))
(- count 1))))
(cont-frac-iter (d k) k))
; Test data showing how cont-frac converges with value of k, when
; used to calculate the value of 1/phi.
; Assuming the output of cont-frac is consistently rounded UP to
; the desired decimal accuracy;
; k >= 13 yields 1/phi accurate to 4 decimal places
; k >= 14 yields 1/phi accurate to 5 decimal places
; (cont-frac (lambda (x) 1.0)
; (lambda (x) 1.0)
; 1)
; ;Value: 1.
; (cont-frac (lambda (x) 1.0)
; (lambda (x) 1.0)
; 2)
; ;Value: 1.
; (cont-frac (lambda (x) 1.0)
; (lambda (x) 1.0)
; 3)
; ;Value: .5
; (cont-frac (lambda (x) 1.0)
; (lambda (x) 1.0)
; 4)
; ;Value: .6666666666666666
; (cont-frac (lambda (x) 1.0)
; (lambda (x) 1.0)
; 5)
; ;Value: .6000000000000001
; (cont-frac (lambda (x) 1.0)
; (lambda (x) 1.0)
; 7)
; ;Value: .6153846153846154
; (cont-frac (lambda (x) 1.0)
; (lambda (x) 1.0)
; 8)
; ;Value: .6190476190476191
; (cont-frac (lambda (x) 1.0)
; (lambda (x) 1.0)
; 9)
; ;Value: .6176470588235294
; (cont-frac (lambda (x) 1.0)
; (lambda (x) 1.0)
; 10)
; ;Value: .6181818181818182
; (cont-frac (lambda (x) 1.0)
; (lambda (x) 1.0)
; 11)
; ;Value: .6179775280898876
; (cont-frac (lambda (x) 1.0)
; (lambda (x) 1.0)
; 12)
; ;Value: .6180555555555556
; (cont-frac (lambda (x) 1.0)
; (lambda (x) 1.0)
; 13)
; ;Value: .6180257510729613
; (cont-frac (lambda (x) 1.0)
; (lambda (x) 1.0)
; 14)
; ;Value: .6180371352785146
; (cont-frac (lambda (x) 1.0)
; (lambda (x) 1.0)
; 15)
; ;Value: .6180327868852459
; (cont-frac (lambda (x) 1.0)
; (lambda (x) 1.0)
; 16)
; ;Value: .6180344478216819
; (cont-frac (lambda (x) 1.0)
; (lambda (x) 1.0)
; 17)
; ;Value: .6180338134001252
; (cont-frac (lambda (x) 1.0)
; (lambda (x) 1.0)
; 18)
; ;Value: .6180340557275542
; (cont-frac (lambda (x) 1.0)
; (lambda (x) 1.0)
; 19)
; ;Value: .6180339631667064
; (cont-frac (lambda (x) 1.0)
; (lambda (x) 1.0)
; 20)
; ;Value: .6180339985218034
; (cont-frac (lambda (x) 1.0)
; (lambda (x) 1.0)
; 21)
; ;Value: .6180339850173578
; (cont-frac (lambda (x) 1.0)
; (lambda (x) 1.0)
; 22)
; ;Value: .6180339901755971
; (cont-frac (lambda (x) 1.0)
; (lambda (x) 1.0)
; 30)
; ;Value: .6180339887505407