Compute the classic Fibonacci running times quantitatively via the third variable

复制代码
//Util.h
static unsigned long long factorialLoops;
long double Fibonacci35(int len);

//Util.cpp
unsigned long long Util::factorialLoops=0;

long double Util::Fibonacci35(int i)
{
    Util::factorialLoops++;
    if(i==0 || i==1)
    {
        return 1;
    }
    else
    {
        return Fibonacci35(i-1)+Fibonacci35(i-2);
    }
}

//main.cpp
void recursion16(int i)
{
    Util ul;
    long double fib = 0;
    for (int x = 0; x < i; x++)
    {
        fib = ul.Fibonacci35(x);
        cout <<fixed<< "Fib of " << x << " is " << fib <<",fibonacci loops is "<<Util::factorialLoops<<endl;
    }
    cout<<endl<<"The recursion loops is "<<Util::factorialLoops<<endl;
}


int main(int args, char **argv)
{
    recursion16(atoi(argv[1]));
    return 0;
}
复制代码

Compile and run as below command which will cost more time definitely

./h1 100

The following snapshot can demonstrate everything

 

 When run the 54 it  call the classic Fibonacci method more than 730 billion times.

posted @   FredGrit  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2020-04-19 dynamic intercept and interpret all method calls via implementation IDynamicMetadataObjectProvider
2020-04-19 get caller member name,caller file path,caller line number via attributes
2020-04-19 try catch exception when
点击右上角即可分享
微信分享提示