Exploring the game twenty four and variations on it

Twenty four the basic Game

Twenty four is played with a standard 52 card deck of playing cards. The aces are have the value 1, jacks eleven, queens twelve and kings 13.
The basic game proceeds by having 4 cards turned face up. The first player that can achieve the number 24 exactly using all the cards and only allowed operations (such as addition, subtraction, multiplication, division, and parentheses) wins the hand.

Should I keep trying

The first point to not is that not every combination of cards has a solution. So I went through the process of writing an algorithm to check if a specific combination has a solution. Like most people I did this by having a computer try every possible set of operations in every possible order. I don't program much and have only recently been introduced to javascript, but my algorithm appears to match others on the internet. However, mine differs in a key way. Mine does not tell its solution. It is handy to check to see if you should keep trying. Here is my version of the checker. Then we can go onto some of the interesting math behind the game.




Analysis

Since not every combination has a solution, this immediately makes several questions pop into my mind:

  • What is the probability that a hand has a solution?
  • Given a specific combination how many ways are there to perform operations on them?
  • Do certain cards guarantee that there will be a solution?
  • Do certain cards guarantee that there will not be a solution?
  • What would happen if we used a different number as a target?
  • what would happen if we used a different number of cards?
  • What would happen if we used a broader range of numbers

Some of these questions we can tackle directly, others we may only be able to approximate solutions.

How many card combinations are there really

To know how often there is a solution, we take the number of combinations that have solutions and divide by the total number of card combinations. Easy Right?
Maybe not.
First we have to decide how to count. To do that it might be worth while to introduce some mathy definitions. From Wikipedia:

In mathematics, a combination is a way of selecting items from a collection, such that (unlike permutations) the order of selection does not matter. In smaller cases it is possible to count the number of combinations. For example, given three fruits, say an apple, an orange and a pear, there are three combinations of two that can be drawn from this set: an apple and a pear; an apple and an orange; or a pear and an orange.
This definition is pretty straight forward, but hard to count with larger sets of things. Instead we can look at ordered sets first, because those are a little easier, then figure out how to reduce it to combinations. We start with playing cards. There are 52 of them and they are all different (This matters). We want to pick four cards. For our first card we have 52 choices, for our second card we have 51 choices, for our third 50 choices and for our fourth we have 49 choices. So when order matters (this is a permutation) there are 52*51*50*49=6497400 ordered sets of four cards. That is a lot. But how many different ways can we order those four cards. One card can go in 4 possible places, the next can only go in one of three remaining places, then the next can go in one of two places. The final card can go in the only place remaining. This means there are 4*3*2*1=24 ways to order four different cards. Of all the permutations (ordered selections) there are 24 ways to rearrange them still only using the same four cards. This means there are 6497400/24=270725 combinations of four cards in a standard deck. This is still big, but not as big as before.

That was all assuming all the cards are unique, but in the game, suit doesn't matter. We don't want to repeat a computation if the only thing that changes is the suit. At the same time, we need to know how many ways we can get the same face values by only mixing up the suits, to know how frequently a hand will show up in a game.

>
Number of
distinct Face Values
Partition Suit choice weight factorFace value weight factorTotal combinations
of this type
1 aaaa 1 13 13
2 aaa b 16 156 2496
aa bb 36 78 2808
3 aa b c 96 858 82368
4 a b c d 256 715 183040
Total 1820 270725

We broke the set of all face value combinations into types, based on whether a number was repeated. The first type, there was only one number. Because this uses all four suits, there is only one combination that gives four kings or aces or threes. In contrast, when there are four distinct face values, each could be any suit. So there are 4*4*4*4 combinations that contain the numbers A,2,3,10.

The table breaks down the ways we can get combinations by focusing on how many distinct face values are shown. The weight factor accounts for the different ways these can be combinations can occur based on altering the suits or the numbers involved.

If done correctly the total (which is the product of the weights) will match our previous total for the number of four card hands. And it does. This doesn't guarantee we did everything right, but if it was different we would know we did something wrong.

How many possible solutions do we need to check

There may be some underlying structure that allows us to determine which combinations have solutions without actually solving them, but I don't know it. The fastest way for me to solve this problem was to try all the possible ways of combining the four numbers using three operators. Take some time and try to figure out a way to go through all the possible ways of using the +,-,*./ operators on four numbers. Two key goals are to make sure you get all possible results and you don't repeat a lot of computations.





Think about it for a bit,really





































Each operation takes two numbers and returns one. We will think of each of these operations as a step. How many ordered pairs can we get from our set of four numbers. Four for the first choice and three for the second, 12 possible ordered pairs. But for the addition and multiplication operator order doesn't matter, which means 6 unordered pairs. 12*2+6*2=36 possible first steps.

Step two is similar with less numbers. Now there are three choices for the first number and 2 choices for the second, thus six ordered pairs and half as many unordered pairs 6*2+3*2=18
Step three follows the same pattern. Now there are two choices for the first number and 1 choice for the second, that means 2 ordered pairs and 1 unordered pairs 2*2+1*2=6
The total number of ways 36*18*6=3780
This is not the most efficient way to get all possible results, but it is simple and generalizable and a small number of computations for a computer to do.
I tried to do a quick test, but rounding errors caused the test to be inconclusive. Later we might go back and explore this problem further.

