python 求3到8位数的水仙花数Pycharm实现

时间:2023-03-09 20:20:28
python 求3到8位数的水仙花数Pycharm实现
#-*- coding: utf-8-*-
import time
import math
#获取3位数的水仙花数
start1 = time.time()
start = time.time() numbers = []
for i in range(100,1000):
a = i % 10
b = i // 10 % 10
c = i // 100
if((a ** 3) + (b ** 3) + (c ** 3)) == i:
numbers.append(i) for j in numbers:
print("3位数中水仙花数有 %d" % (j)) end = time.time()
take = end - start
print("计算3位数中的水仙花数花费时间 %d 秒" % (take)) #四位数中的四爷玫瑰数
numbers = [] for i in range(1000,10000):
a = i % 10
b = i % 100 // 10
c = i % 1000 // 100
d = i // 1000 if((a ** 4) + (b ** 4) + (c ** 4) + (d ** 4)) == i:
numbers.append(i) for j in numbers:
print("4位数中水玫瑰数有 %d" % (j)) #五位数中的五角星数
numbers = [] for i in range(10000,100000):
a = i % 10
b = i // 10 % 10
c = i // 100 % 10
d = i // 1000 % 10
e = i // 10000 if(math.pow(a,5) + (b ** 5) + (c ** 5) +
(d ** 5) + (e ** 5)) == i:
numbers.append(i) for j in numbers:
print("5位数中的五角星数有 %d" % (j)) #六位数中的六合数
numbers = [] for i in range(100000,1000000):
a = i % 10
b = i // 10 % 10
c = i // 100 % 10
d = i // 1000 % 10
e = i // 10000 % 10
f = i // 100000 if(math.pow(a,6) + math.pow(b,6) + math.pow(c,6)
+ math.pow(d,6) + math.pow(e,6) + math.pow(f,6)) == i:
numbers.append(i) for j in numbers:
print("6位数中六合数有 %d " % (j)) #七位数中的北斗七星数
numbers = [] for i in range(1000000,10000000):
a = i % 10
b = i // 10 % 10
c = i // 100 % 10
d = i // 1000 % 10
e = i // 10000 % 10
f = i // 100000 % 10
g = i // 1000000 if(math.pow(a,7) + math.pow(b,7) + math.pow(c,7)
+ math.pow(d,7) + math.pow(e,7) + math.pow(f,7) + math.pow(g,7)) == i:
numbers.append(i) for j in numbers:
print("7位数中北斗七星数有 %d " % (j)) end = time.time()
take = end - start
print("7位数中北斗七星数花费时间 % d 秒" % (take)) #八位数中的八仙花数
numbers = [] for i in range(10000000,100000000):
a = i % 10
b = i // 10 % 10
c = i // 100 % 10
d = i // 1000 % 10
e = i // 10000 % 10
f = i // 100000 % 10
g = i // 1000000 % 10
h = i // 10000000 if(math.pow(a,8) + math.pow(b,8) + math.pow(c,8)
+ math.pow(d,8) + math.pow(e,8) + math.pow(f,8)
+ math.pow(g,8) + math.pow(h,8)) == i:
numbers.append(i) for j in numbers:
print("8位数中八仙花数有 %d " % (j)) end = time.time()
take = end - start
print("8位数中八仙花数花费时间 % d 秒" % (take))