We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
**//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;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #2: Even Fibonacci numbers
You are viewing a single comment's thread. Return to all 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;
}