Skip to content

Project Euler Problem 56

December 4, 2011

Project Euler Problem 56 solution:

let sumOfDigits (n: bigint) =
    n.ToString().ToCharArray()
    |> Array.map (fun x -> (int)x - (int)'0') |> Array.sum

let powers ``base`` =
    Seq.unfold (fun (product, exponent) ->
        if (exponent < 100) then
            let interim = product * ``base``
            Some(interim, (interim, exponent + 1))
        else None) (1I,0)
    |> Seq.skip 90 |> Seq.toList

let problem056 () =
    [90I..99I] 
    |> List.collect powers
    |> List.map sumOfDigits
    |> List.max

For comparison, the same in Haskell will take a single (long) line of code:

import Data.Char (digitToInt)
:{
    maximum $map (\x -> foldl (\acc y -> acc + (digitToInt y)) 0 (show x))
        [a^b | a <- [90..99], b <- [90..99]]
:}
Advertisements

From → Project Euler

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: