Problem 63

原文链接: http://www.cnblogs.com/noonjuan/p/11060470.html

Problem 63

https://projecteuler.net/problem=63

Powerful digit counts

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

五位数16807同时是7的五次方,同样地,九位数134217728是8的九次方。

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

有多少个n位数正整数,这些正整数同时是某个数字的n次方?

from power import power

count = 0
for i in range(1, 100):
    for p in range(1, 100):
        num = power(i, p)
        if len(str(num)) == p:
            print(i, p, num)
            count += 1
print(count)
# power.py
def power(x, y):
    if y == 1:
        return x
    tot = 1
    for i in range(y):
        tot *= x
    return tot


if __name__ == '__main__':
    for x in range(1, 5):
        for y in range(1, 5):
            print('power({0}, {1}) = {2}'.format(x, y, power(x, y)))

转载于:https://www.cnblogs.com/noonjuan/p/11060470.html

猜你喜欢

转载自blog.csdn.net/weixin_30338497/article/details/94845223
63
今日推荐