// you’re reading...

Project Euler Solutions

Project Euler Problem 63 Solution

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
Loading ... Loading ...

Problem Description

The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is a ninth power.

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

Analysis

Note: log() = log10; ln() = loge. One way to know how many digits a number has is to use int(log n) +1 or if you don’t have a log function then use the natural log function as int(ln n / ln 10) +1. For this problem n cannot be greater then 10 since 10n is always n+1 digit long.

So we need to create a function f such that f(n) = upper limit of n. Using our log function and using 9 as an example we define f(9) as int (1 / (1 – log10 9)) or 21 such that 9n < 10n-1. Thus 9n is n digits long when n ≤ 21. We repeat this for 1 through 8 and sum for an answer.

Solution

n int( 1 / (1-log10 n ) )
1 1
2 1
3 1
4 2
5 3
6 4
7 6
8 10
9 21

Comments

We didn’t see a need to write a program for this one.

Discussion

No comments for “Project Euler Problem 63 Solution”

Post a comment