Skip to content

Problem 40

March 20, 2010

The 40th problem of the project Euler can be solved by taking advantage of the Haskell laziness. As one can see below, I defined an infinite list inf_lst, which contains all positive integers in a string format. As I said, since Haskell is lazy, it will only compute the list if it needs its values. Therefore, it was only a matter of accessing the 6 required positions in the list and calculate its multiplication:

import Char

f = foldr ((*) . digitToInt) 1 l
  where l       = map ((!!) inf_lst) [10^i | i <- [1..6]]
        inf_lst = concat [show i | i <- [0..] ]

The execution time is: (0.32 secs, 69247988 bytes)

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: