C++ Program To Find Sum of Fibonacci Numbers at Even Indexes Upto N Terms
Last Updated :
27 Jan, 2023
Given a positive integer N, the task is to find the value of F2 + F4 + F6 +.........+ F2n upto N terms where Fi denotes the i-th Fibonacci number.
The Fibonacci numbers are the numbers in the following integer sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ......
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 + 55
Input: n = 8
Output: 1596
0 + 1 + 3 + 8 + 21 + 55 + 144 + 377 + 987 = 1596.
Method-1: This method includes solving the problem directly by finding all Fibonacci numbers till 2n and adding up the only the even indices. But this will require O(n) time complexity.
Below is the implementation of the above approach:
C++
// C++ Program to find sum of
// even-indiced Fibonacci numbers
#include <bits/stdc++.h>
using namespace std;
// Computes value of first
// fibonacci numbers and
// stores the even-indexed sum
int calculateEvenSum(int n)
{
if (n <= 0)
return 0;
int fibo[2 * n + 1];
fibo[0] = 0, fibo[1] = 1;
// Initialize result
int sum = 0;
// Add remaining terms
for (int i = 2; i <= 2 * n; i++)
{
fibo[i] = fibo[i - 1] + fibo[i - 2];
// For even indices
if (i % 2 == 0)
sum += fibo[i];
}
// Return the alternating sum
return sum;
}
// Driver code
int main()
{
// Get n
int n = 8;
// Find the even-indiced sum
cout << "Even indexed Fibonacci Sum upto " <<
n << " terms: " << calculateEvenSum(n) <<
endl;
return 0;
}
Output: Even indexed Fibonacci Sum upto 8 terms: 1596
Time complexity: O(N).
Auxiliary space: O(N).
Method-2:
It can be clearly seen that the required sum can be obtained thus:
2 ( F2 + F4 + F6 +.........+ F2n ) = (F1 + F2 + F3 + F4 +.........+ F2n) - (F1 - F2 + F3 - F4 +.........+ F2n)
Now the first term can be obtained if we put 2n instead of n in the formula given here.
Thus F1 + F2 + F3 + F4 +.........+ F2n = F2n+2 - 1.
The second term can also be found if we put 2n instead of n in the formula given here
Thus, F1 - F2 + F3 - F4 +.........- F2n = 1 + (-1)2n+1F2n-1 = 1 - F2n-1.
So, 2 ( F2 + F4 + F6 +.........+ F2n)
= F2n+2 - 1 - 1 + F2n-1
= F2n+2 + F2n-1 - 2
= F2n + F2n+1 + F2n+1 - F2n - 2
= 2 ( F2n+1 -1)
Hence, ( F2 + F4 + F6 +.........+ F2n) = F2n+1 -1 .
So in order to find the required sum, the task is to find only F2n+1 which requires O(log n) time.( Refer to method 5 or method 6 in this article.
Below is the implementation of the above approach:
C++
// C++ Program to find even indexed
// Fibonacci Sum in O(Log n) time.
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000;
// 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 even-indexed
// Fibonacci Sum
int calculateEvenSum(int n)
{
return (fib(2 * n + 1) - 1);
}
// Driver code
int main()
{
// Get n
int n = 8;
// Find the alternating sum
cout << "Even indexed Fibonacci Sum upto " <<
n << " terms: " << calculateEvenSum(n) <<
endl;
return 0;
}
Output:Even indexed Fibonacci Sum upto 8 terms: 1596
Time complexity: O(log(N)).
Auxiliary Space: O(N).
Similar Reads
C/C++ Program for nth multiple of a number in Fibonacci Series Given two integers n and k. Find position the n'th multiple of K in the Fibonacci series. Examples: Input : k = 2, n = 3 Output : 9 3'rd multiple of 2 in Fibonacci Series is 34 which appears at position 9. Input : k = 4, n = 5 Output : 30 5'th multiple of 4 in Fibonacci Series is 832040 which appear
4 min read
C++ Program for How to check if a given number is Fibonacci number? Given a number 'n', how to check if n is a Fibonacci number. First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 141, .. Examples : Input : 8 Output : Yes Input : 34 Output : Yes Input : 41 Output : No Following is an interesting property about Fibonacci numbers that can also be
2 min read
Solving f(n)= (1) + (2*3) + (4*5*6) ... n using Recursion Example : Input : 2 Output: 7 Series: (1) + (2*3) Input : 4 Output: 5167 Series: (1) + (2*3) + (4*5*6) + (7*8*9*10) Recommended PracticeRecursive sequenceTry It! C++ // CPP Program to print the solution // of the series f(n)= (1) + (2*3) // + (4*5*6) ... n using recursion #include<bits/stdc++.h
7 min read
C++ Program for Frequencies of even and odd numbers in a matrix Given a matrix of order m*n then the task is to find the frequency of even and odd numbers in the matrix Examples: Input : m = 3, n = 3 { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } Output : Frequency of odd number = 5 Frequency of even number = 4 Input : m = 3, n = 3 { 10, 11, 12 }, { 13, 14, 15 }, { 16, 1
3 min read
C++ Program For Fibonacci Numbers The Fibonacci series is the sequence where each number is the sum of the previous two numbers. The first two numbers of the Fibonacci series are 0 and 1, and they are used to generate the entire series.Examples:Input: 5Output: 5Explanation: As 5 is the 5th Fibonacci number of series 0, 1, 1, 2, 3, 5
5 min read