Project Euler 63

Project Euler 63

题目

Powerful digit counts

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 \(n^\text{th}\) power?

解决方案

可以发现,\(10^n\)是一个\(n+1\)位数。因此,如果一个数\(a^n\)\(n\)位数,那么\(a\leq 9\)

\(9^n\)的位数小于\(n\)位时,统计结束。(因为\(n\)就算增加\(1\)\(9^n\)再乘一个\(9\),也没办法使积的位数增加多于\(1\)位,变成\(n+1\)位)。

代码

1
2
3
4
5
6
7
8
9
10
11
from itertools import count

ans = 0
for n in count(1, 1):
if len(str(9 ** n)) < n:
break
for a in range(1, 10):
if len(str(a ** n)) == n:
ans += 1
print(ans)

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
Ujimatsu Chiya 微信 微信
Ujimatsu Chiya 支付宝 支付宝