Archive

Archive for the ‘Project Euler’ Category

Project Euler Problem 5

October 28th, 2009 Dallas 1 comment

This one takes three or four seconds to run, so it’s a likely candidate for a rewrite.

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Project_Euler_5
{
 class Program
 {
  static void Main(string[] args)
  {
   new Program();
  }
 
  Program()
  {
   for (int i = 1; ; i++)
   {
    if(isEvenlyDivisibleByRange(i, 11, 20))
    {
     Console.WriteLine(i);
     Console.ReadKey();
    }
   }
  }
 
  bool isEvenlyDivisibleByRange(int value, int lowerBound, int upperBound)
  {
   for (int i = lowerBound; i <= upperBound; i++)
    if (value % i != 0)
     return false;
 
   return true;
  }
 }
}
Categories: Project Euler Tags: ,

Project Euler Problem 4

October 28th, 2009 Dallas 1 comment

This one was easier than I expected. I considered using some sort of lookup table to limit unnecessary calculations (103 x 104 = 104 x 103), but I’m pretty sure that would have only created more overhead.

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Project_Euler_4
{
 class Program
 {
 
  static void Main(string[] args)
  {
   new Program();
  }
 
  Program()
  {
 
   int largestPalindromicProduct = 0;
 
   for (int i = 100; i <= 999; i++)
    for (int j = 100; j <= 999; j++)
    {
     int product = i * j;
 
     if (product > largestPalindromicProduct && isPalindromicNumber(product))
      largestPalindromicProduct = product;
    }
 
   Console.WriteLine(largestPalindromicProduct);
   Console.ReadKey();
  }
 
  bool isPalindromicNumber(int number)
  {
   string value = number.ToString();
 
   for (int i = 0; i < value.Length; i++)
   {
    if (!value[i].Equals(value[value.Length - 1 - i]))
     return false;
   }
 
   return true;
  }
 }
}
Categories: Project Euler Tags: ,

Project Euler Problem 3

October 6th, 2009 Dallas 3 comments

This one was actually a little bit of a challenge.  My first attempt got the right answer, but took about 3 hours to do so.  After consulting some online resources (my girlfriend), and rediscovering prime factorization I rewrote  the program.  Now it takes under a second to solve the puzzle.

I thought this was interesting: if you search “prime factor of 600851475143” on Wolfram Alpha it gives you all of the factorizations, including the answer.

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Project_Euler_3
{
 class Program
 {
  static void Main(string[] args)
  {
   new Program();
  }
 
  Program()
  {
   ulong compositeValue = 600851475143;
   ulong compositeFactor = compositeValue;
   bool isDone = false;
 
   while (!isDone)
   {
    ulong prime = getNextPrime();
 
    while (compositeFactor % prime != 0)
     prime = getNextPrime(prime);
 
    compositeFactor /= prime;
 
    if (isPrimeValue(compositeFactor))
     isDone = true;
   }
 
   Console.WriteLine(compositeFactor);
   Console.ReadKey();
  }
 
  ulong getNextPrime()
  {
   return getNextPrime(0);
  }
 
  ulong getNextPrime(ulong value)
  {
   ulong nextPossiblePrime = value + 1;
 
   for (; !isPrimeValue(nextPossiblePrime); nextPossiblePrime++) ;
 
   return nextPossiblePrime;
  }
 
  bool isPrimeValue(ulong value)
  {
   for (ulong i = 1; i <= (ulong)Math.Ceiling(Math.Sqrt(value)); i++)
   {
    if (value == 1)
     return false;
    if (i == value)
     return true;
    else if (value % i == 0 && i != 1)
     return false;
   }
 
   return true;
  }
 }
}
Categories: Project Euler Tags: ,

Project Euler Problem 2

October 5th, 2009 Dallas No comments

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Project_Euler_2
{
 class Program
 {
  static void Main(string[] args)
  {
   new Program();
  }
 
  Program()
  {
   List<ulong> fibonacciSequence = new List<ulong>();
   fibonacciSequence.Add(0);
   fibonacciSequence.Add(1);
 
   ulong sum = 0;
   bool isDone = false;
 
   for (int i = fibonacciSequence.Count; !isDone ; i++)
   {
    ulong nextFibonacciNumber = fibonacciSequence[i - 1] + fibonacciSequence[i - 2];
 
    fibonacciSequence.Add(nextFibonacciNumber);
 
    if(nextFibonacciNumber > 4000000)
     isDone = true;
    else
     if (nextFibonacciNumber % 2 == 0)
      sum += nextFibonacciNumber;
   }
 
   Console.WriteLine(sum);
   Console.ReadKey();
  }
 }
}
Categories: Project Euler Tags: ,

Project Euler Problem 1

October 5th, 2009 Dallas No comments

To keep the mental gears spinning I’m going through the Project Euler problems.  I’m going to solve them primarily in C#, but I may jump between languages depending on the problem.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Project_Euler_1
{
 class Program
 {
  static void Main(string[] args)
  {
   new Program();
  }
 
  Program()
  {
   int sum = 0;
 
   for (int i = 1; i < 1000; i++)
    if (i % 3 == 0 || i % 5 == 0)
     sum += i;
 
   Console.WriteLine(sum);
   Console.ReadKey();
  }
 }
}
Categories: Project Euler Tags: ,