A product array puzzle | Set 2 ( O ( 1 ) Space ) | Python program for product array puzzle with O ( n ) time and O ( 1 ) space . ; epsilon value to maintain precision ; to hold sum of all values ; output product for each index antilog to find original product value ; Driver code
import math NEW_LINE EPS = 1e-9 NEW_LINE def productPuzzle ( a , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += math . log10 ( a [ i ] ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print int ( ( EPS + pow ( 10.00 , sum - math . log10 ( a [ i ] ) ) ) ) , NEW_LINE DEDENT return NEW_LINE DEDENT a = [ 10 , 3 , 5 , 6 , 2 ] NEW_LINE n = len ( a ) NEW_LINE print " The ▁ product ▁ array ▁ is : ▁ " NEW_LINE productPuzzle ( a , n ) NEW_LINE