-
Notifications
You must be signed in to change notification settings - Fork 0
/
Schroeder.py
46 lines (35 loc) · 971 Bytes
/
Schroeder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from functools import cache
from _tabltypes import MakeTriangle
"""Schroeder triangle.
[0] [1]
[1] [0, 1]
[2] [0, 2, 1]
[3] [0, 6, 4, 1]
[4] [0, 22, 16, 6, 1]
[5] [0, 90, 68, 30, 8, 1]
[6] [0, 394, 304, 146, 48, 10, 1]
[7] [0, 1806, 1412, 714, 264, 70, 12, 1]
[8] [0, 8558, 6752, 3534, 1408, 430, 96, 14, 1]
[9] [0, 41586, 33028, 17718, 7432, 2490, 652, 126, 16, 1]
"""
@cache
def schroeder(n: int) -> list[int]:
if n == 0:
return [1]
if n == 1:
return [0, 1]
row = schroeder(n - 1) + [1]
for k in range(n - 1, 0, -1):
row[k] += row[k - 1] + row[k + 1]
return row
@MakeTriangle(
schroeder,
"Schroeder",
["A122538", "A033877", "A080245", "A080247", "A106579"],
True,
)
def Schroeder(n: int, k: int) -> int:
return schroeder(n)[k]
if __name__ == "__main__":
from _tabltest import TablTest
TablTest(Schroeder)