[Edu-sig] Pascal's Triangle (in 2.6)
Edward Cherlin
echerlin at gmail.com
Sat Jan 30 20:21:46 CET 2010
Pascal's Triangle mod 2 is also a Sierpinski gasket fractal. This is
one of the Python examples in Pippy in the Sugar education software.
# Sierpinski triangles
import sys
size = 3
modulus = 2
lines = modulus**size
vector = [1]
for i in range(1,lines+1):
vector.insert(0,0)
vector.append(0)
for i in range(0,lines):
newvector = vector[:]
for j in range(0,len(vector)-1):
if (newvector[j] == 0):
print " ",
else:
remainder = newvector[j] % modulus
if (remainder == 0):
print "O",
else:
print ".",
newvector[j] = vector[j-1] + vector[j+1]
print
vector = newvector[:]
On Sat, Jan 30, 2010 at 12:23, kirby urner <kirby.urner at gmail.com> wrote:
This process below is how I learned Pascal's Triangle from my mother
when I was 11.
> """
> Rows of Pascal's Triangle
>
> See:
> http://en.wikipedia.org/wiki/Pascal%27s_triangle
> "Calculating an individual row"
>
> Consider a row starting as follows:
> 1, 12...
>
> Initialize to [1] and multiply by (row_num/1)
> to get the next term, row[1].
>
> Then decrement and increment again, getting
> (11 / 2), (10 / 3), (9 / 4) and so forth, and multiply
> by the last term so far. Stop when the numerator
> is 0.
>
> 1 * (12/1) = 12
> 12 * (11 / 2) = 66
> 66 * (10 / 3) = 220
> 220 * (9 / 4) = 495
>
> etc.
>
> This is another way of computing successive values
> of C(n, k) without using a factorial function and
> dividing.
>
> Independently discovered by David Koski,
> implemented in Python by Kirby Urner
>
> """
>
> def pascal_row(row_num):
> numer = row_num
> denom = 1
> # initialize row of Pascal's Triangle
> row = [1]
> while numer > 0:
> row.append((row[-1] * numer/denom))
> numer -= 1 # decrement numerator
> denom += 1 # increment denominator
> return row
>
> def pascal_mod2(row_num = 0):
> """
> row integers mod 2, give a binary string which
> corresponds to Rule 60 in the Wolfram categorization
> scheme for cellular automata
>
> http://www.research.att.com/~njas/sequences/A001317
> """
> while True:
> therow = pascal_row(row_num)
> binary = "".join(str(i % 2) for i in therow)
> yield [int(binary,2), binary]
> row_num += 1
>
> """
> traditional generator for successive rows, included
> for completeness
> """
>
> def pascal_gen():
> row = [1]
> while True:
> yield row
> row = [i + j for i,j in zip(row + [0], [0] + row)]
> _______________________________________________
> Edu-sig mailing list
> Edu-sig at python.org
> http://mail.python.org/mailman/listinfo/edu-sig
>
--
Edward Mokurai (默雷/धर्ममेघशब्दगर्ज/دھرممیگھشبدگر ج) Cherlin
Silent Thunder is my name, and Children are my nation.
The Cosmos is my dwelling place, the Truth my destination.
http://www.earthtreasury.org/
More information about the Edu-sig
mailing list