Project Euler #2: Even Fibonacci numbers

  • + 2 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