C++ optimized fibonacci algorithm

void Util::fib53(int *arr, int num, int &result)
{
    arr[0]=0;
    arr[1]=1;
    for(int i=2;i<=num;i++)
    {
        arr[i]=arr[i-2]+arr[i-1];
    }
    
    for(int i=0;i<=num;i++)
    {
        cout<<i<<","<<arr[i]<<"\t\t";
    }
    cout<<endl<<endl;
    result=arr[num];
}

void fib34(int len)
{
Util ul;
int result;
int *arr = new int[len];
ul.fib53(arr, len, std::ref(result));
cout << "Len=" << len << ",result=" << result << endl;
}

 

 

g++ -g -std=c++2a -I. *.cpp ./Model/*.cpp -o h1 -luuid -lpthread

 

posted @ 2022-03-19 20:21  FredGrit  阅读(18)  评论(0编辑  收藏  举报