Qiita Teams that are logged in
You are not logged in to any team

Community
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?

More than 1 year has passed since last update.

@mrrclb48z

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

(参考)Python3モジュールブック＞階差方程式の求解
http://www.k-techlabo.org/www_python/python_modules.pdf#page=72
(参考)sympy＞class sympy.functions.combinatorial.numbers.fibonacci
http://docs.sympy.org/latest/_modules/sympy/functions/combinatorial/numbers.html#fibonacci
(参考)wikipedia＞Fibonacci number
https://en.wikipedia.org/wiki/Fibonacci_number
(参考)Pythonからはじめる数学入門＞フィボナッチ数列と黄金比の関係＞fibonacci_goldenration.py
https://github.com/doingmathwithpython/code/blob/master/chapter2/solutions/fibonacci_goldenratio.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
http://reference.wolfram.com/language/ref/Fibonacci.html
(参考)オンライン mathematica
http://www.wolframalpha.com/input/?i=RSolve%5B%7Ba%5Bn%2B2%5D+%3D%3D+a%5Bn%5D%2Ba%5Bn%2B1%5D,+a%5B0%5D+%3D%3D+0,a%5B1%5D+%3D%3D+1%7D,+a%5Bn%5D,+n%5D

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の一般項で、初期値を出力してみた。

(参考)Pythonで数学の勉強：数列・級数・組み合わせ論
https://qiita.com/tibigame/items/84bfd46df494e05c711e#フィボナッチ数

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
``````
0
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away
0
Help us understand the problem. What is going on with this article?