Problems with "while" looping

Question:

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
Asked By: Miguel Estrada

||

Answers:

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

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 By: ChokMania

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 By: some user
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
Answered By: Girly Corner

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 By: JasonJerry

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 By: Etijie Gideon

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 By: Jamith NImantha
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 By: samer eltahan
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 By: waithira
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
Answered By: Krishna Jadhav

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
Answered By: maca
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.