Skip to content

Instantly share code, notes, and snippets.

@yaotti
Created October 25, 2009 10:13
Show Gist options
  • Save yaotti/217989 to your computer and use it in GitHub Desktop.
Save yaotti/217989 to your computer and use it in GitHub Desktop.
#!/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),
print
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