Created
October 29, 2014 20:12
-
-
Save snahor/884e9f9d7324e34785b4 to your computer and use it in GitHub Desktop.
Pascal
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
def better_pascal(n): | |
def row(n): | |
row = [1] | |
for i in range(n): | |
row.append(row[i] * (n - i) // (i + 1)) | |
return row | |
return [row(i) for i in range(n)] |
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
def naive_pascal(n): | |
def mul(ns): | |
if not ns: | |
return 1 | |
return ns[0] * mul(ns[1:]) | |
def C(n, k): | |
return mul(range(n, n - k, -1)) // mul(range(1, k + 1)) | |
def row(n): | |
return [C(n, i) for i in range(n + 1)] | |
return [row(i) for i in range(n)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment