求一个数的所有因子的和,不包括被除的那个特定的数。
例如,如果我们想要找到数字6的除数,它们应该是1,2,3。数字6应该包括在内,因为它是一个除数,但对于这种情况,我们不考虑它。
注意:-数字3和4不包括在内,因为当你将数字6除以数字3时,会有一个余数。
除数是一个除以另一个没有余数的数。
def sum_divisors(n):
sum = 0
# Return the sum of all divisors of n, not including n
return sum
print(sum_divisors(0)) # 0
print(sum_divisors(3)) # Should sum of 1 # 1
print(sum_divisors(36)) # Should sum of 1+2+3+4+6+9+12+18 # 55
print(sum_divisors(102)) # Should be sum of 2+3+6+17+34+51 # 114发布于 2020-05-09 04:04:24
我想我明白了:
def sum_divisors(n):
return sum([i for i in range(1, n)
if n % i == 0])
print(sum_divisors(0))
print(sum_divisors(3)) # Should sum of 1
print(sum_divisors(36)) # Should sum of 1+2+3+4+6+9+12+18
print(sum_divisors(102)) # Should be sum of 2+3+6+17+34+51输出
0
1
55
114https://stackoverflow.com/questions/61686862
复制相似问题