A product array puzzle | Set 2 ( O ( 1 ) Space ) | epsilon value to maintain precision ; to hold sum of all values ; output product for each index anti log to find original product value ; Driver code
var EPS = 1e-9 ; function productPuzzle ( a , n ) { var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) sum += Math . log10 ( a [ i ] ) ; for ( var i = 0 ; i < n ; i ++ ) document . write ( parseInt ( ( EPS + Math . pow ( 10.00 , sum - Math . log10 ( a [ i ] ) ) ) ) + " " ) ; } var a = [ 10 , 3 , 5 , 6 , 2 ] ; var n = a . length ; document . write ( " " ) ; productPuzzle ( a , n ) ;