60 Days of Euler in F# - Problem 16

The Problem

Compute 21000 and sum its digits.

The Solution

Like Problem 13, this problem is trivially easy because of .NET's BigInteger data type.

(pown 2I 1000).ToString()
|> Seq.map (fun c -> int32(c) - int32('0'))
|> Seq.sum

The pown function returns 21000 as a BigInteger. (Suffixing a numeric literal with I means it is a BigInteger literal)

Strings can be treated as Seq<char> and that's what we do here. We convert each digit into an integer and then finally sum the result.

Other Posts in This Series