# Project Euler Problem 56

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

Leave a Comment