Created
May 5, 2015 17:43
-
-
Save neatnick/eab60865f56ff6d0b428 to your computer and use it in GitHub Desktop.
Python script to generate Pascal's Triangle
This file contains 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
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