Sum of Divisors
Accenture coding challenges question, verified with a worked answer. Free to practise - no sign-up.
Given an integer N, find the sum of all its divisors.
Input format
A single integer N.
Output format
A single integer representing the sum of all divisors of N.
Constraints
1 <= N <= 10^5
Sample tests
Sample 1
Input
6
Expected
12Sample 2
Input
10
Expected
18Sample 3
Input
15
Expected
24Show a reference solution
Reference solution
def sum_of_divisors(n):
if n <= 0: return 0
total = 0
for i in range(1, int(n**0.5) + 1):
if n % i == 0:
total += i
if i != n // i:
total += n // i
return total