真正的斐波那契数的索引

编程入门 行业动态 更新时间:2024-10-26 02:25:02
本文介绍了真正的斐波那契数的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要在斐波那契数列内使用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.您可以在此处查看如何使用它的示例: reallifejs. com/the-meat/calculators/big-number-calculator/

You can use BigInteger. You can see an example of how to use it here: reallifejs/the-meat/calculators/big-number-calculator/

更多推荐

真正的斐波那契数的索引

本文发布于:2023-11-29 01:02:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644663.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!