Sum of all mersenne numbers present in an array | Function that returns true if n is a Mersenne number ; Function to return the sum of all the Mersenne numbers from the given array ; To store the required sum ; If current element is a Mersenne number ; Driver code
def isMersenne ( n ) : NEW_LINE INDENT while ( n != 0 ) : NEW_LINE INDENT r = n % 2 ; NEW_LINE if ( r == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT n //= 2 ; NEW_LINE DEDENT return True ; NEW_LINE DEDENT def sumOfMersenne ( arr , n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > 0 and isMersenne ( arr [ i ] ) ) : NEW_LINE INDENT sum += arr [ i ] ; NEW_LINE DEDENT DEDENT return sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 17 , 6 , 7 , 63 , 3 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( sumOfMersenne ( arr , n ) ) ; NEW_LINE DEDENT