2

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
ΦXocę 웃 Пepeúpa ツ
48.4k17 gold badges77 silver badges102 bronze badges
asked Feb 5, 2020 at 7:44
2
  • 1
    Let me guess, the problem is this code is unreachable? (you should be the one to say it, not me). Commented Feb 5, 2020 at 7:46
  • I think it is a tradition you should also upvote the answer you accepted. Commented Feb 6, 2020 at 5:11

11 Answers 11

5
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
O. Aroesti
1,10512 silver badges32 bronze badges
answered Apr 27, 2020 at 6:48
Sign up to request clarification or add additional context in comments.

Comments

2

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.

answered Feb 5, 2020 at 7:50

Comments

1

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))
answered Feb 5, 2020 at 7:57

Comments

1

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
answered Feb 5, 2020 at 7:47

Comments

1

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)
answered Jun 16, 2020 at 23:35

1 Comment

While this might answer the question, you should edit your answer to include an explanation of how this code block answers the question. This helps provide context and makes your answer much more useful to future readers.
1

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
answered Aug 26, 2020 at 8:48

Comments

1
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
answered Sep 15, 2020 at 21:27

Comments

0

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;
answered May 20, 2020 at 13:58

Comments

0
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
answered Oct 18, 2020 at 11:18

Comments

0
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
couka
1,41110 silver badges16 bronze badges
answered Jan 21, 2021 at 14:17

Comments

0

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
Juancheeto
5865 silver badges18 bronze badges
answered Sep 8, 2022 at 16:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.