some more integer sequences to remember
This commit is contained in:
parent
5c811280d7
commit
4a2b99f8f2
@ -1,5 +1,30 @@
|
||||
import math
|
||||
from functools import cache
|
||||
|
||||
|
||||
def factorial(n: int) -> int:
|
||||
"""
|
||||
n! = 1 * 2 * 3 * 4 * ... * n
|
||||
1, 1, 2, 6, 24, 120, 720, ...
|
||||
"""
|
||||
return math.factorial(n)
|
||||
|
||||
|
||||
@cache
|
||||
def fibonacci(n: int) -> int:
|
||||
"""
|
||||
F(n) = F(n-1) + F(n-2) with F(0) = 0 and F(1) = 1
|
||||
0, 1, 1, 2, 3, 5, 8, 13, 21, ...
|
||||
"""
|
||||
if n < 2:
|
||||
return n
|
||||
|
||||
return fibonacci(n - 1) + fibonacci(n - 2)
|
||||
|
||||
|
||||
def triangular(n: int) -> int:
|
||||
"""
|
||||
a(n) = binomial(n+1,2) = n*(n+1)/2 = 0 + 1 + 2 + ... + n
|
||||
0, 1, 3, 6, 10, 15, ...
|
||||
"""
|
||||
return int(n * (n + 1) / 2)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user