I’m attempting to solve the second problem on Project Euler, here is the problem:
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.
So, I have set up the following:
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();
}
}
}
When I run the program, there is nothing visible, just a cursor in the Windows command line. I think that may be do the fact that currentNumber isn’t getting updated, but I can’t think of how to do that properly, if that even is the case.
You don’t have a condition to end your loop. You never change the value of currentNumber to anything but 1.
You probably want something like: