我如何在Python中打印出完美的数字及其旁边显示的除数?
#I have the formula in which the code produces all perfect numbers between 1 and 10,000 and I want the divisors printed out alongside the perfect numbers
n = 1
while n <= 10001:
sum = 0
divisor = 1
while divisor < n:
if not n % divisor:
sum += divisor
divisor = divisor + 1
if sum == n:
print(n, "is a perfect number")
n = n + 1
#for example, I want the output to look like:
6 - 真因数:1, 2, 3
28 - 真因数:1, 2, 3, 4, 5, 6, 7
#I have the formula in which the code produces all perfect numbers between 1 and 10,000 and I want the divisors printed out alongside the perfect numbers
n = 1
while n <= 10001:
sum = 0
divisor = 1
while divisor < n:
if not n % divisor:
sum += divisor
divisor = divisor + 1
if sum == n:
print(n, "is a perfect number")
n = n + 1
#for example, I want the output to look like:
6 - proper divisors: 1, 2, 3
28 - proper divisors: 1, 2, 3, 4, 5, 6, 7
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要跟踪总和,跟踪除数并在最后检查总和:
输出:
强制列表理解解决方案:
Don't track the sum, track the divisors and check the sum at the end:
Output:
Obligatory list comprehension solution: