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.

- Exactly how many solutions exist for solving 24, given a set of 4 integers each between 1 and 9?
- 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).

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 |
---|---|

2 | 479 |

3 | 469 |

6 | 462 |

1 | 462 |

5 | 459 |

4 | 457 |

7 | 454 |

9 | 448 |

8 | 443 |

10 | 438 |

12 | 435 |

11 | 406 |

15 | 401 |

14 | 400 |

16 | 397 |

18 | 396 |

24 | 381 |

13 | 376 |

20 | 362 |

21 | 349 |

36 | 345 |

17 | 333 |

30 | 321 |

28 | 319 |

27 | 317 |

19 | 313 |

48 | 304 |

40 | 295 |

32 | 293 |

22 | 293 |

25 | 275 |

23 | 274 |

72 | 273 |

35 | 271 |

42 | 268 |

26 | 268 |

45 | 262 |

54 | 260 |

60 | 248 |

56 | 245 |

33 | 245 |

29 | 232 |

63 | 220 |

34 | 213 |

31 | 209 |

44 | 205 |

39 | 205 |

64 | 188 |

50 | 188 |

49 | 188 |

38 | 185 |

90 | 183 |

70 | 182 |

84 | 180 |

80 | 177 |

96 | 176 |

37 | 175 |

41 | 168 |

51 | 166 |

52 | 162 |

46 | 162 |

55 | 155 |

66 | 154 |

47 | 151 |

43 | 150 |

57 | 143 |

58 | 135 |

81 | 132 |

53 | 127 |

78 | 126 |

65 | 125 |

75 | 124 |

62 | 119 |

68 | 117 |

69 | 115 |

88 | 114 |

77 | 109 |

76 | 108 |

61 | 107 |

59 | 107 |

99 | 92 |

67 | 90 |

74 | 88 |

71 | 88 |

100 | 86 |

98 | 84 |

73 | 83 |

91 | 77 |

79 | 74 |

85 | 73 |

92 | 71 |

87 | 71 |

82 | 70 |

93 | 61 |

95 | 56 |

89 | 56 |

86 | 56 |

83 | 54 |

97 | 40 |

94 | 40 |

314 | 2 |

5 6 8 8