The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding numbers, starting with 0 and 1. In this program, we calculate the sum of Fibonacci numbers present at even indexes from index 0 to 2 × N.
- Only Fibonacci numbers at even indexes are included in the sum.
- An iterative solution takes O(N) time, while a mathematical solution can reduce it to O(log N).
Illustration
Input: N = 4
Output: 33Input: N = 7
Output: 609
Approach 1: Using Dynamic Programming
Generate Fibonacci numbers from 0 to 2 × N and add only those whose indexes are even.
Algorithm
- Create a Fibonacci array.
- Generate Fibonacci numbers up to index 2 × N.
- Whenever the index is even, add the Fibonacci number to the sum.
- Print the final sum.
import java.io.*;
class geeksforgeeks {
// Computing the value of first fibonacci series
// and storing the sum of even indexed numbers
static int Fib_Even_Sum(int N)
{
if (N <= 0)
return 0;
int fib[] = new int[2 * N + 1];
fib[0] = 0;
fib[1] = 1;
// Initializing the sum
int s = 0;
// Adding remaining numbers
for (int j = 2; j <= 2 * N; j++) {
fib[j] = fib[j - 1] + fib[j - 2];
// Only considering even indexes
if (j % 2 == 0)
s += fib[j];
}
return s;
}
// The Driver code
public static void main(String[] args)
{
int N = 11;
// Prints the sum of even-indexed numbers
System.out.println(
"Even sum of fibonacci series till number " + N
+ " is: " + +Fib_Even_Sum(N));
}
}
Output
Even sum of fibonacci series till number 11 is: 28656
Explanation: The program first generates Fibonacci numbers iteratively and stores them in an array. While generating the sequence, it checks whether the current index is even. If it is, that Fibonacci number is added to the running sum. After processing all terms up to index 2 × N, the sum is displayed.
Approach 2: Using Fibonacci Identity (Optimized)
Mathematical Formula: The sum of Fibonacci numbers at even indexes is
F0+F2+F4+⋯+F2N=F2N+1−1F_0 + F_2 + F_4 + \cdots + F_{2N} = F_{2N+1} - 1F0+F2+F4+⋯+F2N=F2N+1−1
Therefore, instead of generating the complete sequence, we only need to compute F₂ₙ₊₁.
Algorithm
- Compute F₂ₙ₊₁ using the Fast Doubling Fibonacci algorithm.
- Subtract 1 from the result.
- Print the answer.
class GFG {
static int MAX = 1000;
// Create an array for memoization
static int f[] = new int[MAX];
// Returns n'th Fibonacci number
// using table f[]
static 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] == 1) {
return f[n];
}
int k = (n % 2 == 1) ? (n + 1) / 2 : n / 2;
// Applying above formula [Note value n&1 is 1
// if n is odd, else 0].
f[n] = (n % 2 == 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
static int calculateEvenSum(int n)
{
return (fib(2 * n + 1) - 1);
}
// Driver program to test above function
public static void main(String[] args)
{
// Get n
int n = 11;
// Find the alternating sum
System.out.println(
"Even indexed Fibonacci Sum upto " + n
+ " terms: " + calculateEvenSum(n));
}
}
Output
Even indexed Fibonacci Sum upto 11 terms: 28656