The primary goal of this course is to become familiar with some of the basic mathematical ideas used in programming.
Homework 4
p 108: 3abc. Also give the total count. 3c is not a stars-and-bars, but would be if the letters had to be in alphabetical order.
p 144: 2, 4, 5
1.6.4: Derangements
-------------------
Tower of Hanoi
Factorials, triangle numbers
Recursively defined sequences
Example 2.1.2
Python example like 2.1.2 third sequence (but note differences); compare to sixth sequence
import math
phi=(1+math.sqrt(5))/2
pho=(1-math.sqrt(5))/2
def s(n):
return (pow(phi,n)-pow(pho,n))/math.sqrt(5)
for i in range(20):
print(i,'\t',s(i))
Recursive (or inductive) definitions
Example 2.1.3
def a(n): if n==0: return 3
if n==1: return 4
return 2*a(n-1) - a(n-2)
But there is a much simpler way to calculate a(n)!
p 140:
Example 2.1.4: first step is to identify the patterns
2.1.5: summation notation
2.1.6: more summation
p 148: Investigate!
Example 2.2.1: arithmetic sequences
Example 2.2.2: geometric sequences
Sums of arithmetic and geometric sequences
1 + 2 + 3 + ... + 100
Example 2.2.4
Example 2.2.5
Example 2.2.6: fix
1+2+4+...+512
Example 2.2.7
Example 2.2.8
Example 2.2.9
S = 0.464646.... When we subtract from 100S, we get +46 on the right, but there is no minus term. Why?
Friday:
mathwithbaddrawings.com/2017/02/08/how-to-tell-a-mathematician-you-love-them
Example 2.2.6 on Levin p 154
Let bk = 1+3k. Then the sum
an = 2 + 1 + 4 + 7 + 10 + · · · + (1 + 3(n−1))
can be rewritten as
an =2 + b0 + b1 + ... + bn-1.
Written this way it is clearer that there are n terms of the form bk here, ranging from 0 to (n-1).
Homework 4 questions
Proof by induction: 1 + 2 + ... + N = N(N+1)/2
Find 12 + 22 + 32 + ... + N2.
Investigate!, page 167
def a(n):
if n== 0: return 1
if n==1: return 2
return 5*a(n-1) - 6*a(n-2)
def b(n):
if n== 0: return 1
if n==1: return 3
return 5*b(n-1) - 6*b(n-2)
def c(n):
if n== 0: return 1
if n==1: return 4
return 5*c(n-1) - 6*c(n-2)
Example 2.4.2, p 168
Example 2.4.3: an = an-1 + n
Example 2.4.4: same problem, alternative approach
Example 2.4.5: introduce a factor: an = 3*an-1 + 2. Make a guess?
def a(n):
if n== 0: return 1
return 3*a(n-1) + 2
Characteristic roots
Example 2.4.6
Example at start of chapter: an = 5*an-1 - 6*an-2
Fibonacci example
Postage in Investigate!
Example 2.5.1: triangle numbers
Example 2.5.2: 6n-1 is divisible by 5
Example 2.5.3: n2 < 2n for n>=5
Warning: Canadians
Euclidean algorithm theorem: for integers a and b<a, we can find q and r so a=qb+r