Skip to content

Instantly share code, notes, and snippets.

@neatnick
Created May 5, 2015 17:43
Show Gist options
  • Save neatnick/eab60865f56ff6d0b428 to your computer and use it in GitHub Desktop.
Save neatnick/eab60865f56ff6d0b428 to your computer and use it in GitHub Desktop.
Python script to generate Pascal's Triangle
from math import ceil, floor, log10
def pascal(rows, p_row):
print(" "*rows+ " ".join([" "*(3-floor(log10(x))) + str(x) for x in p_row]))
if rows:
p_row.append(1)
for i in reversed(range(ceil(len(p_row)/2)-1)):
p_row[i+1] += p_row[i]
p_row[-(i+2)] = p_row[i+1]
pascal(rows-1, p_row)
pascal(10, [1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment