Sum and product puzzle: Difference between revisions

Content added Content deleted
Line 1,093: Line 1,093:
=={{header|J}}==
=={{header|J}}==
'''Tacit Solution'''
'''Tacit Solution'''
<syntaxhighlight lang="j">(X=. 0&{"1) (Y=. 1&{"1)
<syntaxhighlight lang="j">(S=. X + Y) (P=. X * Y) (X=. 0&{"1) (Y=. 1&{"1)
(S=. X + Y) (P=. X * Y)
(sd=. S </. ]) (pd=. P </. ]) NB. sum and product decompositions


candidates=. ([ echo o (' candidates' ,~ ": (o=. @:) #))
candidates=. ([ echo o (' candidates' ,~ ": (o=. @:) #))
Line 1,100: Line 1,100:
filter0=. candidates o (constraints # ])
filter0=. candidates o (constraints # ])


(sd=. S </. ]) (pd=. P </. ]) NB. sum and product decompositions
patesd=. S (< o P)/. ] NB. products associated to each sum decomposition

patesd=. S (< o P)/. ] NB. products associated to each sum decomposition
pmtod=. P o ; o (pd #~ 1 < P #/. ]) NB. products with more than one decomposition
pmtod=. P o ; o (pd #~ 1 < P #/. ]) NB. products with more than one decomposition
filter1=. candidates o ((patesd ('' -: -.)&>"0 _ < o pmtod) ; o # sd)
filter1=. candidates o ((patesd ('' -: -.)&>"0 _ < o pmtod) ; o # sd)
Line 1,112: Line 1,110:
show=. 'X=' , ": o X ,' Y=' , ": o Y , ' X+Y=' , ": o (X+Y) , ' X*Y=' , ": o (X*Y)
show=. 'X=' , ": o X ,' Y=' , ": o Y , ' X+Y=' , ": o (X+Y) , ' X*Y=' , ": o (X*Y)


solve=. show :: (''"_) o filter3 o filter2 o filter1 o (] filter0 decompositions) f.</syntaxhighlight>
solve=. show :: (''"_) o filter3 o filter2 o filter1 o (] filter0 decompositions) f.
</syntaxhighlight>


Example use:
Example use: