For a given positive integer N, the purpose is to find the value of F2 + F4 + F6 +………+ F2n till N number. Where Fi indicates the i’th Fibonacci number.
The Fibonacci Series is the numbers in the below-given integer sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ……
Examples:
Input: n = 4
Output: 33
N = 4, So here the fibonacci series will be produced from 0th term till 8th term:
0, 1, 1, 2, 3, 5, 8, 13, 21
Sum of numbers at even indexes = 0 + 1 + 3 + 8 + 21 = 33.
Input: n = 7
Output: 609
0 + 1 + 3 + 8 + 21 + 55 + 144 + 377 = 609.
Approach 1:
Find all Fibonacci numbers till 2n and add up only the even indices.
Java
import java.io.*;
class geeksforgeeks {
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 ;
int s = 0 ;
for ( int j = 2 ; j <= 2 * N; j++) {
fib[j] = fib[j - 1 ] + fib[j - 2 ];
if (j % 2 == 0 )
s += fib[j];
}
return s;
}
public static void main(String[] args)
{
int N = 11 ;
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
Time Complexity: O(n)
Auxiliary Space: O(n) as it is using an auxiliary array fib
Approach 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 .
The task is to find only F2n+1 -1.
Below is the implementation of the above approach:
Java
class GFG {
static int MAX = 1000 ;
static int f[] = new int [MAX];
static int fib( int n)
{
if (n == 0 ) {
return 0 ;
}
if (n == 1 || n == 2 ) {
return (f[n] = 1 );
}
if (f[n] == 1 ) {
return f[n];
}
int k = (n % 2 == 1 ) ? (n + 1 ) / 2 : n / 2 ;
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];
}
static int calculateEvenSum( int n)
{
return (fib( 2 * n + 1 ) - 1 );
}
public static void main(String[] args)
{
int n = 11 ;
System.out.println(
"Even indexed Fibonacci Sum upto " + n
+ " terms: " + calculateEvenSum(n));
}
}
|
Output
Even indexed Fibonacci Sum upto 8 terms: 1596
Time Complexity: O(log n)
Auxiliary Space: O(MAX) because using an auxiliary array
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
21 Aug, 2022
Like Article
Save Article