Skip to content

Instantly share code, notes, and snippets.

@jbochi
Forked from hltbra/pascal.py
Created November 21, 2012 23:01
Show Gist options
  • Save jbochi/4128413 to your computer and use it in GitHub Desktop.
Save jbochi/4128413 to your computer and use it in GitHub Desktop.
Pascal triangule using recursion
def print_pascal_triangle(n_lines):
for row in xrange(n_lines):
for col in xrange(row + 1):
print pascal(row, col),
print
def pascal(row, col):
if col == 0 or row == col:
return 1
else:
return pascal(row - 1, col) + pascal(row - 1, col - 1)
print_pascal_triangle(5)
# 1
# 1 1
# 1 2 1
# 1 3 3 1
# 1 4 6 4 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment