Sum and product puzzle: Difference between revisions

no edit summary
(→‎{{header|Raku}}: Reverting previous change - the built-in `sum ` should probably not actually work here, even though it does at least in v2022.12.)
No edit summary
Line 1,090:
{{Out}}
<pre>[(4,13)]</pre>
 
=={{header|J}}==
'''Tacit Solution'''
<syntaxhighlight lang="j">o=. @:
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)
 
filter0=. candidates o (constraints # ])
 
S=. X + Y
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
allhave=. ('' -: -.)&>"0 _
 
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 o decompositions f.</syntaxhighlight>
 
Example use:
<syntaxhighlight lang="j"> solve 100
2352 candidates
145 candidates
86 candidates
1 candidates
X=4 Y=13 X+Y=17 X*Y=52</syntaxhighlight>
 
The code is tacit and fixed (in other words, it is point-free):
<syntaxhighlight lang="j"> _80 [\ (5!:5)<'solve'
('X=' , ":@:(0&{"1) , ' Y=' , ":@:(1&{"1) , ' X+Y=' , ":@:(0&{"1 + 1&{"1) , ' X*
Y=' , ":@:(0&{"1 * 1&{"1))@:(([ 0 0&$@(1!:2&2)@:(' candidates' ,~ ":@:#))@:;@:((
(0&{"1 + 1&{"1) </. ]) #~ 1 = #&>@:((0&{"1 + 1&{"1) </. ])))@:(([ 0 0&$@(1!:2&2)
@:(' candidates' ,~ ":@:#))@:;@:(((0&{"1 * 1&{"1) </. ]) #~ 1 = #&>@:((0&{"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&
{"1) </. ]) #~ 1 < (0&{"1 * 1&{"1) #/. ]))) ;@:# (0&{"1 + 1&{"1) </. ]))@:(([ 0
0&$@(1!:2&2)@:(' candidates' ,~ ":@:#))@:(((0&{"1 ~: 1&{"1) *. (1 < 0&{"1) *. (1
< 1&{"1) *. (100 >: 0&{"1 + 1&{"1) *. 1&{"1 > 0&{"1) # ]))@:(>@:,@:{@:(;~)@:i.)</syntaxhighlight>
 
=={{header|Java}}==
23

edits