Sum of even fibonacci numbers

Posted by user300484 on Stack Overflow See other posts from Stack Overflow or by user300484
Published on 2010-04-04T14:04:19Z Indexed on 2010/04/10 12:13 UTC
Read the original article Hit count: 230

Filed under:
|
|
|

This is a Project Euler problem. If you don't want to see candidate solutions don't look here.

Hello you all! im developping an application that will find the sum of all even terms of the fibonacci sequence. The last term of this sequence is 4,000,000 . There is something wrong in my code but I cannot find the problem since it makes sense to me. Can you please help me?

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            long[] arr = new long [1000000] ;
            long i= 2;
            arr[i-2]=1;
            arr[i-1]=2;
            long n= arr[i];

           long s=0;
            for (i=2 ; n <= 4000000; i++)
            {

                arr[i] = arr[(i - 1)] + arr[(i - 2)];
            }
            for (long f = 0; f <= arr.Length - 1; f++)
            {
                if (arr[f] % 2 == 0)
                    s += arr[f];
            }
            Console.Write(s);
            Console.Read();

        }
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about fibonacci