Find sum of odd factors of a number | Formula based Python3 program to find sum of all divisors of n . ; Returns sum of all factors of n . ; Traversing through all prime factors . ; ignore even factors by of 2 ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number . ; Driver code
import math NEW_LINE def sumofoddFactors ( n ) : NEW_LINE INDENT res = 1 NEW_LINE while n % 2 == 0 : NEW_LINE INDENT n = n // 2 NEW_LINE DEDENT for i in range ( 3 , int ( math . sqrt ( n ) + 1 ) ) : NEW_LINE INDENT count = 0 NEW_LINE curr_sum = 1 NEW_LINE curr_term = 1 NEW_LINE while n % i == 0 : NEW_LINE INDENT count += 1 NEW_LINE n = n // i NEW_LINE curr_term *= i NEW_LINE curr_sum += curr_term NEW_LINE DEDENT res *= curr_sum NEW_LINE DEDENT if n >= 2 : NEW_LINE INDENT res *= ( 1 + n ) NEW_LINE DEDENT return res NEW_LINE DEDENT n = 30 NEW_LINE print ( sumofoddFactors ( n ) ) NEW_LINE