Skip to content

Instantly share code, notes, and snippets.

@fgshun
Created October 7, 2022 11:13
Show Gist options
  • Save fgshun/10b00f6544e7d9ceed60494c44d49da5 to your computer and use it in GitHub Desktop.
Save fgshun/10b00f6544e7d9ceed60494c44d49da5 to your computer and use it in GitHub Desktop.
Pascal's triangle
class PascalTriangle:
def __init__(self, n):
t = [[1.0]]
for i in range(2, n + 1):
row = t[i - 2]
temp = [1.0]
for j in range(i - 2):
# temp.append(t[i - 2][j] + t[i - 2][j + 1])
# temp.append(sum(t[i - 2][j:j + 2]))
temp.append(sum(row[j:j + 2]))
temp.append(1.0)
t.append(temp)
self.triangle = t
def comb(self, n, m):
return self.triangle[n][m]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment