Using Project Euler to Learn a Programming Language

When I first began learning to develop Silverlight applications, I noticed very quickly that I struggled a lot with C#, even those things that most programmers would consider very basic, such as:

  • i += j is the same as i = i + j;
  • Angle Brackets < > usually identify a generic where a type ought to be specified.
  • What in the world is LINQ?
  • What does static mean?

Since I was struggling so much with C#, I decided to read Illustrated C# 2008 by Daniel Solis.  While it was a great introduction to the language, I still found myself hungering for a way to use C# and thus test my newfound knowledge.  I started a few small projects, but I wasn’t getting excited about working on anything.  I’m talking about the kind of excitement where you don’t want to leave work or go to bed at night because you’re having so much fun writing code.

Then I found Project Euler.

Project Euler ( is a collection of math and programming problems that can be solved using just about any programming language.  While the site is certainly tilted more towards math than programming in most cases, I find it to be an incredibly useful way to learn the nuances of a language and exercise one’s programming skills.

While some of the problems might be easily solved using math or common conventions in computer science, I like to challenge myself to learn new concepts and not simply find the answer to the question.  That’s not to say that the conventional approach isn’t more efficient or correct; it often is. Rather, I’m referring to taking the time to let the problem be an opportunity to learn and try something new versus it being a mere question to be answered.  Let’s walk through an example of any easy problem to see what I mean.

For example, Problem 1 states:

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.

There are in fact several ways to approach this, both mathematically and programmatically.  Going the math route, you can utilize the geometric sum, and an alternative code solution can be as simple as a simple for loop with a little bit of code for logic and addition.  I decided to create a simple array of integers and then try to use LINQ to find the result.  This is the code I used:

static class AddMultiples
    public static int PrintSum(int length)
        int[] arr = new int[length];
        for (int i = 0; i < length; i++)
            arr[i] = i+1;
        return arr.Where(i => (i % 5 == 0) || (i % 3 == 0)).Sum();

I didn’t have to populate an array to find the answer, but I was excited to discover how easy it was to extend the functionality of the array using LINQ.  The return statement of the method has two LINQ extension methods. The first extension method, Where, restricts the data set to only those numbers evenly divisible by 3 or 5, and the second extension method, Sum, adds the results of the data provided by the Where method. Sure, all of this could have been easily set up in a simple for loop, but what’s the fun in that?

If you want to play with numbers and start learning a new programming language, I recommend spending some time at Project Euler.