Print all integers that are sum of powers of two given numbers | Function to print powerful integers ; Set is used to store distinct numbers in sorted order ; Store all the powers of y < bound in a vector to avoid calculating them again and again ; x ^ i ; If num is within limits insert it into the set ; Adding any number to it will be out of bounds ; Increment i ; Print the contents of the set ; Driver code ; Print powerful integers
function powerfulIntegers ( x , y , bound ) { var s = new Set ( ) ; var powersOfY = [ ] ; var i ; powersOfY . push ( 1 ) ; for ( i = y ; i < bound && y != 1 ; i = i * y ) powersOfY . push ( i ) ; i = 0 ; while ( true ) { var xPowI = Math . pow ( x , i ) ; powersOfY . forEach ( j => { var num = xPowI + j ; if ( num <= bound ) s . add ( num ) ; } ) ; if ( xPowI >= bound x == 1 ) break ; i ++ ; } [ ... s ] . sort ( ( a , b ) => a - b ) . forEach ( itr => { document . write ( itr + " " ) } ) ; } var x = 2 , y = 3 , bound = 10 ; powerfulIntegers ( x , y , bound ) ;