Here prime numbers are checked till the maximum number of bits possible because that the maximum bit sum possible is the number of bits itself .
primeArr = SieveOfEratosthenes ( ( int ) Math . ceil ( log2 ( y ) ) ) ; for ( int i = x ; i < y + 1 ; i ++ ) { int temp = setBits ( i ) ; if ( primeArr . contains ( temp ) ) count += 1 ; } System . out . println ( count ) ; } }