Click here to Skip to main content
15,886,362 members
Articles / Web Development / ASP.NET

Bootstrapper for Project Euler in ASP.NET

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
14 Jul 2015CPOL2 min read 4K   2  
Bootstrapper for Project Euler in ASP.NET

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.

euler

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:

C#
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:

C#
// 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.
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:

C#
static void Main(string[] args)
{
	try
	{
		Type interfaceType = typeof(IExcercise);
		//Fetch all types that implement the interface 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)
		{
			//Create a new instance of all found types
			excercises.Add((IExcercise)Activator.CreateInstance(type));
		}
		//Let the user fill in an excercise number
		Console.WriteLine("Which excercise?");
		Console.Write("> ");
		string excerciseIndex = Console.ReadLine();
		IExcercise excercise = excercises.Where
		(e => e.Excercise == excerciseIndex).FirstOrDefault();
		if (excercise != null)
		{
			//If the excercise is found, execute it
			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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
-- There are no messages in this forum --