Saturday, October 22, 2016

Fiding fibonacci series of even numbers upto n

Question Link
Code :
#include<iostream>
#include<vector>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        unsigned long long int i, n, first=0, second = 1, next, sum = 0;
        cin>>n;
        //sum = 0;
        for(i=0;i<=n;i++){
            if(i<=1)
                next = i;
            else{
                next = first + second;
                first = second;
                second = next;
            }
            if((next%2==0)&&(next<=n)){
                sum = sum+next;
                //cout<<next<<" ";
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}
Error : We need more efficient algorithm to correct the error.

No comments:

Post a Comment

Search This Blog