Make a 24 Game Solver The 24 Game is a card game. On each cards are four interger numbers. For a player, the goal is to use Addition, Subtraction, Multiplication and Division to end up with 24.

The goal of this code golf/puzzle is to write a program that, for four intergers, will state the one or multiple solutions possible with those numbers.

Further notes:


Should work for an input of any four interger numbers (below 24, above 0). If there is no solution, display 0 solutions as output.
Should output the total number of solutions for this set of four numbers as well as the formulas used.

Brackets ()  can be used (and are often important) to get one or multiple solutions.

Should only display distinct solutions. For instance, when given {1, 1, 4, 4}, it should not display the cases where the ones and the fours are swapped. Also, if have a solution (a*b)*(c*d), you should not output (c*d)*(a*b) as well, for instance.


An example:
For {9, 6, 3, 2}, the program should output:

   (9-3-2)×6
   (6+2)×9/3
   6×2+9+3
   (9-3)×(6-2)
   (3-2/6)×9
   9×6/2-3
   9×3-6/2
   (9+6-3)×2
   8 solutions


Kudos if you can make a working program that follows the rules outlined above.

Even more kudos if you do this in as little code as possible.

Happy Code Puzzling/Golfing!
Ruby, 253 243 (incomplete, many duplicates, no error message)

g=gets.split.map(&amp;:to_i).permutation(4)
s=[0,2,4,6]
%w[+ - * /].repeated_permutation(3).map{|x|g.map{|y|s.map{|b|s.select{|e|e&gt;b}.map{|a|y.zip(x).flatten[0..6].join.insert(a+1,?)).insert(b,?()}}}}.flatten.each{|x|puts x if 24==eval(x)rescue''}


I got this so far, but there are a lot of duplicates. Example run:

c:\a\ruby&gt;24
9 6 3 2
(9+3)+6*2
(9+3+6*2)
9+(3+6*2)
9+3+(6*2)
(9+3)+2*6
(9+3+2*6)
9+(3+2*6)
9+3+(2*6)
(3+9)+6*2
(3+9+6*2)
3+(9+6*2)
3+9+(6*2)
(3+9)+2*6
(3+9+2*6)
3+(9+2*6)
3+9+(2*6)
(9+6-3)*2
(6+9-3)*2
(9+6*2)+3
(9+6*2+3)
9+(6*2)+3
9+(6*2+3)
(9+2*6)+3
(9+2*6+3)
9+(2*6)+3
9+(2*6+3)
(3+6*2)+9
(3+6*2+9)
3+(6*2)+9
3+(6*2+9)
(3+2*6)+9
(3+2*6+9)
3+(2*6)+9
3+(2*6+9)
(6+2)*9/3
(2+6)*9/3
(9-3+6)*2
(6-3+9)*2
(9-3-2)*6
(9-2-3)*6
(6*2)+9+3
(6*2+9)+3
(6*2+9+3)
6*2+(9+3)
(6*2)+3+9
(6*2+3)+9
(6*2+3+9)
6*2+(3+9)
(2*6)+9+3
(2*6+9)+3
(2*6+9+3)
2*6+(9+3)
(2*6)+3+9
(2*6+3)+9
(2*6+3+9)
2*6+(3+9)
2*(9+6-3)
2*(6+9-3)
9*(6+2)/3
9*(2+6)/3
2*(9-3+6)
2*(6-3+9)
6*(9-3-2)
6*(9-2-3)
(9*3)-6/2
(9*3-6/2)
9*3-(6/2)
(3*9)-6/2
(3*9-6/2)
3*9-(6/2)
(9*6)/2-3
(9*6/2)-3
(9*6/2-3)
9*(6/2)-3
(6*9)/2-3
(6*9/2)-3
(6*9/2-3)
9/3*(6+2)
9/3*(2+6)
(6/2)*9-3
(6/2*9)-3
(6/2*9-3)


It will probably prove very difficult to eliminate duplicates. I am working on it, but I am afraid it will triple my code size, so I'm posting here first. aaaaaaahhhhh my brain hurts now, so I will continue working on this later :P