Sum of all the prime numbers with the maximum position of set bit â ‰¤ D | Function for Sieve of Eratosthenes ; Function to return the sum of the required prime numbers ; Maximum number of the required range ; Sieve of Eratosthenes ; To store the required sum ; If current element is prime ; Driver code
function sieve ( prime , n ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( var p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( var i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } } function sumPrime ( d ) { var maxVal = Math . pow ( 2 , d ) - 1 ; var prime = new Array ( maxVal + 1 ) ; prime . fill ( true ) ; sieve ( prime , maxVal ) ; var sum = 0 ; for ( var i = 2 ; i <= maxVal ; i ++ ) { if ( prime [ i ] ) { sum += i ; } } return sum ; } var d = 8 ; document . write ( sumPrime ( d ) ) ;