编写程序计算正整数输入 n 的所有约数之和。使用 Python

Write a program to compute the sum of all divisors of positive integer input n. Using Python

编写程序计算正整数输入 n 的所有约数之和。 输入示例:正整数 10 output:1+2+5+10=18

def factor(n): if n==0: return[0] if n<=3: return[1] x=n list=[1] i=2 while i<=x: if x%i==0: if i !=list[-1]: list.append(i) x=x//i i=2 continue i+=1

试试这个

n=int(input("Enter an integer:"))
print("The sum of all divisors of given positive integer input is:")
sum = 0
for i in range(1,n+1):
    if(n%i==0):
        sum = sum + i
print(sum)

Enter an integer:36
The sum of all divisors of given positive integer input is:                                                             
91                                                                                                     

希望对您有所帮助。