[Tutor] Question Regarding Quiz Material
Nick Yim
theoldny at gmail.com
Tue Jun 1 08:21:38 EDT 2021
Hello,
My question is regarding the script below.
The program executes fine and gives the desired result, so I guess my
question might have more to do with the math in this problem.
I am wondering why the numbers - n = 3, n = 36, n 102 - are not included in
the list of divisors?
As well, why is the number 1 included as a divisor of 3 and 36, but not as
a divisor of 102?
Please advise!
Best,
Nick
def sum_divisors(n):
x = 1
#trying all the numbers from 1, because you cannot divide by 0
sum = 0
#adding to 0
while n!=0 and x<n:
#conditions for n to have divisors (not be 0) and for x to be a potential
divisor
#error because the answer does not include the number itself
if n%x == 0:
sum += x
x +=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
More information about the Tutor
mailing list