java - 使用 'fib(N) = [Phi^N – phi^N]/Sqrt[5]' 公式计算

Fibonacci 的正常递归是 Fib(N)= Fib(N-1)+Fib(n-2),其时间复杂度为 2^N。 为了降低时间复杂度,我们有以下公式:

Fib(N) = [Phi^N – phi^N]/Sqrt[5] Source

其中 Phi= (1 + Sqrt[5])/2phi = (1-Sqrt[5])/2;phi=Phi-1 Source

我的java代码如下:

import java.util.*;
import java.lang.*;
public class Main
{
    public static void main(String[] args) {
        System.out.println(fib(50)+"");
    }
    
    static long fib(int N){
        final double sqrtFive=Math.sqrt(5);
        double Phi=(sqrtFive+1)/2;
        double phi=Phi-1;
        double fibN=(Math.pow(Phi,N)-Math.pow(phi,N))/sqrtFive;
        return (long) fibN;
    }
}

我的代码的时间复杂度是多少?

O(1)? because modern computer are superfast in computation so Math.pow(base,power) will be almost constant for any power.

O(logn+logn) means O(logn)? because I'm doing Math.pow(Phi,N)-Math.pow(phi,N) and Math.pow() function takes logN time.

我很困惑,请帮帮我。

相关文章:

xamarin - MAUI Shell 从侧面移除 OverScroll

python - 以两种方式遍历列表但无法理解行为

javascript - OpenSea 错误 - 请使用 providerUtils.standa

javascript - 在 Promise.all 中 react setState

node.js - 在 Github 操作中获取错误 : connect ECONNREFUSED

flutter - 禁用时如何更改下拉默认文本颜色?

html - Flexbox 内容显示不正确

windows - 为什么没有启动 Windows 应用程序

c# - 触发 SelectedIndexChanged 后列表框在视觉上卡住

html - 如何用 CSS 连接大写单词?