Length of smallest sequence having sum X and product Y | Function for checking valid or not ; Function for checking boundary of binary search ; Function to calculate the minimum sequence size using binary search ; Initialize high and low ; Base case ; Print - 1 if a sequence cannot be generated ; Otherwise ; Iterate until difference between high and low exceeds 1 ; Calculate mid ; Reset values of high and low accordingly ; Print the answer ; Driver Code ; Function call
function temp ( n , x ) { return Math . pow ( ( x * 1.0 ) / n , n ) ; } function check ( n , y , x ) { var v = temp ( n , x ) ; return v >= y ; } function find ( x , y ) { var high = parseInt ( Math . floor ( x / Math . exp ( 1.0 ) ) ) ; var low = 1 ; if ( x == y ) document . write ( 1 + " " ) ; else if ( ! check ( high , y , x ) ) document . write ( - 1 + " " ) ; else { while ( high - low > 1 ) { var mid = ( high + low ) / 2 ; if ( check ( mid , y , x ) ) high = mid ; else low = mid ; } document . write ( high + " " ) ; } } var x = 9 , y = 8 ; find ( x , y ) ;