Hi all,
Having just discovered Binet's formula for the nth fibonacci number I
thought
I'd give it a try in sympy and stumbled across a possible bug.
Binet's formula, although it has a number of irrational quantities, should
always yield an integer. I found that sympy was able to compute this
correctly
up to the 20th Fibonacci number and that started returning irrational
quantities when I asked it to simplify the expression.
Demonstration using a fresh checkout of sympy master in a freshly created,
otherwise empty Python 2.7.1 virtual env:
>>> import sympy
>>> n = sympy.Symbol('n')
>>> Phi = (1 + sympy.sqrt(5)) / 2
>>> fibn = (Phi**n - (-Phi)**(-n)) / sympy.sqrt(5)
>>> for i in range(1, 25):
... print '%ith Fibonacci number:' % i, fibn.subs(n, i).simplify()
...
1th Fibonacci number: 1
2th Fibonacci number: 1
3th Fibonacci number: 2
4th Fibonacci number: 3
5th Fibonacci number: 5
6th Fibonacci number: 8
7th Fibonacci number: 13
8th Fibonacci number: 21
9th Fibonacci number: 34
10th Fibonacci number: 55
11th Fibonacci number: 89
12th Fibonacci number: 144
13th Fibonacci number: 233
14th Fibonacci number: 377
15th Fibonacci number: 610
16th Fibonacci number: 987
17th Fibonacci number: 1597
18th Fibonacci number: 2584
19th Fibonacci number: 4181
20th Fibonacci number: 6765
21th Fibonacci number: -4498 + sqrt(5) <--- should be 10946
22th Fibonacci number: -10108 + sqrt(5)
23th Fibonacci number: -6785 + sqrt(5)
24th Fibonacci number: sqrt(5) + 14490
>>> fibn.subs(n, 21)
sqrt(5)*(-1/(-sqrt(5)/2 - 1/2)**21 + (1/2 + sqrt(5)/2)**21)/5
>>> fibn.subs(n, 21).simplify()
-4498 + sqrt(5)
>>> fibn.subs(n, 21).simplify().evalf()
-4495.76393202250
>>> fibn.subs(n, 21).evalf()
10946.0000000000
That last line shows that the expression is correct before it is
simplify()ed.
Is this a known problem? I'd be happy to help fix this but I have little
idea
where to start. Is there any significance in the fact that it goes wrong at
powers > 20?
Oscar
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/sympy/-/gH8z3L4TgbUJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sympy?hl=en.