Sum and product puzzle: Difference between revisions

No edit summary
Line 1,093:
=={{header|J}}==
'''Tacit Solution'''
<syntaxhighlight lang="j">o(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=. (X([ ~>: YS o ]) and ((1 < X) and (1 < Y) (and=. *.) (100X >:< SY)) ando (Y > X])
filter0=. candidates o (constraints # ])
 
(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
pmtod=. P o ; o (pd #~ 1 < P #/. ]) NB. products with more than one decomposition
allhavefilter1=. 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)
filter3=. candidates o ; o (sd #~ 1 = (#&>) o sd)
 
decompositions=. > o , o { o (;~) o i.
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(] ofilter0 decompositions) f.</syntaxhighlight>
 
Example use:
<syntaxhighlight lang="j"> solve 1001684
706440 candidates
50485 candidates
17485 candidates
1 candidates
X=4 Y=13 X+Y=17 X*Y=52
solve 100
2352 candidates
145 candidates
86 candidates
1 candidates
X=4 Y=13 X+Y=17 X*Y=52</syntaxhighlight>
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):
Line 1,139 ⟶ 1,151:
&{"1) </. ])))@:(([ 0 0&$@(1!:2&2)@:(' candidates' ,~ ":@:#))@:((((0&{"1 + 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
0 0&$@(1!:2&2)@:(' candidates' ,~ ":@:#))@:((([ >: (0&{"1 ~:+ 1&{"1)@:]) *. ((1 < 0&{"1) *. (1
< 10&{"1) *. (100 >: 0&{"1 +< 1&{"1) *. 10&{"1 >< 01&{"1)@:]) # ]))@:( >@:,@:{@:(;~)@:i.)</syntaxhighlight>
 
=={{header|Java}}==
23

edits