Sum of Fibonacci Numbers - GeeksforGeeks
Given a number positive number n, find value of f0 + f1 + f2 + …. + fn where fi indicates i'th Fibonacci number. Remember that f0 = 0, f1 = 1, f2 = 1, f3 = 2, f4 = 3, f5 = 5, …
The idea is to find relationship between the sum of Fibonacci numbers and n’th Fibonacci number.
DP:
Brute Force approach is pretty straight forward, find all the Fibonacci numbers till f(n-1) and then add them up.
Read full article from Sum of Fibonacci Numbers - GeeksforGeeks
Given a number positive number n, find value of f0 + f1 + f2 + …. + fn where fi indicates i'th Fibonacci number. Remember that f0 = 0, f1 = 1, f2 = 1, f3 = 2, f4 = 3, f5 = 5, …
The idea is to find relationship between the sum of Fibonacci numbers and n’th Fibonacci number.
F(i) refers to the i’th Fibonacci number.
S(i) refers to sum of Fibonacci numbers till F(i),
S(i) refers to sum of Fibonacci numbers till F(i),
We can rewrite the relation F(n+1) = F(n) + F(n-1) as below F(n-1) = F(n+1) - F(n) Similarly, F(n-2) = F(n) - F(n-1) . . . . . . . . . F(0) = F(2) - F(1) -------------------------------
Adding all the equations, on left side, we have
F(0) + F(1) + … F(n-1) which is S(n-1).
F(0) + F(1) + … F(n-1) which is S(n-1).
Therefore,
S(n-1) = F(n+1) – F(1)
S(n-1) = F(n+1) – 1
S(n) = F(n+2) – 1 —-(1)
S(n-1) = F(n+1) – F(1)
S(n-1) = F(n+1) – 1
S(n) = F(n+2) – 1 —-(1)
In order to find S(n), simply calculate the (n+2)’th Fibonacci number and subtract 1 from the result.
F(n) can be evaluated in O(log n) time using either method 5 or method 6
// Create an array for memoization
int
f[MAX] = {0};
// Returns n'th Fibonacci number using table f[]
int
fib(
int
n)
{
// Base cases
if
(n == 0)
return
0;
if
(n == 1 || n == 2)
return
(f[n] = 1);
// If fib(n) is already computed
if
(f[n])
return
f[n];
int
k = (n & 1)? (n+1)/2 : n/2;
// Applying above formula [Note value n&1 is 1
// if n is odd, else 0.
f[n] = (n & 1)? (fib(k)*fib(k) + fib(k-1)*fib(k-1))
: (2*fib(k-1) + fib(k))*fib(k);
return
f[n];
}
// Computes value of first Fibonacci numbers
int
calculateSum(
int
n)
{
return
fib(n+2) - 1;
}
Method 6 (O(Log n) Time)
Below is one more interesting recurrence formula that can be used to find n’th Fibonacci Number in O(Log n) time.
Below is one more interesting recurrence formula that can be used to find n’th Fibonacci Number in O(Log n) time.
If n is even then k = n/2: F(n) = [2*F(k-1) + F(k)]*F(k) If n is odd then k = (n + 1)/2 F(n) = F(k)*F(k) + F(k-1)*F(k-1)
Taking determinant on both sides, we get
(-1)n = Fn+1Fn-1 – Fn2
Moreover, since AnAm = An+m for any square matrix A, the following identities can be derived (they are obtained form two different coefficients of the matrix product)
(-1)n = Fn+1Fn-1 – Fn2
Moreover, since AnAm = An+m for any square matrix A, the following identities can be derived (they are obtained form two different coefficients of the matrix product)
FmFn + Fm-1Fn-1 = Fm+n-1
By putting n = n+1,
FmFn+1 + Fm-1Fn = Fm+n
Putting m = n
F2n-1 = Fn2 + Fn-12
To get the formula to be proved, we simply need to do following
If n is even, we can put k = n/2
If n is odd, we can put k = (n+1)/2
If n is even, we can put k = n/2
If n is odd, we can put k = (n+1)/2
// Create an array for memoization
int
f[MAX] = {0};
// Returns n'th fuibonacci number using table f[]
int
fib(
int
n)
{
// Base cases
if
(n == 0)
return
0;
if
(n == 1 || n == 2)
return
(f[n] = 1);
// If fib(n) is already computed
if
(f[n])
return
f[n];
int
k = (n & 1)? (n+1)/2 : n/2;
// Applyting above formula [Note value n&1 is 1
// if n is odd, else 0.
f[n] = (n & 1)? (fib(k)*fib(k) + fib(k-1)*fib(k-1))
: (2*fib(k-1) + fib(k))*fib(k);
return
f[n];
}
static
int
fib(
int
n)
{
/* Declare an array to store Fibonacci numbers. */
int
f[] =
new
int
[n+
1
];
int
i;
/* 0th and 1st number of the series are 0 and 1*/
f[
0
] =
0
;
f[
1
] =
1
;
for
(i =
2
; i <= n; i++)
{
/* Add the previous 2 numbers in the series
and store it */
f[i] = f[i-
1
] + f[i-
2
];
}
return
f[n];
}
int
fib(
int
n)
{
int
a = 0, b = 1, c, i;
if
( n == 0)
return
a;
for
(i = 2; i <= n; i++)
{
c = a + b;
a = b;
b = c;
}
return
b;
}
Method 4 ( Using power of the matrix {{1,1},{1,0}} )
This another O(n) which relies on the fact that if we n times multiply the matrix M = {{1,1},{1,0}} to itself (in other words calculate power(M, n )), then we get the (n+1)th Fibonacci number as the element at row and column (0, 0) in the resultant matrix.
This another O(n) which relies on the fact that if we n times multiply the matrix M = {{1,1},{1,0}} to itself (in other words calculate power(M, n )), then we get the (n+1)th Fibonacci number as the element at row and column (0, 0) in the resultant matrix.
static
int
fib(
int
n)
{
int
F[][] =
new
int
[][]{{
1
,
1
},{
1
,
0
}};
if
(n ==
0
)
return
0
;
power(F, n-
1
);
return
F[
0
][
0
];
}
/* Helper function that multiplies 2 matrices F and M of size 2*2, and
puts the multiplication result back to F[][] */
static
void
multiply(
int
F[][],
int
M[][])
{
int
x = F[
0
][
0
]*M[
0
][
0
] + F[
0
][
1
]*M[
1
][
0
];
int
y = F[
0
][
0
]*M[
0
][
1
] + F[
0
][
1
]*M[
1
][
1
];
int
z = F[
1
][
0
]*M[
0
][
0
] + F[
1
][
1
]*M[
1
][
0
];
int
w = F[
1
][
0
]*M[
0
][
1
] + F[
1
][
1
]*M[
1
][
1
];
F[
0
][
0
] = x;
F[
0
][
1
] = y;
F[
1
][
0
] = z;
F[
1
][
1
] = w;
}
/* Helper function that calculates F[][] raise to the power n and puts the
result in F[][]
Note that this function is designed only for fib() and won't work as general
power function */
static
void
power(
int
F[][],
int
n)
{
int
i;
int
M[][] =
new
int
[][]{{
1
,
1
},{
1
,
0
}};
// n - 1 times multiply the matrix to {{1,0},{0,1}}
for
(i =
2
; i <= n; i++)
multiply(F, M);
}
The method 4 can be optimized to work in O(Logn) time complexity. We can do recursive multiplication to get power(M, n) in the prevous method (Similar to the optimization done in this post)
static
int
fib(
int
n)
{
int
F[][] =
new
int
[][]{{
1
,
1
},{
1
,
0
}};
if
(n ==
0
)
return
0
;
power(F, n-
1
);
return
F[
0
][
0
];
}
static
void
multiply(
int
F[][],
int
M[][])
{
int
x = F[
0
][
0
]*M[
0
][
0
] + F[
0
][
1
]*M[
1
][
0
];
int
y = F[
0
][
0
]*M[
0
][
1
] + F[
0
][
1
]*M[
1
][
1
];
int
z = F[
1
][
0
]*M[
0
][
0
] + F[
1
][
1
]*M[
1
][
0
];
int
w = F[
1
][
0
]*M[
0
][
1
] + F[
1
][
1
]*M[
1
][
1
];
F[
0
][
0
] = x;
F[
0
][
1
] = y;
F[
1
][
0
] = z;
F[
1
][
1
] = w;
}
/* Optimized version of power() in method 4 */
static
void
power(
int
F[][],
int
n)
{
if
( n ==
0
|| n ==
1
)
return
;
int
M[][] =
new
int
[][]{{
1
,
1
},{
1
,
0
}};
power(F, n/
2
);
multiply(F, F);
if
(n%
2
!=
0
)
multiply(F, M);
}
Brute Force approach is pretty straight forward, find all the Fibonacci numbers till f(n-1) and then add them up.
int
calculateSum(
int
n)
{
if
(n <= 0)
return
0;
int
fibo[n+1];
fibo[0] = 0, fibo[1] = 1;
// Initialize result
int
sum = fibo[0] + fibo[1];
// We are using zero indexing
//so the nth fibonacci number is
//n-1 th fibonacci number
for
(
int
i=2; i<=n; i++)
{
fibo[i] = fibo[i-1]+fibo[i-2];
sum += fibo[i];
}
return
sum;
}