Project Euler – Prolog

Recently I found a website called Project Euler. It is a website, which has a series of problems which can be solved using a mix of math and programming. I like both math and programming, so I really do enjoys the problems. I have only solved a fraction of the problems, but I have already touched many branches of mathematics I usually don’t dabble much in.

It is stated that the problems should be solvable in under one minute once a program has been written. Writing the programs might takes several hours though, and this is the fun challenge is.

In this series of posts, I will gives a run through, and a solution methodology for solving some of the problems (the ones I have solved…), but I will in each case elaborate a bit more on it, than just finding a solution.

The code for these problems will be written in C#, and I wont provide the full code, only the useful snippets. I will use this first blog post on the subject to give you a bit of the structure around the interesting code.

I am solving each problem in its own class, called ProblemXXX, where XXX is the number of the problem. I use the following very simple skeleton for each of the problems

using System;

namespace euler {
    class ProblemXXX {

        public void Solve() {
            DateTime startTime = DateTime.Now;
            int result = 0;

            <Write all the interesting code here>

            DateTime stopTime = DateTime.Now;
            TimeSpan duration = stopTime - startTime;
            Console.WriteLine("The answer to the problem is: {0}", result);
            Console.WriteLine("Solution took {0} ms", duration.Milliseconds);

        public static void Main(String[] args) {
            new ProblemXX().Solve();

In the code in this post, I have added the Main function to run the program inside the class. In my actual solution, I have chosen to have all the problems in one Visual Studio project, and having a separate class containing the Main function, which I can then edit to solve the problem I want to solve. That is mainly a practical issue, so I avoid having to create a new project every time I solve a new problem.

I have one function called Solve(), which as the name suggests solves the problem. The skeleton has a few lines of code in it:

DateTime startTime = DateTime.Now;
<pre>DateTime stopTime = DateTime.Now;
TimeSpan duration = stopTime - startTime;
Console.WriteLine("Solution took {0} ms", duration.Milliseconds);

These lines of code, are there to time the solution, by making a time stamp at the beginning and the end, and then subtracting these from each other. Other than that the skeleton contains a line of code to print the answer to the problem.

On the more philosophical side of programming; I have chosen to make it as an normal method and not as a static method, since I find it more correct in an object oriented programming paradigm. However, it shouldn’t make much of a difference.

While I am at discussing philosophy and preferences; there are many different goals one can set up when solving the problems. Some people try to write as few lines as possible, and some try to squeeze every bit of performance out of it. I am aiming to write some readable code.

This will be it for now, check back for the solution for problem 1.

Posted by Kristian

Leave a Reply