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
def sumOfFactors ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for f in range ( 1 , n // 2 + 1 ) : NEW_LINE INDENT if ( n % f == 0 ) : NEW_LINE INDENT sum += f NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT def getSum ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > 0 and arr [ i ] == sumOfFactors ( arr [ i ] ) ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT arr = [ 17 , 6 , 10 , 6 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( getSum ( arr , n ) ) NEW_LINE