You are viewing a single comment's thread. Return to all comments →
Actually it does. One can skip few steps Using this formula one will get only the even fibinoacci numbers. Traditional algo will be :
f1=1;
f2=2;
f=0;
while(f1 less than n)
{
if(f%2==0) sum+=f;
f=f1+f2;
f2=f1;
f1=f;
}
print -> sum
However you can skip the step of verifing odd-even. Also you are skiping 2 odd number and directly getting only the even numbers.
f1=2;
f2=0;
while(f1 less than n) {
sum+=f1;
f=4*f1+f2;
PS-Sorry for bad english
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 →
Actually it does. One can skip few steps Using this formula one will get only the even fibinoacci numbers. Traditional algo will be :
f1=1;
f2=2;
f=0;
while(f1 less than n)
{
if(f%2==0) sum+=f;
f=f1+f2;
f2=f1;
f1=f;
}
print -> sum
However you can skip the step of verifing odd-even. Also you are skiping 2 odd number and directly getting only the even numbers.
f1=2;
f2=0;
f=0;
while(f1 less than n) {
sum+=f1;
f=4*f1+f2;
f2=f1;
f1=f;
}
print -> sum
PS-Sorry for bad english