Maximize the product of four factors of a Number | Declare the ArrayList of factors for storing the ; function to find out the factors of a number ; Loop until the i reaches the sqrt ( n ) ; Check if i is a factor of n ; if both the factors are same we only push one factor ; factor1 is pushed ; factor2 is pushed ; Function to find the maximum product ; Initialize the product with - 1 ; Find the sum of factors and store it in s ; Compare whether it is equal to the n ; product of factors ; Check whether we have a better p now if yes update ; Driver Code ; initializes the List with the divisors of n ; prints out the maximised product .
let factors = [ ] ; function findFactors ( n ) { for ( let i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( ( n / i ) == i ) { factors . push ( i ) ; } else { factors . push ( n / i ) ; factors . push ( i ) ; } } } } function findProduct ( n ) { let product = - 1 ; let si = factors . length ; for ( let i = 0 ; i < si ; i ++ ) { for ( let j = 0 ; j < si ; j ++ ) { for ( let k = 0 ; k < si ; k ++ ) { for ( let l = 0 ; l < si ; l ++ ) { let s = factors [ i ] + factors [ j ] + factors [ k ] + factors [ l ] ; if ( s == n ) { let p = factors [ i ] * factors [ j ] * factors [ k ] * factors [ l ] ; if ( p > product ) { product = p ; } } } } } } return product ; } let n = 10 ; findFactors ( n ) ; document . write ( findProduct ( n ) ) ;