Find distinct integers for a triplet with given product | Function to find the required triplets ; To store the factors ; Find factors in Math . sqrt ( x ) time ; Choose a factor ; Choose another factor ; These conditions need to be met for a valid triplet ; Prlet the valid triplet ; Triplet found ; Triplet not found ; Driver code
function findTriplets ( x ) { let fact = [ ] ; let factors = new Set ( ) ; for ( let i = 2 ; i <= Math . sqrt ( x ) ; i ++ ) { if ( x % i == 0 ) { fact . push ( i ) ; if ( x / i != i ) fact . push ( x / i ) ; factors . add ( i ) ; factors . add ( x / i ) ; } } let found = false ; let k = fact . length ; for ( let i = 0 ; i < k ; i ++ ) { let a = fact [ i ] ; for ( let j = 0 ; j < k ; j ++ ) { let b = fact [ j ] ; if ( ( a != b ) && ( x % ( a * b ) == 0 ) && ( x / ( a * b ) != a ) && ( x / ( a * b ) != b ) && ( x / ( a * b ) != 1 ) ) { document . write ( a + " " + b + " " + ( x / ( a * b ) ) ) ; found = true ; break ; } } if ( found ) break ; } if ( ! found ) document . write ( " " ) ; } let x = 105 ; findTriplets ( x ) ;