Python calculates the perfect number within 1000, and the output format is "* * = 1 + * + * +... + *".

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

pythoncalculatesperfectnumberoutput

If a natural number is equal to its factor ( Include 1, Not including itself ) The sum of the , It 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 “**=1+++…+*”, The focus is on how to output in format ?




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


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