Project Euler #2: Even Fibonacci numbers

  • + 5 comments

    This question actually has an easy logic. Lets the fibonacci series:1 1 2 3 5 8 13 21 34 55 89 144

    Now separate the even numbers :- 2 8 34 144

    • 8 = 2*4 + 0
    • 34 = 8*4 + 2
    • 144 = 34*4 + 8
    • 610 = 144*4 + 34

    GENERAL FORM

    • beginning = 2
    • previous = 0
    • IN LOOP:
    • (next_even) = beginning * 4 + previous
    • //calcuate sum
    • previous = beginning
    • beginning = next_even
    • + 0 comments

      good one

    • + 1 comment

      This is very similar to what I have done, and should be the most efficient. Not the one posted in the editorial!

      • + 0 comments

        Where is the editorial?

    • [deleted]
      + 0 comments

      good mate in python you can throw in a generator is fast so you dont have to generate the odd numbers In c++ you can make with iterators It works with coroutines tho is not o good idea.

    • + 0 comments

      The most efficient solution

    • [deleted]
      + 0 comments

      Passed all cases.....nice logic

      C++

              long long int n;
              cin>>n;
              long long int curr=0,prev=0,ans=0,next;
              curr=2;
              prev=0;
              ans=2;
              for(int i=1;i<=n;i++)
              {
                  next=4*curr + prev;
                  if(next>n)
                  {
                      cout<<ans<<endl;
                      break;
                  }
                  ans=ans+next;
                  prev=curr;
                  curr=next;
                  
              }