Sum of products of Fibonacci numbers [closed]

雨燕双飞 提交于 2019-12-11 07:42:35

问题


Given a series

Fib(1) * Fib (n+2) + Fib(2) * Fib(n+1) + Fib(3) * Fib(n) + ...... + Fib(n-1) * Fib(4)

or Summation Fib(x) * Fib (n-x+3) where x varies from 1 to n-1 where Fib(n) is nth number of Fibonacci series

to evaluate this series Fib(n) can be calculated using matrix exponentiation .

But the complexity for this is logn and for the n terms it would be nlogn .

I want this series to get reduced to a single term or some other optimization to improve *the time complexity .*

Any suggestions ??


回答1:


I can't reduce the sum to a single term, but it can be reduced to a sum of five terms, which reduces the complexity to O(log n) arithmetic operations.

However, Fib(n) has Θ(n) bits, so the number of bit-operations is not logarithmic. There is a multiplication of a number the size of Fib(n) with n-1, so the number of bit-operations is M(n,log n), where M(a,b) is the bit-operation complexity of a multiplication of an a-bit number with a b-bit number. For the naive algorithm, M(a,b) = a*b, so the number of bit-operations in the below algorithm is O(n*log n).

The fact that allows this reduction is that Fibonacci numbers (like all numbers in a sequence defined by a linear recurrence) can be written as the sum of pure exponential terms, in particular

Fib(n) = (α^n - β^n) / (α - β)

where

α = (1 + √5)/2;    β = (1 - √5)/2.

In addition to the Fibonacci numbers, I also use the Lucas numbers, which follow the same recurrence as the Fibonacci numbers,

Luc(n) = α^n + β^n

so the sequence of Lucas numbers (starting from index 0) begins with

2 1 3 4 7 11 18 29 47 ...

The relation Luc(n) = Fib(n+1) + Fib(n-1) allows an easy conversion between Fibonacci and Lucas numbers, and computation of Luc(n) in O(log n) steps can reuse the Fibonacci code.

So with the representation of Fibonacci numbers given above, we find

(α - β)^2 * Fib(k) * Fib(n+3-k) = (α^k - β^k) * (α^(n+3-k) - β^(n+3-k))
                                = α^(n+3) + β^(n+3) - (α^k * β^(n+3-k)) - (α^(n+3-k) * β^k)
                                = Luc(n+3) - ((-1)^k * α^(2k) * β^(n+3)) - ((-1)^k * α^(n+3) * β^(2k))

using the relation α * β = -1.

Now, since α - β = √5 the summation k = 1, ..., n-1 yields

   n-1                                              n-1                   n-1
5 * ∑ Fib(k)*Fib(n+3-k) = (n-1)*Luc(n+3) - β^(n+3) * ∑ (-α²)^k - α^(n+3) * ∑ (-β²)^k
   k=1                                              k=1                   k=1

The geometric sums can be written in closed form, and a bit of juggling yields the formula

n-1
 ∑ Fib(k)*Fib(n+3-k) = [5*(n-1)*Luc(n+3) + Luc(n+2) + 2*Luc(n+1) - 2*Luc(n-3) + Luc(n-4)]/25
k=1



回答2:


Steps to follow:

  • Define std::vector<int> and fill it with all fibonacci numbers which you need. Compute these numbers using dynamic programming; that is, make use of results which you already have computed. Don't compute the same value more than once!

  • Once you have the vector filled with all the numbers you need, apply the formula Fib(x) * Fib (n-x+3) in a loop and compute the sum of the products.




回答3:


assuming f(n)=f(n-1)+f(n-2) for n>2 and f(1)=f(2)=1, f(0)=0

Let E(n)=sum(k=1 .. n-1, f(k)f(n-k+3)) and E'(n)=sum(k=0 .. n, f(k)f(n-k))
Clearly E(n)=E'(n+3) - ( f(0)f(n+3) + f(n)f(3) + f(n+1)f(2) + f(n+2)f(1) + f(n+3)f(0) )

On wolfram you'll find : E'(n)= (nL(n)-f(n))/5, with L(n)=f(n+1)+f(n-1)

from this :
5E(n)=(n+3)( f(n+4)+f(n+2) ) - 5(2f(n) + f(n+1) +f(n+2))
5E(n)=(n+3)( 4f(n+1)+3f(n) ) - 10f(n+1) -15f(n)
5E(n)= (4n+2)f(n+1) + (3n-6)f(n)

Should be simple to evaluate, the complexity should be the same as the fibonnaci algorithm used,




回答4:


If I know what a Lucas number is, then I can use the identity...

F(n)*F(m) = [L(m+n) - (-1)^n*L(m-n)]/5

See this reduces the sum of products of Fibonacci numbers into a sum of Lucas numbers.

Even better, since n+m is constant for each term, the sum reduces further. There are n-1 terms in this sum, so the sum reduces to a sum of Lucas numbers.

[(n-1)*L(n+3) + L(n+1) - L(n-1) + L(n-3) - ... + (-1)^(n-1)*L(5-n)]/5

As a test, for n = 5, I get 40, which is consistent with the direct sum of products:

1*13 + 1*8 + 2*5 + 3*3 = 40

For n = 1000, I get a sum of:

82283375600539014079871356568026158421560221654785733943009487102720 211767741849325389562067921531531130739623611293922046989610820831567088 516047002196966545744637588824274730947688693969572937880383134671205375

Even better, some extra work will let that sum of Lucas numbers contract further since the Lucas numbers of negative index have a simple relation to those with positive index.

As far as computing the k'th Lucas (as well as the k'th Fibonacci) number, it can be done quite efficiently as shown here.



来源:https://stackoverflow.com/questions/12248587/sum-of-products-of-fibonacci-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!