Monday, 9 February 2009

Project Euler Problem 8

Problem 8
"Find the greatest product of five consecutive digits in the 1000-digit number.


The first step here is to find a representation for that fairly humungous number. Obviously it's not going to fit into a paltry 32-bit int...but then we don't need it to. The problem description requires us to think in terms of smaller (5-digit) numbers, not one giant 1000-digit number.

So, it is sufficient for us to consider the number as an enumerable stream of single digits, which we can conveniently represent as IEnumerable. I could use a macro to convert the number into a collection initialiser, but it's much easier to treat the string as an IEnumerable<char> and let LINQ do the heavy lifting.
var nums = Enumerable.AsEnumerable(
        "73167176531330624919225119674426574742355349194934" +
        "96983520312774506326239578318016984801869478851843" +

        // ..... etc etc ......

        "05886116467109405077541002256983155200055935729725" +
        ).Select(x => Convert.ToInt32(x.ToString()));

This gives us an IEnumerable<int> containing every digit in the 1000-digit number. Now, the 'obvious' way to solve the problem is to iterate through the collection, and at each index multiply the value against the next four indexes. A simple loop should deal with it:
private static int SimpleSolver(int[] ints)
    int max = 0;
    for (int i = 0; i < ints.Length - 4; i++)
        int tmp = ints[i] * ints[i + 1] * ints[i + 2]
            * ints[i + 3] * ints[i + 4];
        max = Math.Max(max, tmp);

    return max;

As ever, though, that's pretty ugly - the loop condition and product calculation is tied to the sequence size of 5, and messing with an index variable is tedious.

An alternative approach is to take advantage of LINQ's Skip and Take methods to split the problem domain into overlapping 'slices'. Similar to the for loop above, the core of the approach is to iterate through the digits, and at each digit grab a number of subsequent digits and calculate the product.

Lets look at the 5-digit slices available from the first 10 digits:
  7   3   1   6   7   1   7   6   5   3
|       73167       |
    |       31671       |
        |       16717       |
            |       67176       |
                |       71765       |
                    |       17653       |

We can use Skip to progressively move the starting index forward, and Take to grab the 5 digits we need. So, starting with i=0, each successive slice can be sliced from the whole with:
var slice = ints.Skip(i++).Take(5);

To calculate the product of the digits in the slice, we can use the Aggregate operation:
slice.Aggregate(1, (curr, next) => curr*next);

We've met Aggregate before - it's basically a fold, which collapses a sequence to a single item by repeatedly applying an operation to an accumulating result.

This can all be wrapped up as an iterator block, like so:
private static IEnumerable EnumerateSlices(
        IEnumerable ints, int sliceSize)
    int i = 0;
    while (true)
        var slice = ints.Skip(i++).Take(sliceSize);

        if (slice.Count() < sliceSize)
            yield break; // end

        yield return slice.Aggregate(1,
                (curr, next) => curr*next);

Note the termination condition - when we have enumerated every slice, our next slice will contain only 4 elements (3, 4, 5, and 0 from the end of the sequence) - that's our cue to exit the loop.

Also note that this approach makes the algorithm trivial to parameterize - it will work just as well with slice sizes other than 5.

This iterator will produce an IEnumerable containing the products of all slices, so the final step is to select the largest:
var result = EnumerateSlices(nums, 5).Max();


  1. Blast, that didn't go well, posting-wise....

  2. [...] at Basildon Coder he has chosen to represent the input as an list of integers, that means he will not have to convert [...]

  3. [...] I have chose to represent the input number as a string, and then converting them on the fly.Over at Basildon Coder he has chosen to represent the input as an list of integers, that means he will not have to convert [...]