Project Euler — Problem 63 Solution

Problem

The 5-dig­it num­ber, 16807=75, is also a fifth pow­er. Sim­i­lar­ly, the 9-dig­it num­ber, 134217728=89, is a ninth pow­er.

How many n-dig­it pos­i­tive inte­gers exist which are also an nth pow­er?

Solution

let naturalNumbers = Seq.unfold (fun state -> Some(state, state+1)) 1

// define function to find the number of n digit numbers which are also nth power
let f n =
    naturalNumbers
    |> Seq.map (fun n' -> pown (bigint(n')) n)
    |> Seq.skipWhile (fun n' -> n'.ToString().Length < n)
    |> Seq.takeWhile (fun n' -> n'.ToString().Length = n)
    |> Seq.length

let answer =
    naturalNumbers
    |> Seq.map f
    |> Seq.takeWhile (fun l -> l > 0)
    |> Seq.sum