Computer Explorations:

My algorithm has been implements and some of the results tabulated. Of the 1820 types of hands 1361 have at least one solution, this is a success rate of only 75%. But many of the hands that had no solution were rare. If we look at all possible hands 217781 of 270725 have solutions. This is a success rate of 80%. So we expect one hand in five to have no solution.

Target Value: Maybe we should be playing 25 or 27

Call the number we are trying to reach the target. I assumed that 24 was chosen as a target because it was special in some way. It may still be, but the way I thought it would be special was that it would have a higher probability of success than other targets. I decided to test this hypothesis and found all the integers that could be created with our standard deck of cards and four operations using four card hands. The following table gives the results. They are not what I would have expected.

Rank |Target| Target probability
of success
1 2 2 0.9786055961
2 2 -2 0.9493175732
3 3 3 0.9416234186
4 4 4 0.9282740789
5 3 -3 0.9183045526
6 6 6 0.9080062794
7 4 -4 0.9068685936
8 5 5 0.89534029
9 1 1 0.892141472
10 1 -1 0.8903684551
11 6 -6 0.8868076461
12 7 7 0.8823233909
13 9 9 0.8789731277
14 5 -5 0.8783082464
15 8 8 0.8771410103
16 10 10 0.8726198172
17 12 12 0.8726198172
18 14 14 0.8612392649
19 8 -8 0.8588678548
20 7 -7 0.857179795
21 15 15 0.8553698402
22 9 -9 0.8540511589
23 11 11 0.8474688337
24 16 16 0.8403324407
25 10 -10 0.8343817527
26 12 -12 0.8338350725
27 18 18 0.8294837935
28 0 0 0.8156062425
29 13 13 0.8125182381
30 20 20 0.8104423308
31 11 -11 0.8048462462
32 24 24 0.8045692123
33 14 -14 0.7995308893
34 15 -15 0.781013944
35 36 36 0.7620278881
36 18 -18 0.7613999446
37 21 21 0.7567790193
38 22 22 0.7550429403
39 16 -16 0.7549875335
40 13 -13 0.7520916059
41 17 17 0.750558685
42 20 -20 0.7397543633
43 28 28 0.7328691477
44 24 -24 0.7322707545
45 19 19 0.7263533106
46 26 26 0.7260430326
47 30 30 0.7228848462
48 40 40 0.7120768307
49 27 27 0.7064216456
50 48 48 0.6898845692
51 23 23 0.6876276665
52 32 32 0.6858103241
53 60 60 0.6795271955
54 21 -21 0.6663329947
55 17 -17 0.6647372795
56 25 25 0.6646560163
57 33 33 0.6580847724
58 36 -36 0.6506565703
59 22 -22 0.6429734971
60 35 35 0.6406464124
61 72 72 0.6370265029
62 44 44 0.6253467541
63 42 42 0.6240354603
64 19 -19 0.6215347678
65 28 -28 0.6175454797
66 29 29 0.6157983193
67 45 45 0.6121968788
68 39 39 0.6107452212
69 30 -30 0.6032283683
70 40 -40 0.5967679379
71 26 -26 0.5914821313
72 27 -27 0.586174162
73 54 54 0.5859710038
74 48 -48 0.5835404931
75 31 31 0.5814017915
76 56 56 0.5743466617
77 32 -32 0.5742949487
78 34 34 0.5715467726
79 23 -23 0.5647391264
80 60 -60 0.5581789639
81 84 84 0.5534915505
82 52 52 0.5472305845
83 80 80 0.5469461631
84 25 -25 0.5463920953
85 38 38 0.5459414535
86 90 90 0.5372019577
87 120 120 0.5360901284
88 37 37 0.5357503001
89 50 50 0.5338627759
90 66 66 0.5300470958
91 70 70 0.524901653
92 55 55 0.5154049312
93 64 64 0.5128820759
94 33 -33 0.512002955
95 96 96 0.5100969619
96 35 -35 0.5072675224
97 63 63 0.5072121156
98 49 49 0.5004229384
99 42 -42 0.4999353588
100 72 -72 0.4952811894

Relative Difficulty and altering the deck

We can try to gauge the difficulty of a particular combination. My first approach was to count how many solutions my algorithm could produce for a given hand. In general my assumption was that the more solutions there were, the more likely I would be to find them. The amount of time it takes me to solve is only loosely correlated with the rankings. This might not be an effective measure of difficulty.

If we alter the deck, either by removing or adding cards we change the likelyhood of getting a hand that has no solution and the distribution of the difficulty of the hands, but do we change it in a significant way.
If we restricted ourselves to one suit, we could only get combinations where every number was different. If we restricted to only one color, no number could show up more than twice. We could remove the face cards. These are all relatively simple alternations. We could could create a deck that had two aces, a king, a jack and three sevens removed. This removes the symmetry involved in our calculations and they become more challenging.