Python 如何在我的程序中得到除数之和?

Python 如何在我的程序中得到除数之和?,python,Python,在我的程序中,它应该要求用户输入一个数字,然后显示所有可能的除数,但最后,它必须显示所有除数的总和。最后一部分我遇到了问题,希望能得到一些帮助 我的代码: prompt = int(input("Enter an interger: ")) print("The divisors of the integer you entered are: ") for i in range(1, prompt+1): if(prompt%i==0): print(i)

在我的程序中,它应该要求用户输入一个数字,然后显示所有可能的除数,但最后,它必须显示所有除数的总和。最后一部分我遇到了问题,希望能得到一些帮助

我的代码:

    prompt = int(input("Enter an interger: "))

print("The divisors of the integer you entered are: ")
for i in range(1, prompt+1):
    if(prompt%i==0):
        print(i)
        print(f")
用户输入20时的示例:

The divisors of the integer you entered are:

1

2

4

5

10

20

The sum of the divisors is: 42

您只需要一个变量来存储总和。我使用了
s
。Rest除了打印(f)之外,所有代码都非常好。它未使用,并且由于不完整的
而产生语法错误。此外,
f
未定义

prompt = int(input("Enter an interger: "))
s=0
print("The divisors of the integer you entered are: ")

for i in range(1, prompt+1):
    if(prompt%i==0):
        s += i
        print(i)
print ("The sum of the divisors is: %d" %s)
输出

Enter an interger: 20
The divisors of the integer you entered are: 
1
2
4
5
10
20
The sum of the divisors is: 42

只要对代码进行简单的修改就可以做到这一点。比如:

prompt = int(input("Enter an interger: "))

print("The divisors of the integer you entered are: ")
divisor_sum = 0 #updated line
for i in range(1, prompt+1):
    if(prompt%i==0):
        print(i)
        divisor_sum+=i #calculate sum of all divisors

print("The sum of divisors " + str(divisor_sum)) #print the sum of divisors
您还可以使用
列表理解
使程序更短、更智能,例如:

prompt = int(input("Enter an interger: "))

divisors = [i for i in range(1,prompt+1) if prompt%i==0]
divisor_sum = sum(divisors)

print("The divisors of the integer you entered are: ")
for i in divisors:
    print(i)

print("The sum of divisors " + str(divisor_sum))

这个问题让我想起了我在Euler项目中解决的一个问题,我是在O(sqrt(n))复杂度下解决的

如果你考虑9,

我们直到9才需要考虑所有的因素。我们只需要考虑到3,如果我们有X作为除数,那么提示符/X也是一个除数。使用此属性可以使算法更高效

import time
from math import sqrt
prompt = int(input("Enter an interger: "))
start =time.time()
print("The divisors of the integer you entered are: ")

sumofdivisors=0 
for divisor in range(1, int(sqrt(prompt))+1):
    if(prompt%divisor==0):
        sumofdivisors+=divisor
        sumofdivisors+=(prompt/divisor)
        if (divisor==prompt/divisor):
            sumofdivisors-=divisor
            print(divisor)
        else:
            print(divisor)
            print(prompt/divisor)
print("sum is",sumofdivisors)
end=time.time()
print("time taken is",end-start)
输出

Enter an interger: 8
The divisors of the integer you entered are: 
1
8.0
2
4.0
sum is 15.0
time took =  0.0002665519714355469

另一种方法是将有效除数列表存储在某种容器中。在这种情况下,适当的“容器”是一个列表。 这样做的好处是可以存储除数以供以后使用

prompt = int(input("Enter an interger: "))
divisors = []
print("The divisors of the integer you entered are: ")

for i in range(1, prompt+1):
    if(prompt%i==0):
        print(i)
        divisors.append(i)

print("The sum of divisors " + sum(divisors)) #print the sum of divisors
# max(divisors)
# min(divisors)
# etc...
输出:

Enter an interger: 20
The divisors of the integer you entered are: 
1
2
4
5
10
20
The sum of the divisors is:42

代码有语法错误。尝试简单地复制并粘贴它(正确的格式)到问题中。哎呀,很抱歉,伙计们试图在打印中添加一个f字符串,但没有完成。但我得到了答案。谢谢这里还讨论了这一主题:
Enter an interger: 20
The divisors of the integer you entered are: 
1
2
4
5
10
20
The sum of the divisors is:42