Skip to content

Problem 8

November 4, 2009

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 _ = []

You can argue that it could be more efficient, however my ghci calculates the following execution time:

(0.03 secs, 1834872 bytes)

Which is good enough 🙂

Advertisements
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: