帕斯卡三角是法国数学家帕斯卡(1623—1662)发现的。中国南宋时期的杨辉也发现了它并且比西方的数学家早,在中国也叫杨辉三角。
def pascal():
row = [1]
while True:
yield row
this = [0] + row
next = row + [0]
row = [a+b for a,b in zip(this, next)]
gen=pascal()
for i in range(10):
print gen.next()
-----------------------------------------------
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
阅读(1878) | 评论(0) | 转发(0) |