Not so long ago, I’ve discovered Project Euler. This website contains all kinds of programming excercises. The excercises may look simple, and you can do some by hand, but to get the solution to most excercises, you need to do some programming. At first, I did the excercises using JavaScript (in io.js). I like JavaScript for doing small things, but when it comes to certain things (like very big integers), it can be a hell. That’s why I chose to switch over to ASP.NET.
In essence, the bootstrapper project I’ve created is a console app. When you start the app, you can type in the number of the excercise which should be executed, and you’ll see the result in the console window.

How did I accomplish this? Let’s see:”
First, I’ve created a solution with 2 projects: the first is the console project and the second is the project which contains the interface, all excercises and other stuff which some excercises might need (like a resources file to store strings and some shared helper methods).
Second, I created the interface that every excercise class will implement:
public interface IExcercise
{
string Excercise { get; }
void Execute();
}
The “Excercise
” property returns the name (or number) of the excercise. So when the property “Excercise
” of a specific excercise returns “1
”, that excercise will be executed when you fill in “1
” in the console app. We will see how this is done later on.
The method “Execute
”, like the name says, executes the excercise
.
Below, you’ll see a full implementation of the first Project Euler excercise
:
public class Exc1 : IExcercise
{
public string Excercise
{
get
{
return "1";
}
}
public void Execute()
{
int sum = 0;
for (int i = 0; i < 1000; i++)
{
if ((i % 3 == 0 || i % 5 == 0) && i != 0)
{
sum += i;
}
}
Helper.WriteLineFormat("Sum: {0}", sum);
}
}
This should work. The only thing we need to do now is to evaluate the number filled in on the console and execute the corresponding excercise. This piece of code should be placed in the console project:
static void Main(string[] args)
{
try
{
Type interfaceType = typeof(IExcercise);
Type[] types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass)
.ToArray();
List<iexcercise> excercises = new List<iexcercise>();
foreach (Type type in types)
{
excercises.Add((IExcercise)Activator.CreateInstance(type));
}
Console.WriteLine("Which excercise?");
Console.Write("> ");
string excerciseIndex = Console.ReadLine();
IExcercise excercise = excercises.Where
(e => e.Excercise == excerciseIndex).FirstOrDefault();
if (excercise != null)
{
excercise.Execute();
}
else
{
throw new ArgumentException(string.Format
("Excercise with key {0} not found", excerciseIndex));
}
}
catch (Exception e)
{
Console.WriteLine(string.Format("Caught exception: {0}", e.Message));
}
finally
{
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
These are the steps needed to create a simple bootstrapper for Project Euler. I’ve created a Github Repo which contains the complete Bootstrapper project + excercise 1.
CodeProject