-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1.45.tex
130 lines (126 loc) · 3.02 KB
/
1.45.tex
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
\documentclass[a4paper,12pt]{article}
\usepackage{listings}
\lstset{language=Lisp}
\begin{document}
Since we don't know yet the number of times we need to apply \\
\lstinline!average-damp!, let's try to guess it by adding a third
parameter.
\begin{lstlisting}
(define (nth-root x n k)
(fixed-point ((repeated average-damp k)
(lambda (y) (/ x (expt y (- n 1)))))
1.0))
(define (test x n k)
(expt (nth-root x n k) n))
\end{lstlisting}
As a test case, we'll compute \lstinline!(test 2 n k)! for some
values of $n$ and $k$, and increment $k$ if the procedure takes too
much time to execute.
\lstinline!C-c C-cuser break! means that we
stopped the program. We obtain the following results
\begin{lstlisting}
(test 2 4 2)
\end{lstlisting}
$\rightarrow$ 1.9999999999999998
\begin{lstlisting}
(test 2 5 2)
\end{lstlisting}
$\rightarrow$ 1.9999858050463717
\begin{lstlisting}
(test 2 6 2)
\end{lstlisting}
$\rightarrow$ 2.0000298387260598
\begin{lstlisting}
(test 2 7 2)
\end{lstlisting}
$\rightarrow$ 1.9999522624207118
\begin{lstlisting}
(test 2 8 2)
\end{lstlisting}
$\rightarrow$ C-c C-cuser break
\begin{lstlisting}
(test 2 8 3)
\end{lstlisting}
$\rightarrow$ 2.0000000000000036
\begin{lstlisting}
(test 2 9 3)
\end{lstlisting}
$\rightarrow$ 2.000006753178216
\begin{lstlisting}
(test 2 10 3)
\end{lstlisting}
$\rightarrow$ 2.000014560609078
\begin{lstlisting}
(test 2 11 3)
\end{lstlisting}
$\rightarrow$ 1.999968957201428
\begin{lstlisting}
(test 2 12 3)
\end{lstlisting}
$\rightarrow$ 1.9999608941805735
\begin{lstlisting}
(test 2 13 3)
\end{lstlisting}
$\rightarrow$ 2.0000853129122884
\begin{lstlisting}
(test 2 14 3)
\end{lstlisting}
$\rightarrow$ 1.9998902567944317
\begin{lstlisting}
(test 2 15 3)
\end{lstlisting}
$\rightarrow$ 2.0001212122767673
\begin{lstlisting}
(test 2 16 3)
\end{lstlisting}
$\rightarrow$ C-c C-cuser break
\begin{lstlisting}
(test 2 16 4)
\end{lstlisting}
$\rightarrow$ 2.0000000000000178
\begin{lstlisting}
(test 2 17 4)
\end{lstlisting}
$\rightarrow$ 1.9999886028289902
\begin{lstlisting}
(test 2 18 4)
\end{lstlisting}
$\rightarrow$ 2.0000109642328416
\begin{lstlisting}
(test 2 19 4)
\end{lstlisting}
$\rightarrow$ 2.0000396021495437
\begin{lstlisting}
(test 2 19 4)
\end{lstlisting}
$\rightarrow$ 2.0000396021495437
\begin{lstlisting}
(test 2 31 4)
\end{lstlisting}
$\rightarrow$ 1.9997159498952142
\begin{lstlisting}
(test 2 32 4)
\end{lstlisting}
$\rightarrow$ C-c C-cuser break
\begin{lstlisting}
(test 2 63 4)
\end{lstlisting}
$\rightarrow$ 1.9996518342602587
\begin{lstlisting}
(test 2 64 4)
\end{lstlisting}
$\rightarrow$ C-c C-cuser break
We remark that the values of $n$ where the test fail are those which
are power of $2$: $8$, $16$, $32$, $64$. And we can see that:
\[ k = \lfloor \lg n\rfloor.\]
So let's redefine \lstinline!nth-root! accordingly and see what happens,
\begin{lstlisting}
(define (nth-root x n)
(fixed-point ((repeated average-damp
(floor (/ (log n)
(log 2))))
(lambda (y) (/ x (expt y (- n 1)))))
1.0))
\end{lstlisting}
It happens that the procedure works.
\end{document}