简体   繁体   中英

Issue with finding sum of the divisors of a number

def get_sum_of_divisors(number):
    div = 2
    total_sum = 1
    while div < number:
        if number % div == 0:
            total_sum = total_sum + 1
        div = div + 1

    return total_sum

def main():
    print("get_sum_of_divisors(24)", get_sum_of_divisors(24))
    
main()

I am trying to create a program which find the sum of the divisors of a number (excluding the divisor which is the number itself). I have checked the code multiple times but my code seems to return the value '7' for the divisors of 24, even though it should be 32.

Is there something wrong with the indentation? Does the div = div + 1 need to be somewhere else along the line?

Just Replace this line, total_sum = total_sum + 1 with this, total_sum = total_sum + div

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM