Sum and product puzzle: Difference between revisions

Content added Content deleted
No edit summary
Line 1,093: Line 1,093:
=={{header|J}}==
=={{header|J}}==
'''Tacit Solution'''
'''Tacit Solution'''
<syntaxhighlight lang="j">o=. @:
<syntaxhighlight lang="j">(X=. 0&{"1) (Y=. 1&{"1)
and=. *.

X=. 0&{"1
Y=. 1&{"1

candidates=. ([ echo o (' candidates' ,~ ": o #))

constraints=. (X ~: Y) and (1 < X) and (1 < Y) and (100 >: S) and (Y > X)


candidates=. ([ echo o (' candidates' ,~ ": (o=. @:) #))
constraints=. (([ >: S o ]) and ((1 < X) and (1 < Y) (and=. *.) (X < Y)) o ])
filter0=. candidates o (constraints # ])
filter0=. candidates o (constraints # ])


S=. X + Y
(S=. X + Y) (P=. X * Y)
(sd=. S </. ]) (pd=. P </. ]) NB. sum and product decompositions
P=. X * Y

sd=. S </. ] NB. sum decompositions
pd=. P </. ] NB. 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
allhave=. ('' -: -.)&>"0 _
filter1=. candidates o ((patesd ('' -: -.)&>"0 _ < o pmtod) ; o # sd)


filter1=. candidates o ((patesd allhave < o pmtod) ; o # sd)
filter2=. candidates o ; o (pd #~ 1 = (#&>) o pd)
filter2=. candidates o ; o (pd #~ 1 = (#&>) o pd)
filter3=. candidates o ; o (sd #~ 1 = (#&>) o sd)
filter3=. candidates o ; o (sd #~ 1 = (#&>) o sd)


decompositions=. > o , o { o (;~) o i.
decompositions=. > o , o { o (;~) o i.
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 o decompositions f.</syntaxhighlight>
solve=. show o filter3 o filter2 o filter1 o (] filter0 decompositions) f.</syntaxhighlight>


Example use:
Example use:
<syntaxhighlight lang="j"> solve 100
<syntaxhighlight lang="j"> solve 1684
706440 candidates
50485 candidates
17485 candidates
1 candidates
X=4 Y=13 X+Y=17 X*Y=52
solve 100
2352 candidates
2352 candidates
145 candidates
145 candidates
86 candidates
86 candidates
1 candidates
1 candidates
X=4 Y=13 X+Y=17 X*Y=52</syntaxhighlight>
X=4 Y=13 X+Y=17 X*Y=52
solve 1684
706440 candidates
50485 candidates
17485 candidates
1 candidates
X=4 Y=13 X+Y=17 X*Y=52
solve 1685
707281 candidates
51011 candidates
17567 candidates
2 candidates
X=4 4 Y=13 61 X+Y=17 65 X*Y=52 244</syntaxhighlight>


The code is tacit and fixed (in other words, it is point-free):
The code is tacit and fixed (in other words, it is point-free):
Line 1,139: Line 1,151:
&{"1) </. ])))@:(([ 0 0&$@(1!:2&2)@:(' candidates' ,~ ":@:#))@:((((0&{"1 + 1&{"1
&{"1) </. ])))@:(([ 0 0&$@(1!:2&2)@:(' candidates' ,~ ":@:#))@:((((0&{"1 + 1&{"1
) <@:(0&{"1 * 1&{"1)/. ]) ('' -: -.)&>"0 _ <@:((0&{"1 * 1&{"1)@:;@:(((0&{"1 * 1&
) <@:(0&{"1 * 1&{"1)/. ]) ('' -: -.)&>"0 _ <@:((0&{"1 * 1&{"1)@:;@:(((0&{"1 * 1&
{"1) </. ]) #~ 1 < (0&{"1 * 1&{"1) #/. ]))) ;@:# (0&{"1 + 1&{"1) </. ]))@:(([ 0
{"1) </. ]) #~ 1 < (0&{"1 * 1&{"1) #/. ]))) ;@:# (0&{"1 + 1&{"1) </. ]))@:(] ([
0&$@(1!:2&2)@:(' candidates' ,~ ":@:#))@:(((0&{"1 ~: 1&{"1) *. (1 < 0&{"1) *. (1
0 0&$@(1!:2&2)@:(' candidates' ,~ ":@:#))@:((([ >: (0&{"1 + 1&{"1)@:]) *. ((1 <
< 1&{"1) *. (100 >: 0&{"1 + 1&{"1) *. 1&{"1 > 0&{"1) # ]))@:(>@:,@:{@:(;~)@:i.)</syntaxhighlight>
0&{"1) *. (1 < 1&{"1) *. 0&{"1 < 1&{"1)@:]) # ]) >@:,@:{@:(;~)@:i.)</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==