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

时间:2018-10-01 14:07:14

标签: python

在我的程序中,应该询问用户一个数字,然后显示所有可能的除数,但是最后,它必须显示所有除数的总和。我在最后一部分上遇到了麻烦,希望能有所帮助。

我的代码:

debug.SetMaxStack(5000000000) // bytes
model.ExcelCreator()

例如,如果用户输入20:

    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")

5 个答案:

答案 0 :(得分:4)

只需对代码进行简单的修改即可解决问题。喜欢:

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

您还可以使用list comprehension使程序更短,更智能,例如:

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))

答案 1 :(得分:3)

您只需要一个变量来存储和。我使用了s。除print(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

答案 2 :(得分:1)

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

如果您考虑9,

我们不需要考虑到9才能得到所有因素。如果我们有X作为除数,我们只需要考虑到3,则提示/ 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

答案 3 :(得分:1)

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

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...

答案 4 :(得分:1)

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

print("The divisors of the integer you entered are: ")
total= 0
for i in range(1, prompt+1):
    if(prompt%i==0):
        print(i)
        total+= i

print("The sum of the divisors is:{}".format(total))

输出:

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