Sum of all the prime divisors of a number | Set 2 | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int SumOfPrimeDivisors ( int n ) { int sum = 0 ; if ( n % 2 == 0 ) { sum = sum + 2 ; } while ( n % 2 == 0 ) { n = n / 2 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { sum = sum + i ; } while ( n % i == 0 ) { n = n / i ; } } if ( n > 2 ) { sum = sum + n ; } return sum ; } int main ( ) { int n = 10 ; cout << SumOfPrimeDivisors ( n ) ; return 0 ; }