The Problem
The series, 11 + 22 + 33 + ... + 1010 = 10405071317.
Find the last ten digits of the series, 11 + 22 + 33 + ... + 10001000.
The Solution
I don't know what people working in C are doing to solve these problems that involve enormous
integer values. But fortunately we're on the .NET stack and we have BigInteger
. It makes solving
this problem trivial.
let sum =
Seq.unfold (fun state -> Some(state, state+1)) 1
|> Seq.map (fun n -> pown (bigint n) n)
|> Seq.take 1000
|> Seq.sum
let str = string sum
str.Substring(str.Length - 10)
We start out by generating natural numbers with Seq.unfold
. We use Seq.map
to raise each element
to its own power using pown
. Then we take the first 1000 elements of that sequence. Then we sum
those.
Next, we convert that number to a string. If you're curious, that string has 3001 characters in it.
Last, we use Substring
to get the last 10 digits of the string. That is our answer.