The Fibonacci sequence is a sequence in which each term is obtained by adding the previous two terms. In this article, we will learn to find the sum of Fibonacci numbers at even indexes up to the first N even indexes.
- The Fibonacci sequence starts with F(0) = 0 and F(1) = 1.
- The required sum is F(0) + F(2) + F(4) + ... + F(2N).
Examples:
Input: n = 5
Output: 88
N = 5, So the fibonacci series will be generated from 0th term upto 10th term:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
Sum of elements at even indexes = 0 + 1 + 3 + 8 + 21 + 55Input: n = 8
Output: 1596
0 + 1 + 3 + 8 + 21 + 55 + 144 + 377 + 987 = 1596.
Method 1: Using Iteration
The direct approach generates Fibonacci numbers up to index 2N and adds the terms whose indexes are even.
Approach
- Initialize the first two Fibonacci numbers as 0 and 1.
- Generate Fibonacci numbers from index 2 to 2N.
- Add each Fibonacci number whose index is even.
- Return the resulting sum.
#include <iostream>
using namespace std;
long long calculateEvenIndexSum(int n) {
if (n < 0)
return 0;
long long prev2 = 0;
long long prev1 = 1;
long long sum = 0;
// F(0) is an even-indexed Fibonacci number.
sum = prev2;
for (int i = 2; i <= 2 * n; i++) {
long long curr = prev1 + prev2;
if (i % 2 == 0)
sum += curr;
prev2 = prev1;
prev1 = curr;
}
return sum;
}
int main() {
int n = 8;
cout << "Sum of Fibonacci numbers at even indexes up to "
<< 2 * n << " is "
<< calculateEvenIndexSum(n);
return 0;
}
Output
Sum of Fibonacci numbers at even indexes up to 16 is 1596
Explanation: The loop generates Fibonacci numbers up to F(2N). Whenever the current index is even, its Fibonacci value is added to sum.
Method 2: Using the Fibonacci Sum Formula
The direct approach generates every Fibonacci number up to F(2N). The sum can be calculated more efficiently using a Fibonacci identity:
F(0) + F(2) + F(4) + \cdots + F(2N) = F(2N+1) - 1
Therefore, instead of generating all the even-indexed Fibonacci numbers, we only need to calculate F(2N + 1).
Approach
- Calculate the Fibonacci number at index 2N + 1.
- Subtract 1 from the result.
- Return the resulting value.
For N = 8:
F(0)+F(2)+F(4)+\cdots+F(16)=F(17)-1
Since F(17) = 1597:
1597-1=1596
Derivation of the Formula
The sum of Fibonacci numbers from F(0) to F(m) is:
F(0)+F(1)+\cdots+F(m)=F(m+2)-1
For even-indexed terms, let m = 2N:
F(0)+F(2)+\cdots+F(2N)=F(2N+1)-1
Thus, the required sum can be found by calculating only F(2N+1).
3. Finding Fibonacci Number Using Fast Doubling
The Fibonacci number F(2N + 1) can be calculated in O(log N) time using the fast doubling method. The method is based on these identities:
F(2k)=F(k)\left(2F(k+1)-F(k)\right)
F(2k+1)=F(k)^2+F(k+1)^2
These formulas allow the Fibonacci sequence to be calculated using divide-and-conquer instead of generating every preceding term.
#include <iostream>
using namespace std;
pair<long long, long long> fib(int n) {
if (n == 0)
return {0, 1};
auto [a, b] = fib(n / 2);
long long c = a * (2 * b - a);
long long d = a * a + b * b;
if (n % 2 == 0)
return {c, d};
return {d, c + d};
}
long long calculateEvenIndexSum(int n) {
if (n < 0)
return 0;
return fib(2 * n + 1).first - 1;
}
int main() {
int n = 8;
cout << "Sum of Fibonacci numbers at even indexes up to "
<< 2 * n << " is "
<< calculateEvenIndexSum(n);
return 0;
}
Output
Sum of Fibonacci numbers at even indexes up to 16 is 1596
Explanation: The fib() function uses fast doubling to calculate two consecutive Fibonacci numbers at each recursive level. The required value F(2N + 1) is then obtained and 1 is subtracted to calculate the sum.