Countdown: Difference between revisions

J draft
(J draft)
Line 30:
The brute force algorithm is quite obvious. What is more interesting is to find some optimisation heuristics to reduce the number of calculations. For example, a rather interesting computational challenge is to calculate, as fast as possible, all existing solutions (that means 2'764'800 operations) for all possible games (with all the 13'243 combinations of six numbers out of twenty-four for all 898 possible targets between 101 and 999).
 
 
=={{header|J}}==
Brute force implementation:
<syntaxhighlight lang=J>deck=: (25*1+i.4),2#1+i.10
deal=: 6&((?#){])@deck
targ=: 101+?@899
Pi=: ,~((#:I.@,)</~)i.
Va=: {{
ok=. I#~(= 1>.>.)u&".&>/y{~|:I=. Pi N=.#y
(N-1){."1 (y{~ok-."1~i.N),.<@(u expr)"1 ok{y
}}
Pa=: {{ if. 1<#;:y do. '(',y,')' else. y end. }}
expr=: {{ (Pa A),(;u`''),Pa B['A B'=.y }}
arith=: [:; <@(+Va, -Va, *Va, %Va,(-Va, %Va)@|.)"1
all=: {{ A#~x=".@>A=.,arith^:5 ":each y}}
 
task=: {{
echo 'terms: ',":c=. /:~ deal ''
echo 'target: ',":t=. targ ''
echo '#solutions: ',":#a=. t all c
echo 'for example: ',;{.a
}}</syntaxhighlight>
 
Examples:
<syntaxhighlight lang=J> task''
terms: 1 1 5 10 10 75
target: 736
#solutions: 3644
for example: (10*(75-1))-(10-(1+5))
task''
terms: 1 2 4 4 7 10
target: 567
#solutions: 37
for example: 7*((2+(10*(4+4)))-1)</syntaxhighlight>
 
=={{header|Phix}}==
6,962

edits