24 game/Solve: Difference between revisions

m (→‎{{header|Picat}}: Fix comment: Perl 6 --> Raku)
Line 3,562:
 
The answer will be either a suitable J sentence or blank if none can be found. "J sentence" means that, for example, the sentence <code>8*7-4*1</code> is equivalent to the sentence <code>8*(7-(4*1))</code>. [Many infix languages use operator precedence to make polynomials easier to express without parenthesis, but J has other mechanisms for expressing polynomials and minimal operator precedence makes the language more regular.]
 
Here is an alternative version that supports multi-digit numbers. It prefers expressions without parens, but searches for ones with if needed.
 
<lang J>ops=: > , { 3#<'+-*%'
perms=: [: ":"0 [: ~. i.@!@# A. ]
build=: 1 : '(#~ 24 = ".) @: u'
 
combp=: dyad define
'a b c d'=. y['f g h'=. x
('(',a,f,b,g,c,')',h,d),('(',a,f,b,')',g,c,h,d),(a,f,'(',b,g,c,')',h,d),:('((',a,f,b,')',g,c,')',h,d)
)
 
math24=: monad define
assert. 4 = # y NB. prefer expressions without parens & fallback if needed
es=. ([: ,/ ops ([: , (' ',[) ,. ])"1 2/ perms) build y
if. 0 = #es do. es =. ([: ,/ [: ,/ ops combp"1 2/ perms) build y end.
es -."1 ' '
)</lang>
 
{{out}}
<pre> math24 2 3 5 12
12%3-5%2
math24 2 3 8 9
8*9-2*3
8*9-3*2
8%2%9-3
math24 3 6 6 11
(6+6*11)%3
(6+11*6)%3
((6*11)+6)%3
((11*6)+6)%3
</pre>
 
=={{header|Java}}==
Anonymous user