title: What is wrong with this algorithm?
body: <p>I was trying to solve <strong>Project Euler Problem #2</strong> : <em>Find the sum of the even terms of the fibonaci series upto 4,000,000.</em> Ex : Upto 1000, the sum is 2+8+34+144+610 = 798.</p>
  
  <p>Now, my algo was to add every third fibonaci number from 2 onwards - as every third fibonaci number is bound to be even following the logic:</p>
  
  <p>Even : E, Odd : O,</p>
  
  <pre><code>O+E=O    E+O=O    O+O=E
  1+2=3    2+3=5    5+3=8    (example)
  </code></pre>
  
  <p>So, I wrote the following code to figure out the answer..</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #define LT 4000000
  int main()
  {
      double i0,i1,sum=0,cycle,eSum=2,status=1;
      i0 = 1;
      i1 = 2;
  
      while(i1&lt;LT &amp;&amp; status == 1)
      {
          for(cycle=3;cycle&gt;0;cycle--)
          {
              sum=i0+i1;
              i0=i1;
              i1=sum;
  
              if((i1+i0)&gt;LT)
              {
                  status = 0;
                  break;
              }
          }
          eSum+=(status == 1)?sum:0;
      }
      printf("\nThe required Answer: %8.0f\n",eSum);
      return 0;
  }
  </code></pre>
  
  <p>Now, it <em>works correctly for <strong>LT = 1000</em></strong>, but for the problem, which requires LT=4,000,000, the program shows a wrong value of <strong>1089154</strong>, instead of the correct value of <strong>4613732</strong>.</p>
  
  <p>I can't figure out what is wrong with this code. Also, I don't understand how it can work correctly for LT=1000, but not for a greater number.. Am I missing something embarrassingly obvious? Please Help..</p>