真正的斐波那契数的索引 [英] Index of a really big Fibonacci Number
问题描述
我需要在斐波那契数列内使用JavaScript计算斐波那契数的索引.我需要在不使用递归或循环的情况下执行此操作.我在数学论坛中找到了以下公式 :
I need to calculate the index of a Fibonacci number with JavaScript, within the Fibonacci sequence. I need to do this without using recursion, or a loop. I found the following formula in the Math forum:
n =⌊logφ(F⋅5√+ 12)⌋
n=⌊logφ(F⋅5√+12)⌋
并用JavaScript进行编码:
and coded it in JavaScript:
function fibIndex(fib)
{
fib = BigNumber(fib);
return logBasePhi(fib.times(Math.sqrt(5)).plus((1/2)));
}
function phi()
{
return (1 + Math.sqrt(5))/ 2;
}
function getBaseLog(x, y) {
return Math.log(y) / Math.log(x);
}
function logBasePhi(x)
{
return getBaseLog(phi(), x);
}
注意此BigNumber库的一部分的.times()
和.plus()
函数
Notice the .times()
and .plus()
functions that are part of this BigNumber Library that has been extremely useful up to this point. This works fine, until the Fibonacci number I want to find the index for is really big.
问题:
我需要一种不同的方法来计算这么大的对数.如果我有一个非常大的数字,例如2000年的斐波那契,则出于明显的原因,我得到Infinity
.该库本身没有任何计算日志的方法,我也无法编写此函数.
I need a different way to calculate the logarithm with such a big number. If I have a really big number, such as Fibonacci of 2000, I get Infinity
for obvious reasons. The library itself does not have any methods to calculate the log, and I can't write this function either.
我从来没有想到过,任何具有这么小的基数(phi)的对数都可以大于JavaScript整数的对数.你们能指出我正确的方向吗?我是否应该让它获得小于Fib(1500)的数字的索引并称其为好?
I would have never imagined that the logarithm of any number with such a small base (phi) can be bigger than the max for JavaScript integers. Can you guys point me in the right direction? Should I just leave it at obtaining the index for numbers less than Fib(1500) and call it good?
推荐答案
您可以使用BigInteger.您可以在此处查看如何使用它的示例: http://reallifejs. com/the-meat/calculators/big-number-calculator/
You can use BigInteger. You can see an example of how to use it here: http://reallifejs.com/the-meat/calculators/big-number-calculator/
这篇关于真正的斐波那契数的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!