Class 17: Golden Sneezewort

Preview:

DESCRIPTION

Generalizing LoopsFibonacciMeasuring CostGolden Ratio

Citation preview

Class 17: Golden Sneezewort

cs1120 Fall 2011David Evans30 September 2011

2

Plan

Generalizing Loops

Introducing CostBook: Chapter 7 and 8

(okay to read after Exam 1)Not covered on Exam 1

3

Recap/Wake-up: A General Loop

(define (loop index result test update proc)

4

General Loop with Result

(define (loop index result test update proc) (if (test index) (loop (update index) (proc index result) test update proc) result))

5

Using Loop

(loop 1 0 (lambda (i) (<= i n)) (lambda (i) (+ i 1)) (lambda (i res) (+ res i)))

(define (gauss-sum n)

)

(define (loop index result test update proc) (if (test index) (loop (update index) (proc index result) test update proc) result))

6

(define (factorial n)

(define (loop index result test update proc) (if (test index) (loop (update index) (proc index result) test update proc) result))

7

(define (list-length p)

(define (loop index result test update proc) (if (test index) (loop (update index) (proc index result) test update proc) result))

8

(define (loop index result test update proc) (if (test index) (loop (update index) (proc index result) test update proc) result))

(define (list-accumulate f base p) (if (null? p) base (f (car p) (list-accumulate f base (cdr p))))

9

(define (loop index result test update proc) (if (test index) (loop (update index) (proc index result) test update proc) result))

(define (list-accumulate f base p) (if (null? p) base (f (car p) (list-accumulate f base (cdr p))))

(define (list-accumulate f base p) (loop p base not-null? cdr (lambda (p res) (f (car p) res))))

10

Challenge Problem

Define an efficient edit-distance procedure (as defined in PS2) using loop (without using memoization).

First good answer (includes expected presentation) is worth an exemption (full credit) from Exam 1!

11

Sneezewort!

12

Sneezewort Growth

First Time Unit Second Time Unit Offshoot

Could we model Sneezewort with PS3 code?

13

Sneezewort Numbers

1

1

2

3

5

8?

13

14

Page fromLiber abbaci,Leonardo da Pisa (1202)

15

Fibonacci’s ProblemSuppose a newly-born pair of rabbits, one male, one female, are put in a field. Rabbits mate at the age of one month so that at the end of its second month a female can produce another pair of rabbits.

Suppose that our rabbits never die and that the female always produces one new pair (one male, one female) every month from the second month on.

How many pairs will there be in one year? Filius Bonacci, 1202 (Pisa)Liber Abaci (“Book of Calculation”)

16

Rabbits

From http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/fibnat.html

17

Fibonacci NumbersGEB p. 136:

These numbers are best defined recursively by the pair of formulas

FIBO (n) = FIBO (n – 1) + FIBO (n – 2) for n > 2

FIBO (1) = FIBO (2) = 1Can we turn this into a Scheme procedure?

18

Defining FIBOThese numbers defined recursively by:FIBO (n) = FIBO (n – 1) + FIBO (n – 2)

for n > 1FIBO (1) = 1FIBO (0) = 0

19

Defining fibo

• (define (fibo n)• (if (= n 0) 0• (if (= n 1) 1• (+ (fibo (- n 1)) • (fibo (- n 2))))))

> (fibo 1)1> (fibo 2)2> (fibo 3)3> (fibo 10)55

20

Defining fibo with loop?(define (loop index result test update proc) (if (test index) (loop (update index) (proc index result) test update proc) result))

21

(define (fibo-loop n) ; doesn't work for n=0 (cdr (loop 1 (cons 0 1) (lambda (i) (< i n)) inc (lambda (i v) (cons (cdr v) (+ (car v) (cdr v)))))))

22

Which is better?

(define (fibo-loop n) ; doesn't work for n=0 (cdr (loop 1 (cons 0 1) (lambda (i) (< i n)) inc (lambda (i v) (cons (cdr v) (+ (car v) (cdr v)))))))

(define (fibo-rec n) (if (= n 0) 0 (if (= n 1) 1 (+ (fibo-rec (- n 1)) (fibo-rec (- n 2))))))

23

Fibo Results> (fibo-rec 2)1> (fibo-rec 3)2> (fibo-rec 4)3> (fibo-rec 10)55> (fibo-rec 60)Still working…

24

Tracing Fibo> (require racket/trace)> (trace fibo-rec)> (fibo-rec 3)|(fibo-rec 3)| (fibo-rec 2)| 1| (fibo-rec 1)| 1|22

> (fibo-loop 3)>(fibo-loop 3)> (loop 1 '(0 . 1) #<procedure> #<procedure:inc> #<procedure>)> (loop 2 '(1 . 1) #<procedure> #<procedure:inc> #<procedure>)> (loop 3 '(1 . 2) #<procedure> #<procedure:inc> #<procedure>)< '(1 . 2)<22

25

> (fibo-rec 4)>(fibo-rec 4)> (fibo-rec 3)> >(fibo-rec 2)> > (fibo-rec 1)< < 1> > (fibo-rec 0)< < 0< <1> >(fibo-rec 1)< <1< 2> (fibo-rec 2)> >(fibo-rec 1)< <1> >(fibo-rec 0)< <0< 1<33

To compute (fibo 4) we calculated:(fibo 3) 1 times(fibo 2) 2 times(fibo 1) 3 times(fibo 0) 3 times

= 3+6 calls to fibo

How many calls to calculate (fibo 60)?

26

> (fibo-rec 5)>(fibo-rec 5)> (fibo-rec 4)> >(fibo-rec 3)> > (fibo-rec 2)> > >(fibo-rec 1)< < <1> > >(fibo-rec 0)< < <0< < 1> > (fibo-rec 1)< < 1< <2> >(fibo-rec 2)> > (fibo-rec 1)< < 1> > (fibo-rec 0)< < 0< <1< 3> (fibo-rec 3)> >(fibo-rec 2)> > (fibo-rec 1)< < 1> > (fibo-rec 0)< < 0< <1> >(fibo-rec 1)< <1< 2<55

To calculate (fibo 5) we calculated:(fibo 4) 1 time(fibo 3) 2 times(fibo 2) 3 times(fibo 1/0) 8 times

How many calls to calculate (fibo 60)?

27

Fast-Fibo Results

> (time (fibo-loop 61))cpu time: 0 real time: 0 gc time: 02504730781961

2.5 Trillion applications2.5 GHz computer does 2.5 Billion simple operations per second, so 2.5 Trillion applications operations take ~1000 seconds. Each application of fibo involves hundreds of simple operations…

28

Beware the Bunnies!!

;;; The Earth's mass is 6.0 x 10^24 kg> (define mass-of-earth (* 6 (expt 10 24))) ;;; A typical rabbit's mass is 2.5 kilograms> (define mass-of-rabbit 2.5)> (/ (* mass-of-rabbit (fibo-loop 60)) mass-of-earth)6.450036483e-013> (/ (* mass-of-rabbit (fibo-loop 120)) mass-of-earth)2.2326496895795693

According to Bonacci’s model, after less than 10 years, rabbits would out-weigh the Earth!

Beware the Sneezewort!!

29

The Verge of Survivalby Filip and Colton

Evaluation CostActual running times vary according to:– How fast a processor

you have– How much memory you

have– Where data is located in

memory– How hot it is– What else is running– etc...

How does time scale with the size of the input?

If the input size increases by one, how much longer will it take?

If the input size doubles, how much longer will it take?

Running Time of fibo-rec

Fibonacci (n) = Fibonacci(n-2) + Fibonacci(n-1) = X Fibonacci(n-1)

= Xn

What is X?> (exact->inexact (/ (fibo-loop 2) (fibo-loop 1)))1.0> (exact->inexact (/ (fibo-loop 3) (fibo-loop 2)))2.0> (exact->inexact (/ (fibo-loop 4) (fibo-loop 3)))1.5

> (for 1 50 (lambda (i) (printf "~a~n" (exact->inexact (/ (fibo-loop (+ i 1)) (fibo-loop i))))))1.02.01.51.66666666666666671.61.6251.61538461538461541.6190476190476191.61764705882352941.61818181818181821.61797752808988761.61805555555555561.61802575107296141.61803713527851461.6180327868852461.6180344478216821.6180338134001253…1.618033988749895

Running Time of fibo-rec

Φ = (/ (+ 1 (sqrt 5)) 2) “The Golden Ratio” 1.618033988749895... > (exact->inexact (/ (fibo-loop 41) (fibo-loop 40)))1.618033988749895

Fibonacci (n) = Fibonacci(n-2) + Fibonacci(n-1) = ? Fibonacci(n-1)

= Φn

“Golden” Rotunda?

35

The Golden Ratio

Parthenon

Nautilus Shell

Euclid: “extreme and mean ratio”

http://www.mathsisfun.com/numbers/golden-ratio.html

36

Charge

PS4 due Monday 3 OctoberExam 1: out October 7, due October 12

Covers: Problem Sets 1-4 including PS CommentsCourse Book Chapters 1-6Classes 1-18

Reading: no reading assignment until after Exam 1, but I will start covering things in Chapter 7-8 soon and encourage you to read The Information, Chapters 5-7

Recommended