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?