Sum and product puzzle: Difference between revisions
→{{header|J}}
No edit summary |
|||
Line 1,093:
=={{header|J}}==
'''Tacit Solution'''
<syntaxhighlight lang="j">
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=. @:) #))
filter0=. candidates o (constraints # ])
(S=. X + Y) (P=. X * Y)
(sd=. S </. ]) (pd=. P </. ]) NB. sum and product 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
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
solve=. show o filter3 o filter2 o filter1 o
Example use:
<syntaxhighlight lang="j"> solve
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
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&$@(1!:2&2)@:(' candidates' ,~ ":@:#))@:((([ >: (0&{"1
=={{header|Java}}==
|