Project Euler N2 - 斐波那契算法无法正常工作
斐波那契数列中的每个新项 序列是通过添加 前两个术语。从 1 开始 2、前 10 项为:
1、2、3、5、8、13、21、34、55、89、...
求所有偶数的总和 序列中不存在的项 超过四百万。
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();
我猜我的程序没有正常运行,因为在欧拉项目上他们说我的答案不正确。也许我忽略了一些事情。有什么帮助吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此行
检查总和是否大于 4MM。您需要检查项 (Numeros[Indice]) 是否大于 4MM。因此,将其更改为...
可能是一个不错的起点。
This line
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...
is probably a good place to start.
而且你的 for 循环中的测试条件是无用的。
i
将永远不会达到 4000000 的值。只需 a
也可以,不需要
i
因为你从不使用它And also man your test condition in for loop is useless..
i
wil never reach the value of 4000000.simply a
will also do, no need of
i
as u never use it