[Edu-sig] Sample Lesson Plan
Kirby Urner
pdx4d@teleport.com
Fri, 04 Feb 2000 01:15:13 -0800
Re recent posts, my operating assumption is there's no time
to change the language. Case sensitivity and the interface
are just the way they are.
That doesn't mean others need to operate on this assumption,
plus I realize Python is evolving. But I want to brainstorm
about how to use Python in K-12 right now, without requiring
that any "wish list" be fulfilled. I take it "as is".
Here's an example of a lesson plan which uses Python in context,
to generate triangular, square, tetrahedral, and half-octahedral
numbers.
In this example, there's no graphical output. However, a teacher
of this lesson might well have graphics to share, plus hands-on
exercises with actual ping pong ball or other spheres
(e.g. marbles).
Kirby
================================================
Triangular numbers represent the sum of consecutive integers,
as per this picture:
* 1
* * 2
* * * + 3
---
6
The 3rd triangular number is the sum of the numbers 1,2 and
3. So if we think of "row number", we could write:
tri(1) = 1 # = 1
tri(2) = 3 # = 1 + 2
tri(3) = 6 # = 1 + 2 + 3
...
The notion is similar to 'factorial' (n!), except instead
of doing 1 x 2 x 3..., we're doing 1 + 2 + 3...
Just as factorial may be written recursively (as in
"a function that calls itself"), so may we write the 'tri'
function:
>>> def tri(n):
if n<=1: return n
else: return n + tri(n-1)
>>> tri(1)
1
>>> tri(2)
3
>>> tri(3)
6
>>> tri(4)
10
>>> tri(10)
55
Notice that a parallelogram of stars may be composed of two
consecutive triangular numbers, e.g.
* * * * * * * *
* * * * * * * *
* * * * = * * * + *
* * * * * * * *
= tri(4) + tri(3)
And a parallelogram of stars easily transforms to a square:
* * * * * * * *
* * * * * * * *
* * * * = * * * *
* * * * * * * *
sqr(4)
In other words, a "square number" is the sum of two consecutive
triangular numbers. Let's see this in code:
>>> def sqr(n):
return tri(n) + tri(n-1)
>>> sqr(1)
1
>>> sqr(2)
4
>>> sqr(3)
9
>>> sqr(4)
16
>>> sqr(20)
400
Imagine triangles stacked on top of each other, with 1 at
the apex, 3 under it, 6 under that and so on. This is a
tetrahedron with an expanding triangular base. You'd get
the number of spheres in a tetrahedral packing (imagine
stacked ping pong balls) by summing consecutive tri numbers.
Here's one way to write the tetra() function:
>>> def tetra(n):
sum = 0
for i in range(n+1)[1:]:
sum = sum + tri(i)
return sum
>>> tetra(1)
1
>>> tetra(2)
4
>>> tetra(3)
10
>>> tetra(4)
20
>>> tetra(5)
35
>>> tetra(100)
171700
Notice that we want to force a loop that goes from 1 to n.
The range() function goes from 0 to n-1, so we have to bump
up the highest outcome by 1, and start the series with
term 1 (instead of term 0).
Let's see this just using range(n):
>>> range(5)
[0, 1, 2, 3, 4]
>>> range(5+1)[1:]
[1, 2, 3, 4, 5]
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(10+1)[1:]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
So you see that range(n+1)[1:] gives us the numbers 1 thru n.
That's what we're doing in the tetrahedral numbers function.
Instead of stacking bigger and bigger triangles, we could
stack bigger and bigger squares. 1 ball would sit on top of 4,
on top of 9, on top of 25 and so on. Here's the function,
which we'll call 'hoct' for "half octahedron" (the shape of
a square-based pyramid):
>>> def hoct(n):
sum = 0
for i in range(n+1)[1:]:
sum = sum + sqr(i)
return sum
>>> hoct(1)
1
>>> hoct(2)
5
>>> hoct(3)
14
>>> hoct(4)
30
>>> hoct(100)
338350
[ to be continued ]
Background reading:
http://www.teleport.com/~pdx4d/sphpack.html