私は逆フィボナッチ定数 (フィボナッチ数の無限の合計) を計算するプログラムに取り組んでいます。エラーまでのすべての項を計算します。
私はプログラムを持っていますが、それは 1474 項までしか進まず、約 10000 項まで到達する必要があります。エラーが返されます。
Traceback (most recent call last):
File "/Users/jaddvirji/Desktop/PapaTechChallenges/Challenge2/Part1/main.py", line 23, in <module>
curf.write(str(Decimal(fibConstant(x))))
File "/Users/jaddvirji/Desktop/PapaTechChallenges/Challenge2/Part1/main.py", line 18, in fibConstant
return (1.0 / fib(n)) + fibConstant(n - 1.0)
File "/Users/jaddvirji/Desktop/PapaTechChallenges/Challenge2/Part1/main.py", line 12, in fib
return long(((phi**n) - (1-phi)**n) / 5**0.5)
OverflowError: (34, 'Result too large')
そして私のコードは次のとおりです。
#!/usr/bin/env python
print "(C) COPYRIGHT JADD VIRJI 2013. ALL RIGHTS RESERVED."
from decimal import *
import time as t
import sys
sys.setrecursionlimit(10000)
phi = (1+(5**0.5))/2
def fib(n):
return long(((phi**n) - (1-phi)**n) / 5**0.5)
def fibConstant(n):
if(n == 1):
return (1.0 / fib(n))
else:
return (1.0 / fib(n)) + fibConstant(n - 1.0)
x = 1
while True:
curf = open(str(x)+" term.txt","w")
curf.write(str(Decimal(fibConstant(x))))
curf.close()
x = x+1
print Decimal(x)
print "DONE. THANKS FOR USING."
また、上記の約 200 の用語からのすべての結果は同じです (そして間違っています)。
これらの問題を解決する方法を知っている人はいますか?
編集: ~ 200 項以降の問題は、Binet Fibonacci 計算の浮動小数点エラーが原因であると感じています。これらの小数を永遠に続けるにはどうすればよいですか?