Created
October 25, 2009 10:13
-
-
Save yaotti/217989 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf8 | |
def comb(n, r): | |
"calc combination" | |
if n == 0 or r == 0: return 1 | |
# return (n - r + 1) / r * comb(n, r - 1) | |
# 先にrで割ると,小数点を切り捨ててしまう | |
return comb(n, r - 1) * (n - r + 1) / r | |
#print comb(10, 2) # 45 | |
def pascal(x): | |
import sys | |
print_no_space = sys.stdout.write | |
for n in xrange(0, x+1): | |
print_no_space(' ' * (x-n)) | |
for r in xrange(0, n+1): | |
print comb(n, r), | |
pascal(3) | |
# 1 | |
# 1 1 | |
# 1 2 1 | |
# 1 3 3 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment