views:

182

answers:

2

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Find the sum of all the even-valued terms in the sequence which do not exceed four million.

        Int64[] Numeros = new Int64[4000005];
        Numeros[0] = 1;
        Numeros[1] = 2;

        Int64 Indice = 2;
        Int64 Acumulador = 2;

        for (int i = 0; i < 4000000; i++)
        {
            Numeros[Indice] = Numeros[Indice - 2] + Numeros[Indice - 1];

            if (Numeros[Indice] % 2 == 0)
            {
                if ((Numeros[Indice] + Acumulador) > 4000000)
                {
                    break;
                }
                else
                {
                    Acumulador += Numeros[Indice];
                }
            }

            Indice++;
        }

        Console.WriteLine(Acumulador);
        Console.ReadLine();

My program isn't functioning as it should be I guess because on Project Euler they say my answer is incorrect. Maybe I'm overlooking something. Any help?

+2  A: 

This line

if ((Numeros[Indice] + Acumulador) > 4000000)

is checking for the sum being greater than 4MM. You need to check that the term (Numeros[Indice]) is greater than 4MM. So changing that to this...

if (Numeros[Indice] > 4000000)

is probably a good place to start.

Jason Punyon
A: 

And also man your test condition in for loop is useless.. i wil never reach the value of 4000000. simply a

while(1){
//code
}

will also do, no need of i as u never use it

Neeraj