Sum of all perfect numbers present in an array | Function to return the sum of all the proper factors of n ; f is the factor of n ; Function to return the required sum ; To store the sum ; If current element is non - zero and equal to the sum of proper factors of itself ; Driver code
function sumOfFactors ( n ) { let sum = 0 ; for ( let f = 1 ; f <= n / 2 ; f ++ ) { if ( n % f == 0 ) { sum += f ; } } return sum ; } function getSum ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 && arr [ i ] == sumOfFactors ( arr [ i ] ) ) { sum += arr [ i ] ; } } return sum ; } let arr = [ 17 , 6 , 10 , 6 , 4 ] ; let n = arr . length ; document . write ( getSum ( arr , n ) ) ;