算法功能斐波纳契数列数列、算法、功能、斐波纳契

2023-09-11 04:14:20 作者:微凉晨光

我不是在寻找一定的答案,但我期待的是什么这个问题是问的。发现这个问题研究的采访,但他们问不知道是什么?

  

这是贯穿始终的斐波那契序列,并返回写入功能   传递作为参数的指数。

解决方案

首先,你可以用这个的从维基链接。看看这个公式快速calculate.and你可以阅读所有关于它< A HREF =htt​​p://fusharblog.com/solving-linear-recurrence-for-programming-contest/相对=nofollow>这个链接。

这是递归函数计算第n个Fibonacci数,是O(2 ^ n)时间:

  INT斐波那契(INT N){
        如果(N == 0 ||ñ== 1)返回N;
        其他
        返回斐波那契数(N  -  1)+斐波那契(N  -  2); }
 
斐波那契数列通项公式,要过程

  

计算序列

     

您可能会认为,在实际计算的值的条件   Fibonacci序列的计算机上,你最好还是使用原来的   递推关系,F [N] = F [N-1] + F [N-2]。我倾向于同意。要使用   对于大的N直接封闭形式的解决方案,你需要保持   很多precision。即使有9位小数了,   fn≈round(0.723606798⋅(1.618033989)n)的,例如,仅适用于   高达38例(观察这里与这里)。此外,增加的整数是多少   更少的计算量很大,更precise比exponentiating一   象征性的分数或浮点数

这是更好的主意来计算第n个Fibonacci数和为O(n)时间:

  INT斐波那契(INT N){
如果(N&LT; = 0)返回0;
如果(N&GT; 0安培;&安培; N 3;)返回1;

INT结果为0;
INT preOldResult = 1;
INT oldResult = 1;

的for(int i = 2;我n种;我++){
    结果= preOldResult + oldResult;
    preOldResult = oldResult;
    oldResult =结果;
}

返回结果;}
 

这是计算第n个斐波纳契数的最好办法,是为O(log(n))的时间:

this链接:

因为你已经怀疑,这会工作非常相似。使用的的n次方X * X 基质

  | 1 0 0 0 .... 1 1 |
| 1
| 1
| 1
| 1
| 1
...................
...................
| ... 1 0 |
 

这是容易,如果你用向量乘以这个矩阵理解

  F(N-1),F(N-2),...,F(N-X + 1),F(NX)
 

这导致

  F(N),F(N-1),...,F(N-X + 1)
 

矩阵求幂可以在O完成(的log(n))的时间(当x被认为是常数)。

有关Fibonacci递推,还有一个封闭的公式的解决方案,在这里看到的 HTTP://en.wikipedia .ORG /维基/ Fibonacci_number ,寻找比奈的或棣美弗公式。

和查看: 在次线性时间1 第n个斐波纳契数

I'm not looking necessarily for an answer, but I am looking for what this question is asking of. Found this question studying for an interview but not sure what they're asking?

Write function that runs through the Fibonacci sequence and returns the index that is passed in as a parameter.

解决方案

firstly,you can update your base math information about Fibonacci with this link from wiki. and look at this formula for fast calculate.and you can read all of about it in this link.

This is recursive function to compute nth Fibonacci number and is of O(2^n) time:

 int Fibonacci(int n) {  
        if (n == 0 || n == 1)  return n;
        else
        return Fibonacci(n - 1) + Fibonacci(n - 2); }

Computing the Sequence

You might argue that in terms of actually computing the values of the Fibonacci sequence on a computer, you’re better off using the original recurrence relation, f[n]=f[n−1]+f[n−2]. I’m inclined to agree. To use the direct closed-form solution for large n, you need to maintain a lot of precision. Even with 9 decimal places out, fn≈round(0.723606798⋅(1.618033989)n), for example, is only valid for up to n=38 (observe here versus here). Also, adding integers is much less computationally expensive and more precise than exponentiating a symbolic fraction or a floating point value

this is better idea to compute nth Fibonacci number and is of O(n) time:

int Fibonacci(int n) { 
if(n <= 0) return 0;
if(n > 0 && n < 3) return 1;

int result = 0;
int preOldResult = 1;
int oldResult = 1;

for (int i=2;i<n;i++) { 
    result = preOldResult + oldResult;
    preOldResult = oldResult;
    oldResult = result;
}

return result;}

and this is the best way to compute nth Fibonacci number and is of O(log(n)) time:

this link:

As you are already suspecting, this will work very similar. Use the n-th power of the x * x matrix

|1 0 0 0  .... 1 1|
|1 
|  1
|    1
|      1
|        1
...................
...................
|          ... 1 0|

This is easy to understand if you multiply this matrix with the vector

f(n-1), f(n-2), ... , f(n-x+1), f(n-x)

which results in

f(n), f(n-1), ... , f(n-x+1)

Matrix exponentiation can be done in O(log(n)) time (when x is considered to be constant).

For the Fibonacci recurrence, there is also a closed formula solution, see here http://en.wikipedia.org/wiki/Fibonacci_number, look for Binet's or Moivre's formula.

and look at: 1-nth fibonacci number in sublinear time