Euler #8 : Largest Product

by Nicolas Wu

Posted on 19 August 2010

Tags: Haskell, Euler

This week, our Project Euler puzzle is:

Discover the largest product of five consecutive digits in the 1000-digit number.

The number we’ve been given is 1000 digits long, so rather than writing this number as an Integer, it’s easier to express it as a string first (since the formatting looks better for this post), and to convert it to something more sensible afterwards.

To do that, we’ll be making use of an external module, called Data.Char, which exports a function, digitToInt, that converts between values of type Char and Int. Module imports in Haskell must come before function definitions, so we’ll put the import line right here:

import Data.Char (digitToInt)
import Data.List (tails)

Written as a string, the 1000 digit number we’ve been given is:

num :: String
num =

It really makes more sense to turn this little beast into a list of Int, rather than in its current form. We’ll do this by mapping the digitToInt function of Data.Char onto our list num:

nums :: [Int]
nums = map digitToInt num

Now that we have our list of integers, we can think about how we can find the product of all the sequences of consecutive digits that are 5 long.

Maybe, Just, Nothing

We’ll start by first extracting all the lists that are 5 digits long. To do this, we’ll define the function nibble, which takes a number n, and a list xs, and returns the first subsequence of xs of length n, paired up with the result of removing the first value in xs.

nibble :: Int -> [a] -> Maybe ([a], [a])
nibble n xs
  | length xs < n = Nothing
  | otherwise     = Just $ (take n xs, tail xs)

Since not every list xs has a length greater or equal to n, we use a Maybe type in conjunction with our returned pair of lists.

Values of type Maybe have the following defintion:

data Maybe a = Nothing | Just a

In other words, something of type Maybe is either Nothing, which contains no data, or Just a which contains a value of type a. In our case, the function returns Nothing when no list of length n can be extracted from xs, and otherwise returns the first list of length n, along with the nibbled rest of xs.

We can use nibble to define a function that produces all the subsets of xs that have length n. Basically, all we need is a case analysis on the result of nibbling xs. If no nibble can be taken, then we simply return the empty list. However, if a nibble can be taken, then we prepend the list ys of length n to the front of the result of nibbling zs:

nibbles :: Int -> [a] -> [[a]]
nibbles n xs = case nibble n xs of
  Nothing       -> []
  Just (ys, zs) -> ys : nibbles n zs

The solution for our problem then becomes relatively simple. First we produce all the lists of length 5, and apply the product function to each of those by using a map. We get our final result by applying the maximum function.

euler8  = maximum . map product . nibbles 5 $ nums


Another way of obtaining the solution is to use the tails function, which returns all the suffixes of a list. The tails function is part of the Data.List module, which we imported at the beginning of this post. As an example of its use, let’s see what tails returns for the list [1..4]:

Prelude Data.List> tails [1..4]

By using this function, we can build a different version of nibbles', where we take n elements from each of the sublists of the result of tails. Since the last n-1 elements of the list returned will have lengths shorter than n, so we reverse the list and drop those elements:

nibbles' :: Int -> [a] -> [[a]]
nibbles' n xs = drop n . reverse . map (take n) . tails $ xs

This function returns the reversed output of nibbles, which isn’t a problem in our case, since maximum is associative:

euler8' = maximum . map product . nibbles' 5 $ nums


This week we’ve seen two rather different approaches to solving our problem, one using Maybe types, and another using tails.

  • Maybe types capture the notion of a failed computation or an invalid result.
  • Function composition can give us very succinct definitions (like in our nibbles' definition).