-
Notifications
You must be signed in to change notification settings - Fork 1
/
2.19.tex
70 lines (66 loc) · 2.52 KB
/
2.19.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
\documentclass[a4paper,12pt]{article}
\usepackage{listings}
\lstset{language=Lisp}
\begin{document}
\begin{lstlisting}
(define (cc amount coin-values)
(cond ((= amount 0) 1)
((or (< amount 0) (no-more? coin-values)) 0)
(else
(+ (cc amount
(except-first-denomination
coin-values))
(cc (- amount
(first-denomination
coin-values))
coin-values)))))
(define no-more? null?)
(define except-first-denomination cdr)
(define first-denomination car)
\end{lstlisting}
The order of the list \lstinline!coin-values! doesn't affect the
answer produced by \lstinline!cc!. Let's proove this by induction
according to the number $m$ of the elements of
\lstinline!coin-values!.
The result is trivial for $m = 1$. Suppose we have the result for
$m$. Note $a_1, a_2, \ldots, a_{m+1}$ the elements of
\lstinline!coin-values!. And let $k$ be an integer verifying
$ 2 \le k \le m+1$. Let's show that we can swap $a_1$ and $a_k$
without modifying the value returned by \lstinline!cc!.
Note $n$ the amount we want to count the number of changes. Suppose
$n > 0$ otherwise the result is straightforward. We have,
\begin{eqnarray*}
\mathrm{cc}(n, a_1,\ldots,a_k,\ldots,a_{m+1}) &=&
\mathrm{cc}(n, a_2,\ldots,a_k,\ldots,a_{m+1}) + \\ &&
\mathrm{cc}(n-a_1,a_2,\ldots,a_k,\ldots,a_{m+1})
\end{eqnarray*}
Note that we have only $m$ coins left in the right-hand side of the
equality, so by induction we have
\begin{eqnarray*}
\mathrm{cc}(n, a_1,\ldots,a_k,\ldots,a_{m+1}) &=&
\mathrm{cc}(n, a_k, a_2,\ldots,a_{k-1},a_{k+1},\ldots,a_{m+1}) +
\\&&
\mathrm{cc}(n-a_1, a_k, a_2, \ldots, a_{k-1}, a_{k+1}, \ldots,
a_{m+1}) \\ &=&
\mathrm{cc}(n, a_2, \ldots, a_{k-1},a_{k+1},\ldots, a_{m+1}) + \\ &&
\mathrm{cc}(n-a_k, a_2, \ldots, a_{k-1}, a_{k+1},\ldots,a_{m+1}) +
\\ &&
\mathrm{cc}(n-a_1, a_2, \ldots, a_{k-1}, a_{k+1}, \ldots, a_{m+1}) +
\\ &&
\mathrm{cc}(n-a_1-a_k, a_2, \ldots, a_{k-1}, a_{k+1}, \ldots,
a_{m+1}) \\
\end{eqnarray*}
Grouping the first and third terms and the second and last one we
obtain
\newpage
\begin{eqnarray*}
\mathrm{cc}(n, a_1,\ldots,a_k,\ldots,a_{m+1}) &=&
\mathrm{cc}(n, a_1, a_2, \ldots, a_{k-1}, a_{k+1}, \ldots, a_{m+1}) +
\\ &&
\mathrm{cc}(n-a_k, a_1, a_2, \ldots, a_{k-1}, a_{k+1}, \ldots,
a_{m+1}) \\ &=&
\mathrm{cc}(n, a_k, a_1, a_2, \ldots, a_{k-1}, a_{k+1}, \ldots, a_{m+1})
\end{eqnarray*}
So finally we deduce that the order of the list doesn't affect the
result of \lstinline!cc!.
\end{document}