Iteration and Invariants CoSc 450: Programming Paradigms 03 Tail - - PowerPoint PPT Presentation
Iteration and Invariants CoSc 450: Programming Paradigms 03 Tail - - PowerPoint PPT Presentation
CoSc 450: Programming Paradigms 03 Iteration and Invariants CoSc 450: Programming Paradigms 03 Tail recursion: What our author calls iteration is more commonly called tail recursion. A good optimizing compiler can convert a
CoSc 450: Programming Paradigms Tail recursion:
- What our author calls “iteration” is more
commonly called “tail recursion”.
- A good optimizing compiler can convert a tail
recursive program into an iterative one (as a loop). 03
CoSc 450: Programming Paradigms Tail recursion:
- What our author calls “iteration” is more
commonly called “tail recursion”.
- A good optimizing compiler can convert a tail
recursive program into an iterative one (as a loop). 03
CoSc 450: Programming Paradigms Tail recursion:
- What our author calls “iteration” is more
commonly called “tail recursion”.
- A good optimizing compiler can convert a tail
recursive program into an iterative one (as a loop). 03
CoSc 450: Programming Paradigms Not tail recursion: 03
CoSc 450: Programming Paradigms Not tail recursion: 03
n! = n ∗ (n − 1)!
CoSc 450: Programming Paradigms Not tail recursion: 03
n! = n ∗ (n − 1)!
CoSc 450: Programming Paradigms Not tail recursion: 03
n! = n ∗ (n − 1)!
Because after the recursive call additional processing must be done before the value is returned
CoSc 450: Programming Paradigms General idea:
- The function calls a helper function once.
- The helper function is tail recursive, and calls
itself.
- The helper function has an extra parameter.
- The extra processing is done in the parameters
- f the helper function.
03
CoSc 450: Programming Paradigms General idea:
- The function calls a helper function once.
- The helper function is tail recursive, and calls
itself.
- The helper function has an extra parameter.
- The extra processing is done in the parameters
- f the helper function.
03
CoSc 450: Programming Paradigms General idea:
- The function calls a helper function once.
- The helper function is tail recursive, and calls
itself.
- The helper function has an extra parameter.
- The extra processing is done in the parameters
- f the helper function.
03
CoSc 450: Programming Paradigms General idea:
- The function calls a helper function once.
- The helper function is tail recursive, and calls
itself.
- The helper function has an extra parameter.
- The extra processing is done in the parameters
- f the helper function.
03
CoSc 450: Programming Paradigms General idea:
- The function calls a helper function once.
- The helper function is tail recursive, and calls
itself.
- The helper function has an extra parameter.
- The extra processing is done in the parameters
- f the helper function.
03
CoSc 450: Programming Paradigms Not tail recursion: 03
n! = n ∗ (n − 1)!
CoSc 450: Programming Paradigms Not tail recursion: 03
n! = n ∗ (n − 1)!
Helper function, with two parameters a and b that computes a * b!
CoSc 450: Programming Paradigms Not tail recursion: 03
n! = n ∗ (n − 1)!
Helper function, with two parameters a and b that computes a * b!
a · b! = (a · b) · (b − 1)!
CoSc 450: Programming Paradigms Not tail recursion: 03
n! = n ∗ (n − 1)!
Helper function, with two parameters a and b that computes a * b!
a · b! = (a · b) · (b − 1)!
The main function calls the helper function with a value of one for a and n for b.
CoSc 450: Programming Paradigms Write factorial and factorial-product. 03
CoSc 450: Programming Paradigms Prove factorial-product is correct. 03
(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))
CoSc 450: Programming Paradigms 03
(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))
Base case Code inspection: (factorial-product a 0) returns a. Math: Therefore, correct in base case.
a · 0! = a
CoSc 450: Programming Paradigms 03
(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))
Base case Code inspection: (factorial-product a 0) returns a. Math: Therefore, correct in base case.
a · 0! = a
CoSc 450: Programming Paradigms 03
(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))
Base case Code inspection: (factorial-product a 0) returns a. Math: Therefore, correct in base case.
a · 0! = a
CoSc 450: Programming Paradigms 03
(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))
Base case Code inspection: (factorial-product a 0) returns a. Math: Therefore, correct in base case.
a · 0! = a
CoSc 450: Programming Paradigms 03
(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))
Inductive case Prove that (factorial-product x b) terminates with value assuming that (factorial-product y (- b 1)) terminates with value as the inductive hypothesis.
x · b! y · (b − 1)!
CoSc 450: Programming Paradigms 03
(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))
Inductive case Prove that (factorial-product x b) terminates with value assuming that (factorial-product y (- b 1)) terminates with value as the inductive hypothesis.
x · b! y · (b − 1)!
CoSc 450: Programming Paradigms 03
(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))
Inductive case Prove that (factorial-product x b) terminates with value assuming that (factorial-product y (- b 1)) terminates with value as the inductive hypothesis.
x · b! y · (b − 1)!
CoSc 450: Programming Paradigms 03
(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))
Inductive case
- =
- =
- (a·b)·(b−1)!
=
- a·b·(b−1)!
=
- a·b!
CoSc 450: Programming Paradigms 03
(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))
Inductive case
- =
- =
- (a·b)·(b−1)!
=
- a·b·(b−1)!
=
- a·b!
CoSc 450: Programming Paradigms 03
(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))
Inductive case
- =
- =
- (a·b)·(b−1)!
=
- a·b·(b−1)!
=
- a·b!
CoSc 450: Programming Paradigms 03
(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))
Inductive case
- =
- =
- (a·b)·(b−1)!
=
- a·b·(b−1)!
=
- a·b!
CoSc 450: Programming Paradigms 03
(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))
Inductive case
- =
- =
- (a·b)·(b−1)!
=
- a·b·(b−1)!
=
- a·b!
CoSc 450: Programming Paradigms 03
(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))
Inductive case
- =
- =
- (a·b)·(b−1)!
=
- a·b·(b−1)!
=
- a·b!
CoSc 450: Programming Paradigms 03
(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))
Inductive case
- =
- =
- (a·b)·(b−1)!
=
- a·b·(b−1)!
=
- a·b!
CoSc 450: Programming Paradigms 03
(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))
Inductive case
- =
- =
- (a·b)·(b−1)!
=
- a·b·(b−1)!
=
- a·b!
CoSc 450: Programming Paradigms 03
(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))
Inductive case
- =
- =
- (a·b)·(b−1)!
=
- a·b·(b−1)!
=
- a·b!
CoSc 450: Programming Paradigms 03
(define factorial-product (lambda (a b) ; Returns a*b!, b >= 0. (if (= b 0) a (factorial-product (* a b) (- b 1)))))
Inductive case
- =
- =
- (a·b)·(b−1)!
=
- a·b·(b−1)!
=
- a·b!
CoSc 450: Programming Paradigms 03 The power function
> (power 4 5) 1024 (power 4 5) returns 4 to the power 5.
CoSc 450: Programming Paradigms 03 The power function
> (power 4 5) 1024 (power 4 5) returns 4 to the power 5.
CoSc 450: Programming Paradigms 03 The power function
> (power 4 5) 1024 (power 4 5) returns 4 to the power 5.
CoSc 450: Programming Paradigms Not tail recursion: 03
be = b · be−1
CoSc 450: Programming Paradigms Not tail recursion: 03
be = b · be−1
Tail recursion:
(a) · be = (ab) · be−1
CoSc 450: Programming Paradigms Write power and power-product. 03
CoSc 450: Programming Paradigms 03 Exponentiation is not associative
(34)5 6= 3(45) 320 6= 31024
CoSc 450: Programming Paradigms 03 Exponentiation is not associative
(34)5 6= 3(45) 320 6= 31024
CoSc 450: Programming Paradigms 03 Fermat numbers
Fn = 2(2n) + 1
The first few Fermat numbers
n = 0 ⇒ 2 + 1 = 3 n = 1 ⇒ 22 + 1 = 5 n = 2 ⇒ (22)2 + 1 = 17 n = 3 ⇒ ((22)2)2 + 1 = 257
CoSc 450: Programming Paradigms 03 Fermat numbers
Fn = 2(2n) + 1
The first few Fermat numbers
n = 0 ⇒ 2 + 1 = 3 n = 1 ⇒ 22 + 1 = 5 n = 2 ⇒ (22)2 + 1 = 17 n = 3 ⇒ ((22)2)2 + 1 = 257
CoSc 450: Programming Paradigms 03 Fermat numbers
Fn = 2(2n) + 1
The first few Fermat numbers
n = 0 ⇒ 2 + 1 = 3 n = 1 ⇒ 22 + 1 = 5 n = 2 ⇒ (22)2 + 1 = 17 n = 3 ⇒ ((22)2)2 + 1 = 257
CoSc 450: Programming Paradigms 03 Fermat numbers
Fn = 2(2n) + 1
The first few Fermat numbers
n = 0 ⇒ 2 + 1 = 3 n = 1 ⇒ 22 + 1 = 5 n = 2 ⇒ (22)2 + 1 = 17 n = 3 ⇒ ((22)2)2 + 1 = 257
CoSc 450: Programming Paradigms 03 Fermat numbers
Fn = 2(2n) + 1
The first few Fermat numbers
n = 0 ⇒ 2 + 1 = 3 n = 1 ⇒ 22 + 1 = 5 n = 2 ⇒ (22)2 + 1 = 17 n = 3 ⇒ ((22)2)2 + 1 = 257
CoSc 450: Programming Paradigms 03 Fermat numbers
Fn = 2(2n) + 1
The first few Fermat numbers
n = 0 ⇒ 2 + 1 = 3 n = 1 ⇒ 22 + 1 = 5 n = 2 ⇒ (22)2 + 1 = 17 n = 3 ⇒ ((22)2)2 + 1 = 257
CoSc 450: Programming Paradigms 03 Fermat numbers
n = 3 ⇒ ((22)2)2 | {z } +1 = 257
2 repeatedly squared 3 times
(repeatedly-square 2 0) should return (repeatedly-square 2 1) should return (repeatedly-square 2 2) should return
2 22 = 4 (22)2 = 16
CoSc 450: Programming Paradigms 03 Fermat numbers
n = 3 ⇒ ((22)2)2 | {z } +1 = 257
2 repeatedly squared 3 times
(repeatedly-square 2 0) should return (repeatedly-square 2 1) should return (repeatedly-square 2 2) should return
2 22 = 4 (22)2 = 16
CoSc 450: Programming Paradigms 03 Fermat numbers
n = 3 ⇒ ((22)2)2 | {z } +1 = 257
2 repeatedly squared 3 times
(repeatedly-square 2 0) should return (repeatedly-square 2 1) should return (repeatedly-square 2 2) should return
2 22 = 4 (22)2 = 16
CoSc 450: Programming Paradigms 03 Fermat numbers
n = 3 ⇒ ((22)2)2 | {z } +1 = 257
2 repeatedly squared 3 times
(repeatedly-square 2 0) should return (repeatedly-square 2 1) should return (repeatedly-square 2 2) should return
2 22 = 4 (22)2 = 16
CoSc 450: Programming Paradigms 03 Fermat numbers
n = 3 ⇒ ((22)2)2 | {z } +1 = 257
2 repeatedly squared 3 times
(repeatedly-square 2 0) should return (repeatedly-square 2 1) should return (repeatedly-square 2 2) should return
2 22 = 4 (22)2 = 16
CoSc 450: Programming Paradigms 03 Fermat numbers
- b(2n)
= 2n = 2·2n−1 b2·(2n−1) = xy·z = (xy)z (b2)2n−1 b n b2 n−1
CoSc 450: Programming Paradigms 03 Fermat numbers
- b(2n)
= 2n = 2·2n−1 b2·(2n−1) = xy·z = (xy)z (b2)2n−1 b n b2 n−1
CoSc 450: Programming Paradigms 03 Fermat numbers
- b(2n)
= 2n = 2·2n−1 b2·(2n−1) = xy·z = (xy)z (b2)2n−1 b n b2 n−1
CoSc 450: Programming Paradigms 03 Fermat numbers
- b(2n)
= 2n = 2·2n−1 b2·(2n−1) = xy·z = (xy)z (b2)2n−1 b n b2 n−1
CoSc 450: Programming Paradigms 03 Fermat numbers
- b(2n)
= 2n = 2·2n−1 b2·(2n−1) = xy·z = (xy)z (b2)2n−1 b n b2 n−1
CoSc 450: Programming Paradigms 03 Fermat numbers
- b(2n)
= 2n = 2·2n−1 b2·(2n−1) = xy·z = (xy)z (b2)2n−1 b n b2 n−1
CoSc 450: Programming Paradigms Write fermat-number and repeatedly-square. 03
CoSc 450: Programming Paradigms 03 Perfect number
- The sum of its divisors is twice the number, or
- The number is equal to the sum of its divisors
- ther than itself.
CoSc 450: Programming Paradigms 03 Perfect number
- The sum of its divisors is twice the number, or
- The number is equal to the sum of its divisors
- ther than itself.
2·6 = 1+2+3+6 6 = 1+2+3
- ⇒
CoSc 450: Programming Paradigms 03 Perfect number
- The sum of its divisors is twice the number, or
- The number is equal to the sum of its divisors
- ther than itself.
2·6 = 1+2+3+6 6 = 1+2+3
- ⇒
There are no known odd perfect numbers.
CoSc 450: Programming Paradigms 03 Suppose you have a function sum-of-divisors such that (sum-of-divisors 4) returns 1+2+4, (sum-of-divisors 5) returns 1+5, (sum-of-divisors 6) returns 1+2+3+6, etc.
CoSc 450: Programming Paradigms 03 Suppose you have a function sum-of-divisors such that (sum-of-divisors 4) returns 1+2+4, (sum-of-divisors 5) returns 1+5, (sum-of-divisors 6) returns 1+2+3+6, etc. Write perfect? such that (perfect? 4) returns #f, (perfect? 5) returns #f, (perfect? 6) returns #t, etc.
CoSc 450: Programming Paradigms 03 The shape of sum-of-divisors
(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0)))
CoSc 450: Programming Paradigms 03 The shape of sum-of-divisors
(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0)))
sum-from-plus is defined
inside sum-of-divisors. So, it has access to n.
CoSc 450: Programming Paradigms 03 The shape of sum-of-divisors
(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0)))
sum-from-plus is defined
inside sum-of-divisors. So, it has access to n.
low addend
CoSc 450: Programming Paradigms 03 The shape of sum-of-divisors
(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0)))
sum-from-plus returns the sum of all the divisors
- f n that are greater than or equal to low
plus the addend.
low addend
CoSc 450: Programming Paradigms 03
sum-from-plus returns the sum of all the divisors
- f n that are greater than or equal to low
plus the addend.
CoSc 450: Programming Paradigms 03
sum-from-plus returns the sum of all the divisors
- f n that are greater than or equal to low
plus the addend. Is 12 perfect?
CoSc 450: Programming Paradigms 03
sum-from-plus returns the sum of all the divisors
- f n that are greater than or equal to low
plus the addend. Is 12 perfect? 1 2 3 4 5 6 7 8 9 10 11 12
CoSc 450: Programming Paradigms 03
sum-from-plus returns the sum of all the divisors
- f n that are greater than or equal to low
plus the addend. Is 12 perfect? 1 2 3 4 5 6 7 8 9 10 11 12
n = 12
CoSc 450: Programming Paradigms 03
sum-from-plus returns the sum of all the divisors
- f n that are greater than or equal to low
plus the addend. Is 12 perfect? 1 2 3 4 5 6 7 8 9 10 11 12
low = 6 n = 12
CoSc 450: Programming Paradigms 03
sum-from-plus returns the sum of all the divisors
- f n that are greater than or equal to low
plus the addend. Is 12 perfect? 1 2 3 4 5 6 7 8 9 10 11 12
addend = 1+2+3+4 low = 6 n = 12
CoSc 450: Programming Paradigms 03
sum-from-plus returns the sum of all the divisors
- f n that are greater than or equal to low
plus the addend. Is 12 perfect? 1 2 3 4 5 6 7 8 9 10 11 12
addend = 1+2+3+4 low = 6 n = 12 (sum-from-plus 6 10)
CoSc 450: Programming Paradigms 03
sum-of-divisors
(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend
CoSc 450: Programming Paradigms 03
(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend
sum-of-divisors
CoSc 450: Programming Paradigms 03
(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend
sum-of-divisors
CoSc 450: Programming Paradigms 03
(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend
sum-of-divisors
CoSc 450: Programming Paradigms 03
(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend
sum-of-divisors
CoSc 450: Programming Paradigms 03
(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend
sum-of-divisors
CoSc 450: Programming Paradigms 03
(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend
sum-of-divisors
CoSc 450: Programming Paradigms 03
(define sum-of-divisors (lambda (n) (define sum-from-plus ; sum of all divisors of n which are (lambda (low addend) ; >= low, plus addend (if (> low n) addend ; no divisors of n are greater than n (sum-from-plus (+ low 1) (if (divides? low n) (+ addend low) addend))))) (sum-from-plus 1 0))) low addend
sum-of-divisors
CoSc 450: Programming Paradigms 03 Hallmarks of pure functional programming
- A function returns a value.
- There is no call by reference.
- All parameters are called by value.
- There are no loops.
- Repetition is achieved by recursion.
- There is no assignment statement.
CoSc 450: Programming Paradigms 03 The Golden Ratio
CoSc 450: Programming Paradigms 03 The Golden Ratio
CoSc 450: Programming Paradigms 03 The Golden Ratio A A
CoSc 450: Programming Paradigms 03 The Golden Ratio A B A
CoSc 450: Programming Paradigms 03 The Golden Ratio A B A A B = A + B A = 1 + B A = 1 + 1 A/B φ = A/B φ = 1 + 1 φ
CoSc 450: Programming Paradigms 03 The Golden Ratio A B A A B = A + B A = 1 + B A = 1 + 1 A/B φ = A/B φ = 1 + 1 φ
CoSc 450: Programming Paradigms 03 The Golden Ratio A B A A B = A + B A = 1 + B A = 1 + 1 A/B φ = A/B φ = 1 + 1 φ
CoSc 450: Programming Paradigms 03 The Golden Ratio A B A A B = A + B A = 1 + B A = 1 + 1 A/B φ = A/B φ = 1 + 1 φ
CoSc 450: Programming Paradigms 03 The Golden Ratio A B A A B = A + B A = 1 + B A = 1 + 1 A/B φ = A/B φ = 1 + 1 φ
CoSc 450: Programming Paradigms 03 Successive approximations of the Golden Ratio
CoSc 450: Programming Paradigms 03 Successive approximations of the Golden Ratio
φ0 = 1 φ1 = 1 + 1 φ0 = 2 φ2 = 1 + 1 φ1 = 3 2 φ3 = 1 + 1 φ2 = 5 3 φ4 = 1 + 1 φ3 = 8 5
CoSc 450: Programming Paradigms 03 Successive approximations of the Golden Ratio
φ0 = 1 φ1 = 1 + 1 φ0 = 2 φ2 = 1 + 1 φ1 = 3 2 φ3 = 1 + 1 φ2 = 5 3 φ4 = 1 + 1 φ3 = 8 5
CoSc 450: Programming Paradigms 03 Successive approximations of the Golden Ratio
φ0 = 1 φ1 = 1 + 1 φ0 = 2 φ2 = 1 + 1 φ1 = 3 2 φ3 = 1 + 1 φ2 = 5 3 φ4 = 1 + 1 φ3 = 8 5
CoSc 450: Programming Paradigms 03 Successive approximations of the Golden Ratio
φ0 = 1 φ1 = 1 + 1 φ0 = 2 φ2 = 1 + 1 φ1 = 3 2 φ3 = 1 + 1 φ2 = 5 3 φ4 = 1 + 1 φ3 = 8 5
CoSc 450: Programming Paradigms 03 Successive approximations of the Golden Ratio
φ0 = 1 φ1 = 1 + 1 φ0 = 2 φ2 = 1 + 1 φ1 = 3 2 φ3 = 1 + 1 φ2 = 5 3 φ4 = 1 + 1 φ3 = 8 5
CoSc 450: Programming Paradigms 03 Successive approximations of the Golden Ratio (phi 0) returns 1, (phi 1) returns 2, (phi 2) returns 3/2, (phi 3) returns 5/3, (phi 4) returns 8/5, etc. Write phi
CoSc 450: Programming Paradigms 03 The Josephus Problem Rule: Everybody stands in a circle. Starting with the first, kill every third person. Which two people remain alive?
CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6
CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6
CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6
CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6
CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6
CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6
CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6
CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6
(survives? 4 8) returns #t (survives? 7 8) returns #t (survives? k 8) returns #f
for all other values of k
CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6
(survives? 4 8) returns #t (survives? 7 8) returns #t (survives? k 8) returns #f
for all other values of k
CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6
(survives? 4 8) returns #t (survives? 7 8) returns #t (survives? k 8) returns #f
for all other values of k
CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6
(survives? 4 8) returns #t (survives? 7 8) returns #t (survives? k 8) returns #f
for all other values of k
Exercise for the student: Write survives?
CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6
Hint: Parentheses are the new numbers after the first person is killed.
(survives? 6 8)
is recursively the same as
(survives? 3 7)
(1) (2) (3) (4) (5) (6) (7)
CoSc 450: Programming Paradigms 03 The Josephus Problem 1 5 7 3 2 4 8 6
Hint: Parentheses are the new numbers after the first person is killed.
(survives? 6 8)
is recursively the same as
(survives? 3 7)