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
function isMersenne ( n ) { while ( n != 0 ) { let r = n % 2 ; if ( r == 0 ) return false ; n = Math . floor ( n / 2 ) ; } return true ; } function sumOfMersenne ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 && isMersenne ( arr [ i ] ) ) { sum += arr [ i ] ; } } return sum ; } let arr = [ 17 , 6 , 7 , 63 , 3 ] ; let n = arr . length ; document . write ( sumOfMersenne ( arr , n ) ) ;