CSE390D Notes for Wednesday, 10/23/24
section 5.3, recursive definitions
functions can be defined recursively:
1) base case
2) recursive case
examples:
f(0) = 1
f(n + 1) = f(n) + 2
recursive definition of subtraction building on addition
f(0) = 1
f(n + 1) = f(n) * 2
recursive definition of exponentiation building on multiplication
f(0) = 1
f(n + 1) = 2 ^ f(n)
recursive definition of tetration building on exponentiation
Knuth's ^^ operator
----------------------------------------------------------------------
Fibonacci sequence
fib(0) = fib(1) = 1
fib(n) = fib(n - 1) + fib(n - 2)
Turing was fascinated with fibonacci numbers in nature (rows of
sunflower seeds)
Python exploration:
from math import *
phi = (1 + sqrt(5))/2
phip = (1 - sqrt(5))/2
phi**2
1/phi
for n in range(20):
print n, phi**n, phi**(n-1)+phi**(n-2)
for n in range(20):
print n, (phi**(n+1)-phip**(n+1))/sqrt(5)
def fib(n):
return (phi**(n+1) + phip**(n+1))/sqrt(5)
----------------------------------------------------------------------
bounding fib
Prove: 2^(n/2) <= fib(n) <= 2^n for n >= 2
prove by strong induction
P(n): 2^(n/2) <= fib(n) <= 2^n for integers n >= 2
basis step:
prove P(2)
fib(2) = 2
2 <= 4 = 2^2
2 >= 2^1 = 2^(2/2)
thus, 2^(2/2) <= fib(2) <= 2^2
prove P(3)
fib(3) = 3
3 <= 8 = 2^3
3 = 2 * 1.5 >= 2 * 2^0.5 = 2^(3/2) (because sqrt(2) < 1.5)
thus, 2^(3/2) <= fib(3) <= 2^3
inductive step:
prove that P(2) ^ P(3) ^ ... P(k) -> P(k+1)
P(k+1): 2^((k+1)/2) <= fib(k+1) <= 2^(k+1) for integers k >= 2
P(k): 2^(k/2) <= fib(k) <= 2^k for integers k >= 2
P(k-1): 2^((k-1)/2) <= fib(k-1) <= 2^(k-1) for integers k >= 2
first prove fib(k+1) <= 2^(k+1)
fib(k+1) = fib(k-1) + fib(k)
applying i.h.:
<= 2^(k-1) + 2^k
because k is a positive integer:
<= 2^k + 2^k
= 2^k+1
then prove fib(k+1) >= 2^((k+1)/2)
fib(k+1) = fib(k-1) + fib(k)
applying i.h.:
>= 2^((k-1)/2) + 2^(k/2)
because k is a positive integer:
>= 2^((k-1)/2) + 2^((k-1)/2)
= 2 * 2^((k-1)/2)
= 2^(1 + (k-1)/2)
= 2^(2/2 + (k-1)/2)
= 2^((2 + k - 1)/2)
= 2^(k+1)/2
----------------------------------------------------------------------
Given this definition of fib:
def fib(n):
if n <= 1:
return 1
else:
return fib(n - 1) + fib(n - 2)
Define calls(n), the number of calls made to compute fib(n)
calls(0) = calls(1) = 1
calls(n) = 1 + calls(n-1) + calls(n-2)
this is a slight variation of fib
----------------------------------------------------------------------
Consider the subset S of the set of integers recursively defined by:
basis step: 3 in S
recursive step: if x in S and y in S, then x + y in S
This is the set of positive multiples of 3
Given an alphabet sig, define sig* (the set of strings over the
alphabet):
basis step: lambda is in sig* (where lambda is the empty string)
recursive step: wx is in sig* whenever w is in sig* and x is in
sig
wff's: define some subset of wff's:
basis step: x is a wff if it is a number or variable
recursive step: for F and G in wff, all of these are wffs:
(F + G)
(F - G)
(F * G)
(F / G)
(F ^ G)
Stuart Reges
Last modified: Wed Oct 23 13:08:02 PDT 2024