I would like to know, how I can add up all possible divisors of a number in python without the given number itself.
For example, all possible divisors of the number 36 are 1, 2, 3, 4, 6, 9, 12 and 18. When you add them up, you should get a total of 55. I would like to know how to get these divisors with a while loop.
I have tried the following code:
def sum_divisors(n): sum = 0 divisor = 2 while n % divisor == 0: if n == 0: return 0 print(n / divisor) divisor += 1 print(sum_divisors(36))
When I excecute this code I only get the number 18, 12, and 9. After these numbers I get "None". I think my problem is, when I execute this code the divisor will add 1 as long as the condition ist "True". But when the programm have to divide 36 / 5 the remainder is 1 and the code will no longer be excecuted because the condition is "False". So I tried to fix this problem with the following code:
def sum_divisors(n): sum = 0 divisor = 2 while n % divisor == 0: if n == 0: return 0 if n % divisor == 1: divisor += 2 print(n / divisor) divisor += 1 print(sum_divisors(36))
But this code din’t work either. Do you have some tips how I can fix this problem. I only want to have tips and not suggested solutions. I am aware that I still need to find a code on how to add everything up. But I will surely be able to solve this problem. 😉
Source: Python Questions