文章目录
- fibonacci
- tribonacci
- 对比
fibonacci
斐波那契数列的递推公式大家都很熟悉了,是 F n = F n − 1 + F n − 2 F_n=F_{n-1}+F_{n-2} Fn=Fn−1+Fn−2,其在复数域的的解析延拓可表示为
F z = ϕ z − cos ( π z ) ϕ − z 5 , ϕ = 5 − 1 2 F_z=\frac{\phi^z-\cos(\pi z)\phi^{-z}}{\sqrt{5}}, \phi=\frac{\sqrt{5}-1}{2} Fz=5ϕz−cos(πz)ϕ−z,ϕ=25−1
根据斐波那契数列可构造斐波那契多项式,其递推公式为
F 1 ( x ) = 1 , F 2 ( x ) = x F n ( x ) = x F n − 1 ( x ) + F n − 2 ( x ) \begin{aligned} F_1(x)&=1, \quad F_2(x)=x\\ F_n(x)&=xF_{n-1}(x)+F_{n-2}(x) \end{aligned} F1(x)Fn(x)=1,F2(x)=x=xFn−1(x)+Fn−2(x)
sympy中的fibonacci函数,当输入一个参数时会返回斐波那契数。
from sympy import print_latex
from sympy import fibonacci, Symbol
[fibonacci(x) for x in range(11)]
输出结果为 [ 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 ] [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55] [0,1,1,2,3,5,8,13,21,34,55]。
当输入一个数字和符号时,则会返回斐波那契多项式
p = fibonacci(5, Symbol('t'))
print_latex(p)
tribonacci
tribonacci,从名字就能看出来,是和3有关的斐波那契数列,其递推公式为 T n = T n − 1 + T n − 2 + T n − 3 , T 0 = 0 , T 1 = 1 , T 2 = 1 T_n=T_{n-1}+T_{n-2}+T_{n-3}, T_0=0, T_1=1, T_2=1 Tn=Tn−1+Tn−2+Tn−3,T0=0,T1=1,T2=1,其对应的函数可表示为
T 0 ( x ) = 0 , T 1 ( x ) = 1 , T 2 ( x ) = x 2 T n ( x ) = x 2 T n − 1 ( x ) + x T n − 2 ( x ) + T n − 3 ( x ) \begin{aligned} T_0(x)&=0, \quad T_1(x)=1,\quad T_2(x)=x^2\\ T_n(x)&=x^2T_{n-1}(x)+xT_{n-2}(x)+T_{n-3}(x) \end{aligned} T0(x)Tn(x)=0,T1(x)=1,T2(x)=x2=x2Tn−1(x)+xTn−2(x)+Tn−3(x)
tribonacci数列前10项输出如下
from sympy import tribonacci
[tribonacci(x) for x in range(11)]
[ 0 , 1 , 1 , 2 , 4 , 7 , 13 , 24 , 44 , 81 , 149 ] [0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149] [0,1,1,2,4,7,13,24,44,81,149]
p = tribonacci(5, Symbol('t'))
print_latex(p)
T 5 ( x ) = x 4 + 3 x 2 + 1 T_5(x)=x^{4} + 3 x^{2} + 1 T5(x)=x4+3x2+1
对比
接下来,可以绘制一下这两种数列的增长速率
import matplotlib.pyplot as plt
fs = [fibonacci(x) for x in range(11)]
ts = [tribonacci(x) for x in range(11)]
plt.plot(fs, label="fibonacci")
plt.plot(ts, label="tribonacci")
plt.legend()
plt.show()
对比如下