Project Euler #2: Even Fibonacci numbers

  • + 0 comments

    **//1,1,2,3,5,8,13,21,34,55,89,144 //using the knowledge that after every two terms comes an even term //Sum of two odds is an even(two oods then an even) ** int main(){ int t; cin >> t; for(int a0 = 0; a0 < t; a0++){ long n; cin >> n; long a1=1; long a2=1;

        long sum=a1+a2;
        while(a1+2*a2<n){
            long x=2*a2+a1;
            a2=x+a1+a2;
            a1=x;
            sum+=a1+a2;
    
        }
                //if the current term was greater than n, subtract their sum from the overall sum
        if(a1+a2>n){
            sum-=(a1+a2);
        }
        cout<<sum<<endl;
    }
    return 0;
    

    }