Created
November 6, 2017 09:09
-
-
Save nicholaskajoh/da12b51e90a0822cef08408c8bc77375 to your computer and use it in GitHub Desktop.
Python function to generate n rows of a Tribonacci 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
def tribonacci_triangle(n): | |
# n <- number of rows to generate | |
# a <- tribonacci triangle | |
a = [[1], [1, 1]] | |
if n == 1: | |
return [[1]] | |
elif n == 2: | |
return a | |
else: | |
# generate rows 3 to n | |
for i in range(3, n + 1): | |
# generate row n elements | |
# b <- row n | |
b = [1] | |
for j in range(1, i - 1): | |
b.append(a[i - 3][j - 1] + a[i - 2][j - 1] + a[i - 2][j]) | |
b.append(1) | |
a.append(b) | |
b = [1] | |
return a | |
print(tribonacci_triangle(7)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment