Exercise 762
Write a Python program as a function which takes an integer n as a parameter and which returns the sum of all divisors of n
Solution
def sumDivisors(n):
# Initializing the sum of all divisors of n
sumDiv = 0
for i in range(1 , n + 1):
if n%i == 0:
sumDiv = sumDiv + i
return sumDiv
# Testing algorithm for n = 6 sumdivisors(6) = 1 +2 + 3 + 6 = 12
print('The sum of all divisors of 6 is : ' , sumDivisors(6))
# The output is : The sum of all divisors of 6 is : 12
Younes Derfoufi
my-courses.net
my-courses.net