Project Euler Problem #20

Home/Uncategorized/Project Euler Problem #20

I’m beginning to learn F# and the best way for me to go about that is to start applying it. There are some interesting computational problems over at Project Euler . This post is the continuation of a series of posts each involving a separate Project Euler problem. This post involves solutions to Project Euler problem #20.

n! means n × (n − 1) × … × 3 × 2 × 1

Find the sum of the digits in the number 100!

With F#’s BigInt support this was easy, straight-forward, and fast:

    let swap (a, b) = (b, a)
    let rec digits x =
        let getNextDigit num =
            if num <= 0I then None else 
            (num, 10I) |> BigInt.DivRem |> swap |> Some
        Seq.unfold getNextDigit x
    let answer = BigInt.Factorial >> digits >> Seq.sum

Leave a Comment