Reputation: 31
I have to somehow do a function in which when I execute it I will be able to add all the divisors of a number, in the way shown below.
This is getting me crazy, I have been in the same problem for about an hour.
def sum_divisors(n):
# Return the sum of all divisors of n, not including n
divisor = 1
while divisor < n:
if n%divisor==0:
return divisor
divisor = divisor + 1
else:
divisor = divisor + 1
print(sum_divisors(6)) # Should be 1+2+3=6
print(sum_divisors(12)) # Should be 1+2+3+4+6=16
Upvotes: 2
Views: 18661
Reputation: 1
here's my code(didn't clean it up but it works well) :
def sum_divisors(n):
sum = 0
divisors = 1
while divisors < n:
if n == 0:
return 0
else:
if n % divisors == 0:
sum += divisors
divisors += 1
else:
divisors += 1
continue
# 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
Upvotes: 0
Reputation: 1
def sum_divisors(n):
x = 1
a = 0
while n!=0 and x<n:
if n%x == 0:
a = a + x
x += 1
#Return the sum of all divisors of n, not including n
return a
print(sum_divisors(0)) #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
Upvotes: 0
Reputation: 340
def sum_divisors(n):
sum = 0
x = 1
while n != 0 and x < n :
if n % x == 0 :
sum += x
else:
sum += 0
x += 1
return sum
Upvotes: 0
Reputation: 9
def sum_divisors(n):
sum = 0
accum = 1
# Return the sum of all divisors of n, not including n
while n != 0 and accum < n:
if n % accum == 0:
sum += accum
accum += 1
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
Upvotes: 1
Reputation: 2247
You can use this simple while loop to print the sum of all the divisors of a number. you should use an accumulator to increment the temp.
def sum_divisors(n):
sum = 0
accum = 1
while n != 0 and accum < n:
if n % accum == 0:
sum += accum
accum += 1
return sum
print(sum_divisors(6)) # prints 6
print(sum_divisors(12)) # prints 16
Upvotes: 1
Reputation: 15
def sum_divisors(n):
total = [0]
divisors = 1
while divisors < n:
if n % divisors == 0:
result.append(divisors)
else:
pass
divisors += 1
return sum(total)
Upvotes: 1
Reputation: 21
There should be a if condition at line 5, so that the required result will be obtained for 0 also
if(num==0):
return result;
Upvotes: 0
Reputation: 66
def sum_divisors(n):
sum = 0
z = 1
while n > z:
if n % z == 0:
sum = sum + z
z = z + 1
else:
z = z + 1
# Return the sum of all divisors of n, not including n
return sum
Upvotes: 5
Reputation: 1775
A simple google search would lead you to answers with a lot of explanations: sum of divisors in python
In case you think about some efficiency:
we need to check divisors till sqrt
of a number
import math
def sum_divisors(num) :
# Final result of summation of divisors
result = 0
# find all divisors which divides 'num'
i = 2
while i<= (math.sqrt(num)) :
# if 'i' is divisor of 'num'
if (num % i == 0) :
# if both divisors are same then
# add it only once else add both
if (i == (num / i)) :
result = result + i;
else :
result = result + (i + num//i);
i = i + 1
# Add 1 to the result as 1 is also
# a divisor
return (result + 1);
print(sum_divisors(6))
print(sum_divisors(12))
Upvotes: 1
Reputation: 48297
this here is weird:
if n%divisor==0:
return divisor
divisor = divisor + 1 //<= this is actually dead code, since is after the return statement...
on the other hand:
this here:
divisor = divisor + 1
works more like a counter but you are missing the accumulator...
you should do something like
accum = 0
while divisor < n:
foo = n % divisor
if foo == 0:
accum = accum + divisor
Upvotes: 1
Reputation: 29
In your fonction, you return instantly after finding a divisor. That's why your fonction doesnt work Try to put each n%divisor == 0 in a list ans return it AT the end of the while.
Or try to print it directly.
Upvotes: 2