1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

sympyで「フィボナッチ数(Fibonacci number)」の一般項を出力してみた

Last updated at Posted at 2018-04-18

(参考)
階差方程式の求解<Python3モジュールブック

公式

http://docs.sympy.org/latest/_modules/sympy/functions/combinatorial/numbers.html#fibonacci

Pythonからはじめる数学入門>フィボナッチ数列と黄金比の関係>
fibonacci_goldenration.py

from sympy import *
f = simplify('f(n)')
s = simplify('f(n+2)-f(n+1)-f(n)')
ini = simplify('{f(0):0,f(1):1}')
print(rsolve(s,f,ini))
#sqrt(5)*(1/2 + sqrt(5)/2)**n/5 - sqrt(5)*(-sqrt(5)/2 + 1/2)**n/5

wolframalphaで

参考
Fibonacci?Wolfram Language Documentation

Input interpretation:
solve a(n + 2) = a(n) + a(n + 1)
a(0) = 0
a(1) = 1 for a(n)
Result:
a(n) = F_n
何か違います。

(2018-11-26)-----------------------------

sympyの一般項で、初期値を出力してみた。

参考

FullScript.py
from sympy import *
var('n')
f = simplify('f(n)')
s = simplify('f(n+2)-f(n+1)-f(n)')
ini = simplify('{f(0):0,f(1):1}')
g=rsolve(s,f,ini)
print(float(g.subs(n,0)))
print(float(g.subs(n,1)))
#------------------------------------
f = simplify('f(n)')
s = simplify('f(n+2)-f(n+1)-f(n)')
ini = simplify('{f(1):0,f(2):1}')
g=rsolve(s,f,ini)
print(float(g.subs(n,1)))
print(float(g.subs(n,2)))
# 0.0
# 1.0
# 5.909106315382871e-126
# 1.0
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?