1Jul/080
Four Millionacci – Project Euler #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.
Code:
using System;
namespace Problem_2
{
class Entry
{
static void Main(string[] args)
{
int limit = 4000000;
int a = 1;
int b = 2;
int c = 0;
int sum = 0;
while (b < limit)
{
c = a + b;
a = b;
b = c;
if (a % 2 == 0)
sum += a;
}
Console.WriteLine(sum);
Console.ReadLine();
}
}
}
Answer:
4613732
30Jun/080
FizzBuzz Plus – Project Euler #1
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
Code:
using System;
namespace Problem_1
{
class Entry
{
static void Main(string[] args)
{
Int32 sum = 0;
//Sum all multiples of 3 below 1000
for (int i = 3; i < 1000; i += 3)
{
sum += i;
}
//Add all multiples of 5 below 1000 to that
for (int j = 5; j < 1000; j += 5)
{
//Don't count multiples of both twice
if (j % 3 != 0)
sum += j;
}
System.Console.WriteLine(sum.ToString());
System.Console.ReadLine();
}
}
}
Answer:
233168