Remember that math game 24 from middle school? It's the one where you are given 4 numbers and you have to do basic math operations (+,-,*,/) to make the 4 numbers equal 24. For example, if I had the numbers 2,3,4,5 I could get to 24 by doing 2*(5+4+3) or maybe by doing 4*(5+3-2). You get the idea.

So the game itself is pretty simple and for some reason I find myself playing it with the numbers on a clock or credit card or something else when I am bored. And I was finding that it was normally pretty easy to find a solution given any set of numbers. That made me start to ask a couple of questions.

  1. Exactly how many solutions exist for solving 24, given a set of 4 integers each between 1 and 9?
  2. Is there a reason 24 was chosen? Does it have more solutions than another number, say 20?

The first step was to figure out exactly how many sets of 4 numbers there were. We need unique sets here, the set [1,3,3,7] is fundamentally equivalent to [3,1,3,7] for the purposes of the game. Sadly, this took me longer than it should have, my combinatorics must be rusty. But by using the Multiset Coefficient (basically, combinations with replacement) I used the following formula to determine that there are only 495 unique sets. Much less than I expected given that there are 6,561 total sets of numbers (9^4).

\(\mathfrak{\mathit{\frac{(n+k-1)!}{k! (n-1)!}}}\)

The next step is to bust out the IDE and get to coding. Fortunately, this part didn't take as long as the math (my coding isn't rusty yet, phew). Here is a quick little Python 24-solver (really an N-solver...we'll get there) I threw together:

So now I've got a solver and a list of 495 unique sets, time to find out how many of those actually have a solution for 24. Ready? Drumroll....

381 out of 495 have a solution!

381?! Wayyyy more than I was guessing. No wonder this game is so easy, pick 4 numbers and you pretty much have a 3 out of 4 shot that there is a solution. So on to part 2, is there possibly an easier or more difficult number to use as a target? I ran it for target solutions from 1 to 100, and the winner (meaning the number with the most solutions) is....more drumroll...

2! - with 479 solutions out of 495 possible

2 has the most, followed up by the rest of the single-digit numbers interestingly. 24 came in 17th place. 94 and 97 tied for last with 40 solutions each (remember, only checking from 1-100). Here is the full list for your viewing pleasure.

Target# Solutions
2479
3469
6462
1462
5459
4457
7454
9448
8443
10438
12435
11406
15401
14400
16397
18396
24381
13376
20362
21349
36345
17333
30321
28319
27317
19313
48304
40295
32293
22293
25275
23274
72273
35271
42268
26268
45262
54260
60248
56245
33245
29232
63220
34213
31209
44205
39205
64188
50188
49188
38185
90183
70182
84180
80177
96176
37175
41168
51166
52162
46162
55155
66154
47151
43150
57143
58135
81132
53127
78126
65125
75124
62119
68117
69115
88114
77109
76108
61107
59107
9992
6790
7488
7188
10086
9884
7383
9177
7974
8573
9271
8771
8270
9361
9556
8956
8656
8354
9740
9440
3142
One more fun thing; there are actually 2 (and only 2!) sets of 4 that can produce solutions for a target of 314. Here they are, can you come up with the formulas to get there?

1 5 7 9
5 6 8 8