Problem #63 says:
The 5-digit number, 16807=7^(5), is also a fifth power. Similarly, the 9-digit number, 134217728=8^(9), is a ninth power.
How many n-digit positive integers exist which are also an nth power?
The main question for this solution is determining the range to go through and check for numbers. I decided to use 1 – 100 for the bases and exponents. And since this is right up f#’s ally I want ahead and wrote the solution in that, using list comprehension and pattern matching to make short work of the problem. Not counting the code for the timer the solution only takes 14 lines of f# to solve.
#light
open System
open Microsoft.FSharp.Math
open System.Diagnostics
let sw = new System.Diagnostics.Stopwatch()
sw.Start()
let expcheck (x:BigInt,y:BigInt) = BigInt (((x) ** (y)).ToString().Length) = y
let numbers =
seq {
for x in 1 .. 100 do
for y in 0 .. 100 ->(BigInt x,BigInt y)
}
let answer =
numbers
|> Seq.filter expcheck
|> Seq.length
sw.Stop()
Console.WriteLine(answer)
Console.WriteLine(sw.Elapsed)
Console.ReadKey()