我試圖解決上項目歐拉的第二個問題,現在的問題是:C#Fibonacci序列複製
在Fibonacci序列中的每個新名詞是通過將前兩個方面產生。通過用1和2開始,第一10項將是:
1,2,3,5,8,13,21,34,55,89,...
找到所有的總和序列中的偶數值不超過四百萬。
所以,我已經設置了以下內容:
using System;
namespace ProjectEuler
{
class Question2
{
//Project Euler - Question 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
static void Main()
{
int sum = 0;
int oldNumber = 1;
int currentNumber = 1;
int nextNumber;
while (currentNumber <= 500)
{
nextNumber = currentNumber + oldNumber;
if (nextNumber % 2 == 0)
{
sum += currentNumber;
}
}
Console.WriteLine("Project Euler - Question 2\n\nAnswer: " + sum);
Console.ReadLine();
}
}
}
當我運行程序,沒有什麼可見的,只需在Windows命令行的光標。我認爲這可能是因爲currentNumber沒有得到更新的事實,但我想不出如何正確地做到這一點,即使是這樣的情況。