Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added 1.13 and 1.14

  • Loading branch information...
commit 6904e83aa8beeb6f6c19802ced8c042a9e83ebf1 1 parent 2126206
@sarabander authored
Showing with 215 additions and 0 deletions.
  1. BIN  1.2/Ex1.13.pdf
  2. +115 −0 1.2/Ex1.13.tex
  3. +100 −0 1.2/Ex1.14.scm
View
BIN  1.2/Ex1.13.pdf
Binary file not shown
View
115 1.2/Ex1.13.tex
@@ -0,0 +1,115 @@
+% (c) Andres Raba 2011
+% GPL v.3
+
+\documentclass{article}
+
+%\usepackage[scaled=0.85]{beramono}
+%\usepackage{fourier}
+\usepackage{inconsolata}
+\usepackage[bitstream-charter]{mathdesign}
+
+\usepackage{vmargin}
+\setpapersize{custom}{130mm}{170mm}
+\setmarginsrb{1.5cm}{1.0cm} % left, top
+ {1.5cm}{1.0cm} % right, bottom
+ {12pt}{10pt} % head height, sep
+ {12pt}{10pt} % foot height, sep
+\usepackage{fancyhdr}
+\pagestyle{fancy}
+\lhead{}
+\chead{}
+\rhead{\thepage}
+\lfoot{}
+\cfoot{}
+\rfoot{}
+\renewcommand{\headrulewidth}{0pt}
+\renewcommand{\footrulewidth}{0pt}
+\usepackage{amsmath}
+\newenvironment{rcase}{\left.\begin{aligned}}{\end{aligned}\right\rbrace}
+\usepackage{setspace}
+\onehalfspacing
+\usepackage{fancyvrb}
+
+\newcommand{\D}{\displaystyle}
+\newcommand{\T}{\textstyle}
+
+% --------------------------------------------------
+\usepackage{titlesec}
+\usepackage[usenames,dvipsnames]{color}
+
+\definecolor{pythonblue}{RGB}{54,112,160}
+\definecolor{gray}{RGB}{112,112,112}
+
+\titleformat{\section}
+{\color{pythonblue}\normalfont\huge\bfseries}
+{}{}{{\color{pythonblue}}}
+
+\titleformat{\subsection}
+{\color{gray}\normalfont\Large\bfseries}
+{}{}{{\color{gray}}}
+% --------------------------------------------------
+
+\begin{document}
+
+\section*{Exercise 1.13}
+
+\subsection*{Golden ratio and Fibonacci numbers}
+
+Using the definition of Fibonacci numbers:
+$$
+{\rm Fib}(n) =
+\begin{cases}
+ 0 & \text{if $n = 0$} \\
+ 1 & \text{if $n = 1$} \\
+ {\rm Fib}(n-1) + {\rm Fib}(n-2) $\quad$ & \text{otherwise}
+\end{cases}
+$$
+and mathematical induction, we will prove that
+$$ \text{Fib}(n) = \frac{\varphi^n - \psi^n}{\sqrt{5}}. $$
+
+%\subsection*{Proof}
+
+First, we take $n = 0$ and $n = 1$ as induction base and show that
+$ \text{Fib}(n) = \D\frac{\varphi^n - \psi^n}{\sqrt{5}} $ is valid in these cases.
+$$
+\begin{array}{rcl}
+\text{Fib}(0) & = & \D\frac{\varphi^0 - \psi^0}{\sqrt{5}} =
+ \D\frac{1 - 1}{\sqrt{5}} = 0 \\
+
+\hspace{1ex} & & \\
+
+\text{Fib}(1) & = & \D\frac{\varphi^1 - \psi^1}{\sqrt{5}} =
+ \D\frac{1}{\sqrt{5}} \left(\D\frac{1 + \sqrt{5}}{2} -
+\D\frac{1 - \sqrt{5}}{2}\right) =
+ \D\frac{2\sqrt{5}}{2\sqrt{5}} = 1
+%\end{displaystyle}
+\end{array}
+$$
+Yes, they agree with the definition.
+
+Next, we presume that the following is true for some $k < n$:
+$$ \text{Fib}(k) = \frac{\varphi^k - \psi^k}{\sqrt{5}}. $$
+We will show that the truth of last statement implies the truth of
+$$ \text{Fib}(k + 1) = \frac{\varphi^{k+1} - \psi^{k+1}}{\sqrt{5}}. $$
+
+By definition of the Fibonacci sequence, and using the equations
+$\varphi^2 = \varphi + 1$ and $\psi^2 = \psi + 1$ we have:
+\begin{eqnarray*}
+\text{Fib}(k+1) & = & \text{Fib}(k) + \text{Fib}(k-1) =
+\frac{\varphi^k - \psi^k}{\sqrt{5}} +
+\frac{\varphi^{k-1} - \psi^{k-1}}{\sqrt{5}} \\
+& = &
+\frac{\varphi^{k-1} (\varphi + 1) - \psi^{k-1} (\psi + 1)}{\sqrt{5}} =
+\frac{\varphi^{k-1} \varphi^2 - \psi^{k-1} \psi^2}{\sqrt{5}} \\
+& = &
+\frac{\varphi^{k+1} - \psi^{k+1}}{\sqrt{5}}. \quad \text{QED.}
+\end{eqnarray*}
+
+We have just established that the induction step is valid. This can now be
+used to show that if a statement with $n = k$ is true, then the one with
+$n = k + 1$ is also true. We have already demonstrated that the base cases
+with $n = 0$ and $n = 1$ are true. These imply that the case with $n = 2$
+must also be true. Step by step, this leads to any $n$. Therefore, we can
+safely assert the truth in general, with all $n$.
+
+\end{document}
View
100 1.2/Ex1.14.scm
@@ -0,0 +1,100 @@
+(define (count-change amount)
+ (cc amount 5))
+
+;; Original version of cc with counter added
+(define (cc amount kinds-of-coins)
+ (set! counter (+ counter 1))
+ (cond ((= amount 0) 1)
+ ((or (< amount 0) (= kinds-of-coins 0)) 0)
+ (else (+ (cc amount
+ (- kinds-of-coins 1))
+ (cc (- amount
+ (first-denomination kinds-of-coins))
+ kinds-of-coins)))))
+
+;; Second version of cc:
+;; ---------------------
+;; condition
+;; ((= amount 0) 1)
+;; replaced by
+;; ((or (= amount 0) (= kinds-of-coins 1)) 1);
+;; this eliminates all subtrees with root node (cc amount 1)
+;; and reduces number of steps significantly
+(define (cc amount kinds-of-coins)
+ (set! counter (+ counter 1))
+ (cond ((or (= amount 0) (= kinds-of-coins 1)) 1)
+ ((or (< amount 0) (= kinds-of-coins 0)) 0)
+ (else (+ (cc amount
+ (- kinds-of-coins 1))
+ (cc (- amount
+ (first-denomination kinds-of-coins))
+ kinds-of-coins)))))
+
+;; Third version:
+;; --------------
+;; additional condition
+;; ((= kinds-of-coins 2) (+ 1 (floor (/ amount 5))))
+;; eliminates all subtrees with root node (cc amount 2)
+;; and reduces number of steps even further
+(define (cc amount kinds-of-coins)
+ (set! counter (+ counter 1))
+ (cond ((= kinds-of-coins 2) (+ 1 (quotient amount 5)))
+ ((or (= kinds-of-coins 1) (= amount 0)) 1)
+ ((or (< amount 0) (= kinds-of-coins 0)) 0)
+ (else (+ (cc amount
+ (- kinds-of-coins 1))
+ (cc (- amount
+ (first-denomination kinds-of-coins))
+ kinds-of-coins)))))
+
+(define (first-denomination kinds-of-coins)
+ (cond ((= kinds-of-coins 1) 1)
+ ((= kinds-of-coins 2) 5)
+ ((= kinds-of-coins 3) 10)
+ ((= kinds-of-coins 4) 25)
+ ((= kinds-of-coins 5) 50)))
+
+;; Experimental estimation of big-O in time
+
+;; counts the cc procedure calls
+(define counter 0)
+
+;; Base n logarithm
+(define (logn n x)
+ (/ (log x) (log n)))
+
+(define (estimation amount)
+ (begin
+ (set! counter 0)
+ (newline)
+ (letrec ((ways (count-change amount))
+ (c (logn amount counter)))
+ (display ways) (display " ways to make change for ")
+ (display amount) (display " cents") (newline)
+ (display "number of calls to cc: ") (display counter) (newline)
+ (display "the c in O(n^c): ") (display c) (newline))))
+
+; # of ways 3rd version 2nd version original version
+(estimation 11) ; 4 c -> 0.92 c -> 1.18 c -> 1.67
+(estimation 100) ; 292 0.96 1.39 2.10
+(estimation 200) ; 2435 1.14 1.60 2.33
+(estimation 500) ; 59576 1.35 1.88 2.63
+(estimation 1000) ; 801451 1.50 2.07 2.84
+(estimation 1500) ; 3820626 1.57 2.17
+(estimation 2000) ; 11712101 1.62 2.23
+(estimation 2500) ; 28070876 1.66 2.28
+(estimation 3000) ; 57491951 1.69
+(estimation 4000) ; 178901001 1.73
+(estimation 6000) ; 891646701 1.79
+(estimation 10000); 6794128501 1.86
+
+(time (estimation 12000))
+
+;; Bill the Lizard has a very good explanation of the time complexity here:
+;; http://www.billthelizard.com/2009/12/sicp-exercise-114-counting-change.html
+
+;; He shows that the number of steps grows as O(n⁵).
+
+;; I claim that the third version of cc above reduces it to O(n³).
+;; 55 steps to count ways of change to 11 cents is reduced to 9 steps!
+
Please sign in to comment.
Something went wrong with that request. Please try again.