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 Feb 5, 2020 at 7:44
2
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 Apr 27, 2020 at 6:48
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
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
some usersome user
1,6532 gold badges12 silver badges29 bronze badges
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 + 1works 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 + divisoranswered Feb 5, 2020 at 7:47
ΦXocę 웃 Пepeúpa ツΦXocę 웃 Пepeúpa ツ
46.3k17 gold badges67 silver badges93 bronze badges
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
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 16answered Aug 26, 2020 at 8:48
Jamith NImanthaJamith NImantha
1,6792 gold badges22 silver badges27 bronze badges
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
if(num==0): return result;There should be a if condition at line 5, so that the required result will be obtained for 0 also
answered May 20, 2020 at 13:58
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
waithirawaithira
1801 gold badge1 silver badge10 bronze badges
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,2818 silver badges15 bronze badges
answered Jan 21, 2021 at 14:17
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 Sep 8 at 16:37