Pascal's triangle

This commit is contained in:
newt 2024-10-09 18:02:36 +01:00
parent 6a7111706e
commit efd0249cb6

View file

@ -0,0 +1,18 @@
def pascal(rowCount):
rows = [[1]]
for _ in range(rowCount):
previousRow = rows[-1]
newRow = [1] # starts with a 1
for j in range(len(previousRow) - 1):
newRow.append(previousRow[j] + previousRow[j + 1])
newRow.append(1) # ends with a 1
rows.append(newRow)
return rows
def nthRow(n):
rows = pascal(n)
return rows[n]
print(nthRow(10))