Problem 8

The 8th problem in the project_euler website didn’t offer a lot of resistance. I started by defining the 1000-digit number in Haskell, naming it n. So, I had something like:

    import Char
    import List

    n :: Integer
    n = 73167176531 ... 52963450

(I didn’t see the point of writing the entire number here, since it is very big). Afterwards, it was a matter of defining this simple function:

    f :: Integer -> Int
    f = head . reverse . sort . g . map digitToInt . show

Where g is defined as:

    g (x1:x2:x3:x4:x5:xs) = x1*x2*x3*x4*x5 : g (x2:x3:x4:x5:xs)
    g _ = []

One could argue that it could be more efficient, however my ghci calculates the following execution time: (0.03 secs, 1834872 bytes)

Published: November 04 2009

blog comments powered by Disqus