8.30.2012

Project Euler Problem 2: Find the sum of the even-valued terms in the Fibonacci sequence

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, ... By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

The answer is 4613732.

First, find all numbers in Fibonacci sequence that is less than 4 million. Then add up all numbers that are even.
List<int> listFibonacci = new List<int> { 1, 2 };
int sum = 0;
int numFibonacci = 0;

while (listFibonacci[listFibonacci.Count - 1] < 4000000)
{
   numFibonacci = listFibonacci[listFibonacci.Count - 1] + listFibonacci[listFibonacci.Count - 2];
   if (numFibonacci < 4000000) listFibonacci.Add(numFibonacci);
      else break;                
}

for (int i = 0; i

8.29.2012

Project Euler Problem 7: Find the 10001st prime number

Problem 7:
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number?
The answer is 104743.

            List listPrime = new List {3, 5, 7, 11, 13};

            bool flagPrime = true;
            int num = listPrime.ElementAt(listPrime.Count - 1);

            while (listPrime.Count < 10000)
            {                
                num = num + 2;
                flagPrime = true;
                foreach (int j in listPrime) 
                {
                    if (num % j == 0)
                    {
                        flagPrime = false;
                        break;
                    }
                }

                if (flagPrime == true)  listPrime.Add(num);
            }

            Console.WriteLine("10001st Prime number = {0}", listPrime.Last();


8.27.2012

Project Euler Problem 6: Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum

Problem 6: The sum of the squares of the first ten natural numbers is,
1^2 + 2^2 + ... + 10^2 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)^2 = 55^2 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025-385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

The answer is 25164150.

Because
(a+b+c+....+n)^2
= a^2 + b^2 + c^2 + .... n^2 + 2(ab+ac+...+an+bc+bd+...+bn+cd+ce+...cn+....+(n-1)n)
The difference between the sum of the squares of the first ten natural numbers and the square of the sum is
(a+b+c+....+n)^2 - (a^2 + b^2 + c^2 + .... n^2)
= 2(ab+ac+...+an+bc+bd+...+bn+cd+ce+...cn+....+(n-1)n)

            int x = 0, y = 0;
            int diff = 0;

            for (int i = 1; i <=100; i++)
            {
                for (int j = i + 1; j <=100; j++)
                    diff += 2 * i * j;
            }

            Console.WriteLine("Answer = {0}", diff);

Project Euler Problem 1: Find the sum of all the multiples of 3 or 5 below 1000

Problem 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.

The answer is 233168.

The first solution I had was straight forward: check the reminder of each number below 1000 and added it to Sum if it's multiples of 3 or 5.
            int Sum = 0;

            for (int x = 1; x < 1000; x++)
            {
                if (x % 3 == 0) Sum += x;
                else if (x % 5 == 0) Sum += x;
                Console.WriteLine("x = {0}, sum = {1}", x, Sum);
            }
Another solution is using the formula (1+2+3+....+n) = (1+n)n/2
Once we find the largest number, we can use this formula to calculate the sum:
 
           int sumOf3 = 0;
            int sumOf5 = 0;
            int sumOf15 = 0;

            for (int i = 999; i > 0; i--)
            {
                if (i % 3 == 0)
                {
                    sumOf3 = (3 + i) * (i / 3) / 2; //remember to start with 3
                    break;
                }
            }

            for (int j = 999; j > 0; j--)
            {
                if (j % 5 == 0)
                {
                    sumOf5 = (5 + j) * (j / 5) / 2; //remember to start with 5
                    break;
                }
            }


            for (int k = 999; k > 0; k--)
            {
                if (k % 15 == 0)
                {
                    sumOf15 = (15 + k) * (k / 15) / 2; //remember to start with 15
                    break;
                }
            }

            Console.WriteLine("Sum of 3 = {0}", sumOf3);
            Console.WriteLine("Sum of 5 = {0}", sumOf5);
            Console.WriteLine("Sum of 15 = {0}", sumOf15);
            Console.WriteLine("Solution = {0}", sumOf3 + sumOf5 - sumOf15);