Project Euler Problem 63 Solution

Question

The 5-digit number, 16807=7516807=7^5, is also a fifth power. Similarly, the 9-digit number, 134217728=89134217728=8^9, is a ninth power.

How many n-digit positive integers exist which are also an nth power?

Haskell

main ::  IO ()
main = print $ sum $ [1 | i <- [1..99], n <- [1..99], length (show (i^n)) == n]
$ ghc -O2 -o nPower nPower.hs
$ time ./nPower
real   0m0.026s
user   0m0.024s
sys    0m0.000s

Python

#!/usr/bin/env python
print(sum(1 for i in range(1,100) for x in range(1,100) if len(str(i**x)) == x))
$ time python3 n-power.py
real   0m0.030s
user   0m0.020s
sys    0m0.008s

Questions? Comments? Send me an email: [email protected]