'Python newb "Fill in the empty function so that it returns the sum of all the divisors of a number, without including it."
Good Day I'm a Python newb and I'm having a hard time figuring out how'd the output ended up 55? Also what is this sum in return sum? Are there any other methods also to make it less complicated?
def sum_divisors(n):
return sum([i for i in range(1, n) if n % i == 0])
print(sum_divisors(36))
output:
55
Solution 1:[1]
ok the output is 55 because ur summing up all divisors of 36
which are
1
2
3
4
6
9
12
18
let n = 36
ur iterating over the range and checking if the number when divided with n returns 0 as remainder
as for making it look simpler, look at this
def sum_divisors(n):
sum_list = []
for num in range(1, n):
if n%num == 0:
sum_list.append(num)
return sum(sum_list)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ryuga |
