-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24game.wl
40 lines (25 loc) · 1 KB
/
24game.wl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
(* ::Package:: *)
(* 不考虑去重 *)
ClearAll[calc24];
calc24[L : {n1_, n2_, n3_, n4_}] :=
Cases[
Tuples[{Tuples[{Plus, Subtract, Times, Divide}, 3], Permutations[L]}] /.
{{o1_, o2_, o3_}, {a_, b_, c_, d_}} ->
HoldForm /@ {
a~o1~b~o2~c~o3~d,
(a~o1~b)~o2~(c~o3~d),
(a~o1~(b~o2~c))~o3~d,
a~o1~(b~o2~(c~o3~d)),
a~o1~((b~o2~c)~o3~d)
},
e_ /; ReleaseHold@e == 24, {2}
] // Map[Map[InputForm]] // Quiet;
calc24[{1, 5, 7, 9}]
(* {(1 - 7)*(5 - 9),(5 - 9)*(1 - 7),(7 - 1)*(9 - 5),(9 - 5)*(7 - 1)} *)
(* 考虑去重 *)
ClearAll[calc24, gps];
gps = Groupings[Permutations[{a, b, c, d}], {Plus -> {2, Orderless}, Subtract -> 2, Times -> {2, Orderless}, Divide -> 2}, HoldForm] // DeleteDuplicatesBy[Factor@*ReleaseHold];
calc24[L : {n1_, n2_, n3_, n4_}] :=
gps /. Thread[{a, b, c, d} -> L] // Pick[#, ReleaseHold[#] /. ComplexInfinity -> 0, 24] & // DeleteDuplicates // Map[Map[InputForm]] // Quiet;
calc24[{1, 5, 7, 9}]
(* {(1 - 7)*(5 - 9)} *)