Sum of all the prime divisors of a number | Function to check if the number is prime or not . ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; function to find sum of prime divisors of N ; return type of sqrt function if float ; both factors are same ; both factors are not same ( i and n / i ) ; Driver code
function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( let i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } function SumOfPrimeDivisors ( n ) { let sum = 0 ; let root_n = parseInt ( Math . sqrt ( n ) , 10 ) ; for ( let i = 1 ; i <= root_n ; i ++ ) { if ( n % i == 0 ) { if ( i == parseInt ( n / i , 10 ) && isPrime ( i ) ) { sum += i ; } else { if ( isPrime ( i ) ) { sum += i ; } if ( isPrime ( parseInt ( n / i , 10 ) ) ) { sum += ( parseInt ( n / i , 10 ) ) ; } } } } return sum ; } let n = 60 ; document . write ( " " + SumOfPrimeDivisors ( n ) + " " ) ;