Sum of all the prime divisors of a number | Set 2 | Python3 program for the above approach ; Function to find sum of prime divisors of the given number N ; Add the number 2 if it divides N ; Traverse the loop from [ 3 , sqrt ( N ) ] ; If i divides N , add i and divide N ; This condition is to handle the case when N is a prime number greater than 2 ; Driver code ; Given input ; Function call
import math NEW_LINE def SumOfPrimeDivisors ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE if n % 2 == 0 : NEW_LINE INDENT sum += 2 NEW_LINE DEDENT while n % 2 == 0 : NEW_LINE INDENT n //= 2 NEW_LINE DEDENT k = int ( math . sqrt ( n ) ) NEW_LINE for i in range ( 3 , k + 1 , 2 ) : NEW_LINE INDENT if n % i == 0 : NEW_LINE INDENT sum += i NEW_LINE DEDENT while n % i == 0 : NEW_LINE INDENT n //= i NEW_LINE DEDENT DEDENT if n > 2 : NEW_LINE INDENT sum += n NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 10 NEW_LINE print ( SumOfPrimeDivisors ( n ) ) NEW_LINE DEDENT