Hyper@2016-03-16

Class=Mathematica Calculating24

Calculating24

ops = Map[Inactive, 

   Tuples[{Times, Plus, Subtract, Divide}, {4 - 1}], {2}];

rules = Thread[{a, b, c} -> #] & /@ ops;

matchQ[list_] := 

 Module[{}, 

  res = Fold[Replace[#1, List -> #2[[1]], {#2[[2]]}, Heads -> True] &,

       list, Transpose@{{a, b, c}, {1, 2, 3}}] /. rules //. 

    Inactive[Subtract][x_, y_] :> Inactive[Plus][x, -y];

  Select[res, Activate[#] == 24 &]]

list = {6, 4, 2, 3};

pattern = {{#[[1 ;; 2]], #[[3 ;; 4]]}, {#[[

      1]], {#[[2]], #[[3 ;; 4]]}}, 

    Reverse@{#[[1]], {#[[2]], #[[3 ;; 4]]}}, {{{#[[1]], #[[2]]}, #[[

       3]]}, #[[4]]}, Reverse@{{{#[[1]], #[[2]]}, #[[3]]}, #[[4]]}} &;

parts = Flatten[(pattern) /@ Permutations[list, {4}], 1];

res1 = matchQ /@ parts // Quiet // Flatten

Calculating24_1.png

When I’m playing with a calculating 24, I used some function like `Inactive`, My question is How can I make the output more reasonble in human writing habbit? for example, here I replace Subtract with Plus

I want to change ((6+4)+-2)*3 to ((6+4)-2)*3

StringForm is also OK, just for display