Given a limit, find the sum of all the even-valued terms in the Fibonacci sequence below given limit.
The first few terms of Fibonacci Numbers are, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233 ,… (Even numbers are highlighted).
Examples :
Input : limit = 8
Output : 10
Explanation : 2 + 8 = 10
Input : limit = 400;
Output : 188.
Explanation : 2 + 8 + 34 + 144 = 188.
A simple solution is to iterate through all Fibonacci numbers while the next number is less than or equal to given limit. For every number, check if it is even. If the number is even, add it to the result.
An efficient solution is based on the below recursive formula for even Fibonacci Numbers
Recurrence for Even Fibonacci sequence is:
EFn = 4EFn-1 + EFn-2
with seed values
EF0 = 0 and EF1 = 2.
EFn represents n'th term in Even Fibonacci sequence.
Refer this more details of above formula.
So while iterating over Fibonacci numbers, we only generate even Fibonacci numbers.
C++
#include<iostream>
using namespace std;
int evenFibSum(int limit)
{
if (limit < 2)
return 0;
long long int ef1 = 0, ef2 = 2;
long long int sum = ef1 + ef2;
while (ef2 <= limit)
{
long long int ef3 = 4*ef2 + ef1;
if (ef3 > limit)
break;
ef1 = ef2;
ef2 = ef3;
sum += ef2;
}
return sum;
}
int main()
{
int limit = 400;
cout << evenFibSum(limit);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int evenFibSum(int limit)
{
if (limit < 2)
return 0;
long ef1 = 0, ef2 = 2;
long sum = ef1 + ef2;
while (ef2 <= limit)
{
long ef3 = 4 * ef2 + ef1;
if (ef3 > limit)
break;
ef1 = ef2;
ef2 = ef3;
sum += ef2;
}
return(int) sum;
}
public static void main (String[] args)
{
int limit = 400;
System.out.println(evenFibSum(limit));
}
}
|
Python3
def evenFibSum(limit) :
if (limit < 2) :
return 0
ef1 = 0
ef2 = 2
sm= ef1 + ef2
while (ef2 <= limit) :
ef3 = 4 * ef2 + ef1
if (ef3 > limit) :
break
ef1 = ef2
ef2 = ef3
sm = sm + ef2
return sm
limit = 400
print(evenFibSum(limit))
|
C#
using System;
class GFG {
static int evenFibSum(int limit)
{
if (limit < 2)
return 0;
long ef1 = 0, ef2 = 2;
long sum = ef1 + ef2;
while (ef2 <= limit)
{
long ef3 = 4 * ef2 + ef1;
if (ef3 > limit)
break;
ef1 = ef2;
ef2 = ef3;
sum += ef2;
}
return(int) sum;
}
public static void Main ()
{
int limit = 400;
Console.Write(evenFibSum(limit));
}
}
|
PHP
<?php
function evenFibSum($limit)
{
if ($limit < 2)
return 0;
$ef1 = 0; $ef2 = 2;
$sum = $ef1 + $ef2;
while ($ef2 <= $limit)
{
$ef3 = 4 * $ef2 + $ef1;
if ($ef3 > $limit)
break;
$ef1 = $ef2;
$ef2 = $ef3;
$sum += $ef2;
}
return $sum;
}
$limit = 400;
echo(evenFibSum($limit));
?>
|
Javascript
<script>
function evenFibSum(limit)
{
if (limit < 2)
return 0;
let ef1 = 0, ef2 = 2;
let sum = ef1 + ef2;
while (ef2 <= limit)
{
let ef3 = 4 * ef2 + ef1;
if (ef3 > limit)
break;
ef1 = ef2;
ef2 = ef3;
sum += ef2;
}
return sum;
}
let limit = 400;
document.write(evenFibSum(limit));
</script>
|
Output :
188
Time Complexity: O(n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
14 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...