Python outputs perfections within 1000 in the format of "* = * + * +... + *"

CSDN Q & A 2022-05-14 13:58:11 阅读数:745

pythonoutputsperfectionsformat

If a natural number is equal to its factor ( Include 1, Not including itself ) The sum of the , Then this number is called “ Complete ”. Such as 6=1+2+3, be 6 The number is perfect. . Programming , Output 1000 Within the end of , The output format is “*=++…+*”. Now I would like to ask you , How to output the format that meets the requirements of the topic ?




Take the answer :
for i in range(1, 1000): sum = 0 l = [] for j in range(1, i): if i%j == 0: sum += j; l.append(j) if sum == i: print("{left}={right}".format(left=i, right="+".join([str(x) for x in l])))

img


for i in range(1, 1000): sum = 0 l = [] for j in range(1, i): if i%j == 0: sum += j; l.append(j) if sum == i: print(i,str(l).replace('[', '= ').replace(']', '').replace(',', ' +'))

img


版权声明:本文为[CSDN Q & A]所创,转载请带上原文链接,感谢。 https://pythonmana.com/2022/134/202205072241177089.html