1~5位自幂数解法
本帖最后由 xiaohu小虎 于 2020-5-19 13:20 编辑一位自幂数:
def selfpower1(): #这一例代码没有神马用
results = []
for i in range(0, 10):
if i**1 == i:
results.append(i)
print(results)
两位自幂数:
def selfpower2(): #这段莫有结果
results = []
for i in range(10, 100):
if ((i//10)**2) + ((i%10)**2) == i:
results.append(i)
print(results)
三位自幂数:
def selfpower3():
results = []
for i in range(100, 1000):
a = i // 100
b = i // 10 % 10
c = i % 10
if (a**3) + (b**3) + (c**3) == i:
results.append(i)
print(results)
四位自幂数:
def selfpower5():
results = []
for i in range(1000, 10000):
a = i // 1000
b = i // 100 % 10
c = i // 10 % 100
d = i % 10
if (a**4) + (b**4) + (c**4) + (d**4) == i:
results.append(i)
print(results)
五位自幂数:
def selfpower5():
results = []
for i in range(10000, 100000):
a = i // 10000
b = i // 1000 % 10
c = i // 100 % 10
d = i // 10 %10
e = i % 10
if (a**5) + (b**5) + (c**5) + (d**5) + (e**5) == i:
results.append(i)
print(results) 本帖最后由 xiaohu小虎 于 2020-5-19 16:50 编辑
sorry,这些我要改一下:
四位自幂数:
def selfpower4():
results = []
for i in range(1000, 10000):
a = i // 1000
b = i // 100 % 10
c = i // 10 % 10
d = i % 10
if (a**4) + (b**4) + (c**4) + (d**4) == i:
results.append(i)
print(results)
五位自幂数:
def selfpower5():
results = []
for i in range(10000, 100000):
a = i // 10000
b = i // 1000 % 10
c = i // 100 % 10
d = i // 10 %10
e = i % 10
if (a**5) + (b**5) + (c**5) + (d**5) + (e**5) == i:
results.append(i)
print(results)
{:10_245:}{:10_245:}{:10_250:}{:10_250:}{:10_257:} 五位的那个有缩进错误 result=[]
for i in range(100000):
i=str(i)
length=len(i)
sum1=0
for j in i:
sum1+=int(j)**length
if sum1==int(i):
result.append(i)
print(result)
用这个多少位都行!!! 徐小龙 发表于 2020-5-19 14:34
result=[]
for i in range(100000):
i=str(i)
重复计算大师。 4 位有错 永恒的蓝色梦想 发表于 2020-5-19 14:45
4 位有错
啊
徐小龙 发表于 2020-5-21 10:28
啊
???
我说楼主
页:
[1]