Even Fibonacci Numbers Sum | Practice | GeeksforGeeks

Given a number N find the sum of all the even valued terms in the Fibonacci sequence less than or equal to N.
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).
 
Example 1:
Input:
N = 8
Output:
10
Explanation:
There are two even 
fibonacci numbers less
than equal to 8, 8 and 2 
and 8 + 2 = 10
Example 2:
Input:
N = 89
Output:
44
Explanation:
There are three even
fibonacci numbers less than
equal to 89, 8, 2 and 34 
8 + 2 + 34 = 44

Your Task:
You don't need to read input or print anything. Your task is to complete the function evenFibSum() which takes an integer N as input parameters and returns the sum of all the Fibonacci number less than equal to N.
 
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
 
Constraints:
1 <= N <= 106
0
dasilashubham392 weeks ago
Easy Java Solution ---→
class Solution{   static long evenFibSum(long N){       // code here       long a=1;       long b=1;       long c=1;       long sum=0;       long mod=1000000007;       while(c<=N){           c=(a+b)%mod;                  if(N>=c && c%2==0){               sum+=c;           }           a=b;           b=c;       }       return sum;   } };
+1
pketul22127 months ago
Total Time Taken: 0.1
class Solution{
    static long evenFibSum(long N){
        
        long sum = 2, a = 1, b = 2, temp = 0;
        
        while(N > temp) {
            temp = a + b;
            a = b;
            b = temp;
            
            if(N > temp && temp % 2 == 0)
                sum += temp;
        }
        
        return sum;
    } 
}
0
saivishnu1289 months ago
class Solution{public:   long long evenFibSum(int N){       int s=0,a=0,b=1,c=a+b;       while(c<=N)       {           c=a+b;           a=b;           b=c;           if(c%2==0&&c<=N)           {               s=s+c;           }       }       return s;       // code here   }};
0
ANKIT SINHA11 months agohttps://uploads.disquscdn.c...Reply Open ExternallyShow 0 RepliesLoading...
ANKIT SINHA
https://uploads.disquscdn.c...
0
Dharmendra Kumar2 years agoexecution time: 0.01 long long a=1,b=0;        int sum=0;        for(int i=1;i<=N;i++){          if(a%2==0)          sum = sum + a;          a=a+b;          b=a-b;          if(a>N)          break;        }        return sum;Reply Open ExternallyShow 0 RepliesLoading...
Dharmendra Kumar
execution time: 0.01
 long long a=1,b=0;        int sum=0;        for(int i=1;i<=N;i++){          if(a%2==0)          sum = sum + a;          a=a+b;          b=a-b;          if(a>N)          break;        }        return sum;
0
Lakhvinder Singh2 years agoc++ 0.01#include <iostream>using namespace std;typedef long long ll;int main() {ll f3 = 1,f2 = 1,f1 = 0;ll dp[1000001] = {0};int i = 1;while(i<=1000000){    if(f3<=i)    {        if(f3%2==0)        {            dp[i] = dp[i-1]+f3;        }        else        {            dp[i] = dp[i-1];        }        f3 = f2+f1;        f1 = f2;        f2 = f3;    }    else    {        dp[i] = dp[i-1];    }    i++;}int t;cin>>t;while(t--){    int n;    cin>>n;    cout<<dp[n]<<endl; }="" return="" 0;="" }="" <="" code="">Reply Open ExternallyShow 0 RepliesLoading...
Lakhvinder Singh
c++ 0.01#include <iostream>using namespace std;typedef long long ll;int main() {ll f3 = 1,f2 = 1,f1 = 0;ll dp[1000001] = {0};int i = 1;while(i<=1000000){    if(f3<=i)    {        if(f3%2==0)        {            dp[i] = dp[i-1]+f3;        }        else        {            dp[i] = dp[i-1];        }        f3 = f2+f1;        f1 = f2;        f2 = f3;    }    else    {        dp[i] = dp[i-1];    }    i++;}int t;cin>>t;while(t--){    int n;    cin>>n;    cout<<dp[n]<<endl; }="" return="" 0;="" }="" <="" code="">
0
Jainav2 years agoWould like to know about a better solution to this than:#include <iostream>using namespace std;int main() {//codeint t;cin>>t;while(t--){    int n;    cin>>n;    unsigned int a = 1, b =  1,c=0;    while(n>=a+b){            c = c+a+b;        a = a+2*b;            b = 2*a-b;    }    cout<<c<<endl; }="" return="" 0;="" }="">Reply Open ExternallyShow 0 RepliesLoading...
Jainav
Would like to know about a better solution to this than:#include <iostream>using namespace std;
int main() {//codeint t;cin>>t;while(t--){    int n;    cin>>n;    unsigned int a = 1, b =  1,c=0;    while(n>=a+b){            c = c+a+b;        a = a+2*b;            b = 2*a-b;    }    cout<<c<<endl; }="" return="" 0;="" }="">
0
Harsh K2 years ago#include <bits stdc++.h="">using namespace std;typedef long long ll;int main() {//codeint t;cin>>t;while(t--){    int n;cin>>n;    ll dp[n];    dp[0]=1;    dp[1]=1;   unsigned long long ans=0;    for(int i=2;i<n;i++) {="" dp[i]="dp[i-1]+dp[i-2];" if(dp[i]%2="=0" &&="" dp[i]<="n)" ans+="dp[i];" if(dp[i]="">n)        break;    }    cout<<ans<<endl; }="" return="" 0;="" }="">Reply Open ExternallyShow 0 RepliesLoading...
Harsh K
#include <bits stdc++.h="">using namespace std;
typedef long long ll;
int main() {//codeint t;cin>>t;while(t--){    int n;cin>>n;    ll dp[n];    dp[0]=1;    dp[1]=1;   unsigned long long ans=0;    for(int i=2;i<n;i++) {="" dp[i]="dp[i-1]+dp[i-2];" if(dp[i]%2="=0" &&="" dp[i]<="n)" ans+="dp[i];" if(dp[i]="">n)        break;    }    cout<<ans<<endl; }="" return="" 0;="" }="">
0
Soumya Sekhar2 years agofor the 2nd case:the sum must be greater than 400But in question it is given 188 which is less than 400Reply Open ExternallyShow 1 RepliesLoading...
Soumya Sekhar
for the 2nd case:the sum must be greater than 400But in question it is given 188 which is less than 400
0
Abhinay Lakhera2 years agotry this one....long long  int t;cin>>t;while(t--){    long  long int n;    cin>>n;   long  long  int x[n];    long  long int s=0;    for(long  long int i=1;i<=n;i++)    {        x[1]=1;        x[2]=1;        if(i>2)        x[i]=x[i-1]+x[i-2];        if(x[i]%2==0&&x[i]<=n)        s=s+x[i];        if(x[i]>n)            {              break;            }    }    cout<<s<<endl; }="" return="" 0;="">Reply Open ExternallyShow 0 RepliesLoading...
Abhinay Lakhera
try this one....
long long  int t;cin>>t;while(t--){    long  long int n;    cin>>n;   long  long  int x[n];    long  long int s=0;    for(long  long int i=1;i<=n;i++)    {        x[1]=1;        x[2]=1;        if(i>2)        x[i]=x[i-1]+x[i-2];        if(x[i]%2==0&&x[i]<=n)        s=s+x[i];        if(x[i]>n)            {              break;            }    }    cout<<s<<endl; }="" return="" 0;="">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.