Proof of solutions to “24” operations game

EDIT: there was a bug in my code so it missed some solutions. There are actually only 48 unsolvable hands, not 56.

EDIT 2: due to rounding errors, my code thought [9,7,5,5] was solvable, when in fact it is not (see update below).

I have played that game too, so when I saw your question I was pretty sure there are some hands that have no series reaching 24. Since there's only $\binom{52}{4} = 270,725$ possible hands of 4 cards (even less since this game doesn't count suits), I figured it would be feasible to do a computer search to find all of these solutions (see the bottom for my code).

My program found solutions for all but 48 hands (ignoring different suits):

[1,1,7,7] [1,1,9,9] [1,1,10,10] [1,1,10,11] [1,7,7,13] [1,9,9,9] [1,9,10,10] [1,10,10,10] [1,10,10,11] [1,10,11,11] [1,11,11,11] [1,13,13,13] [6,6,6,13] [6,6,7,7] [6,6,7,13] [6,6,13,13] [6,7,7,13] [6,7,13,13] [6,8,8,13] [6,11,11,13] [7,7,7,7] [7,7,7,13] [7,7,10,10] [7,7,10,12] [7,7,11,11] [7,7,13,13] [7,8,9,9] [7,10,10,13] [7,11,11,13] [7,13,13,13] [8,8,9,9] [8,8,11,11] [8,9,9,10] [8,9,13,13] [9,9,9,9] [9,9,9,11] [9,9,10,10] [9,9,13,13] [9,10,10,10] [9,10,10,11] [9,10,11,11], [10,10,10,10] [10,10,10,11] [10,10,11,11] [10,10,13,13] [10,11,11,11] [11,11,11,11] [13,13,13,13]

Counting suits, this works out to be 2413 possibilities out of the 270,725 total hands, or a probability of just a tad below 0.9%.

Since my program did not use factorials of any numbers larger than 13, so it is possible that some of these 48 actually do have solutions, but I don't find that very likely. Among these combinations all have repeated cards. None of the combinations have a 2, 3, 4, or 5, and only one has a 12. Surprisingly, the most common number in the unsolvable hands is 10 (accounting for suits, it shows up in 1109 out of the 2413 unsolvable hands). As someone who has played this game a lot, I was expecting 11 or 13 to be the most likely to give an unsolvable hand, but they only showed up in 845 and 1073 unsolvable hands, respectively.

As for the number of ways to get to 24 from a given hand, my program didn't really look at that because I was trying not to make the program take up to much computational power, and it was much easier to simply remember a single boolean than a whole chain.


Rounding errors update:

[9,7,5,5] is not a solution, but my code thinks it is due to rounding errors. For example, it couldn't distinguish $9^{5-7!}$ from $0$, and found this "solution":$$ \left(5 - \left(9^{5-7!}\right)!\right)! \approx 24 $$ If you use the gamma function to interpolate the factorials, this is off by only about$5\times 10^{-4804}$.

Removing exponentiation from the options, other than using $1^a = 1$, shows that this is the only solution that took advantage of this particular numerical error. Since I didn't ever take a factorial of a number bigger than 13, the factorials should not have introduced any other fake solutions.


Some interesting example hands: Among the hands that have solutions, I've chosen to highlight a few particularly interesting ones:

[13,11,10,6], [13,6,1,1], [13,12,10,8], [11,11,5,5], [11,11,5,1], [9,7,7,7], and [5,1,1,1]

If you want to challenge yourself, see if you can solve these before looking at the solutions.

[13,11,10,6]

This is the only hand that requires a factorial of a number bigger than 10: $$24 = \left(\frac{13 + \frac{11!}{10!}}{6}\right)!$$

[13,6,1,1]

This and [9,8,1,1] are the only ones that require use of exponentiation: \begin{eqnarray} 24&=&\left(\frac8{1+1^9}\right)!\\24&=&\left(6 - 1^{13} - 1\right)! \end{eqnarray}

[9,7,7,7]

This hand has only one possible solution, and is an interesting example of one which requires factorials but doesn't use $4!=24$. $$24 = \frac{9!}{7!+7!+7!}$$

If you disallow factorials, there are fully 430 unsolvable hands (not counting suits). Some of my favorite solutions without factorials include [11,11,1,5], [13,12,10,8], [11,11,5,5] and [5,1,1,1], which have the following unique non-factorial solutions:

[13,12,10,8]

$$24=\frac{10\cdot 12}{13-8}$$

[11,11,5,5]

$$24=5\cdot5-\frac{11}{11}$$

[11,11,5,1]

$$24=\frac{11\cdot 11 - 1}{5}$$

[5,1,1,1]

$$5^{1+1} - 1$$


Below is my Haskell program. Programming is not my strong point, so excuse the messiness.

unsolvable = p 4
-- all hands that have no way to reach 24.

-- derivation:

-- returns True if an input four card hand is solvable.
check :: (Num a, Fractional a, Eq a, Ord a, Floating a, Enum a) => [a] -> Bool
check [a,b,c,d] = firstCheck a b c d
check _ = False

--check as a four-argument function
firstCheck :: (Num a, Fractional a, Eq a, Ord a, Floating a, Enum a) => a -> a -> a -> a -> Bool
firstCheck a b c d = or [fCheck a b c d, fCheck b c d a, fCheck c d a b, fCheck d a b c, fCheck d b a c, fCheck a c b d]

fCheck :: (Num a, Fractional a, Eq a, Ord a, Floating a, Enum a) => a -> a -> a -> a -> Bool
fCheck a b c d = or $ map (secondCheck a b) $ op c d

{--finalCheck :: (Num a, Fractional a, Eq a, Ord a, Floating a, Enum a) => a -> a -> Bool
finalCheck a b = (24 `elem` s) 
    where s = op a b --}

--check if 3 numbers can make 24
sCheck :: (Num a, Fractional a, Eq a, Ord a, Floating a, Enum a) => a -> a -> a -> Bool
sCheck a b c = or $ map (finalCheck a) $ op b c

secondCheck :: (Num a, Fractional a, Eq a, Ord a, Floating a, Enum a) => a -> a -> a -> Bool
secondCheck a b c = (sCheck a b c) || (sCheck b a c) || (sCheck c a b)

-- checks if 2 numbers can combine to 24 or 4! = 24
finalCheck :: (Num a, Fractional a, Eq a, Ord a, Floating a, Enum a) => a -> a -> Bool
finalCheck a b = (24 `elem` s) || (4 `elem` s)
    where s = op a b

--all operations except factorial, with the assumption that a a -> a -> [a]
opp 0 b = [0,1,b,-b, 1+b, b-1, 1-b]
opp 1 b = [1+b,b,1/b,1-b,b-1,1]
opp a b = [a+b,a*b,a/b,a-b,b-a,b/a,a**b,b**a]

-- op includes operations from opp as well as manually including factorials up to 13!
op ::  (Num a, Fractional a, Eq a, Ord a, Floating a, Enum a) => a -> a -> [a]
op 3 b = opp (min 3 b) (max 3 b) ++ op b 6 ++ op 6 b
op 4 b = opp (min 4 b) (max 4 b) ++ op b 24
op 5 b = opp (min 5 b) (max 5 b) ++ op b 120 
op 6 b = opp (min 6 b) (max 6 b) ++ op b 720
op 7 b = opp (min 7 b) (max 7 b) ++ op b 5040 
op 8 b = opp (min 8 b) (max 8 b) ++ op b 40320 
op 9 b = opp (min 9 b) (max 9 b) ++ op b 362880 
op 10 b = opp (min 10 b) (max 10 b) ++ op b 3628800 
op 11 b = opp (min 11 b) (max 11 b) ++ op b 39916800
op 12 b = opp (min 12 b) (max 12 b) ++ op b 479001600
op 13 b = opp (min 13 b) (max 13 b) ++ op b 6227020800
op a b = opp (min a b) (max a b) 


lcheck :: (Num a, Fractional a, Eq a, Ord a, Floating a, Enum a,Enum a) => [a] -> [Bool]
lcheck [a,b,c,d] = [check [a,b,c,d]]
lcheck s = map (and . lcheck . (\n -> n:s)) [(head s)..13] 
-- s must be in order from largest to smallest.
-- (lcheck s !! i) is True whenever there is a solvable hand sorted 
-- from largest to smallest that ends with (head s + i):s

ccheck :: (Num a, Fractional a, Eq a, Ord a, Floating a, Enum a,Enum a) => [a] -> Bool
ccheck = and . lcheck
-- returns True if a sorted hand ending with s is solvable

p1 = filter (not . (\k -> (ccheck [k])) . fromIntegral) [1..13]
-- possible smallest cards for an unsolvable hand

pf :: (Integral a) => [a] -> [[a]]
pf x = map (\s -> s:x) $ filter (not . (\k -> (ccheck $ map fromIntegral (k:x))) . fromIntegral) [(head x)..13] 
-- if x is n cards, pf x lists possibilities of length n+1 endings for sorted hands that end in x

p :: Int -> [[Integer]]
p 1 = map (\x -> [x]) p1
p n = foldr (++) [] $ map pf $ p (n-1)
-- lists possible last n cards of a sorted unsolvable hand.

Dark Malthorp showed that there are combinations of cards that cannot yield 24 from those operations you specified. I'll try to answer your other question about what strategy could someone use. Given the number/types of allowed operations, I think this strategy is unfeasible but it's only to showcase what we can do if the game was less complex (no ^, smaller numbers, etc.). EDIT: See comments on why $!$ cannot be removed.

Every formula built from $v_1,v_2,v_3,$ and $v_4$, using the operations $+,-,\cdot,/,$^$,$ and $!$, is of the following five forms:

  1. $(((v_1!_n*v_2!_n)!_n*v_3!_n)!_n*v_4!_n)!_n$
  2. $((v_1!_n*(v_2!_n*v_3!_n)!_n)!_n*v_4!_n)!_n$
  3. $(v_1!_n*((v_2!_n*v_3!_n)!_n*v_4!_n)!_n)!_n$
  4. $(v_1!_n*(v_2!_n*(v_3!_n*v_4!_n)!_n)!_n)!_n$
  5. $((v_1!_n*v_2!_n)!_n*(v_3!_n*v_4!_n)!_n)!_n$

where $*$ is a binary operation $+,-,\cdot,/,$ or ^, and $!_n$ is an $n$ number of factorials $!$. It looks really ugly since a $!_n$ may appear after every $v_n$ and parenthesis.

I had a feeling that $v_1=v_2=v_3=v_4=13$ would work. Informally, making all the numbers the same "limits" what the operations can do in a sense, and Dark confirmed that 13,13,13,13 is one of the combinations that cannot yield 24.
So let's try proof by contradiction. This is in no way a proof since I haven't actually checked every case.

Suppose $v_1=v_2=v_3=v_4=13$, and that every form yields 24. Then in every form, $n$ in the outermost $!_n$ is zero.* (* means not fully justified without a lot of case checking). First, we have that $m!=24$ iff $m=4$. Suppose $n=2$, then the same formula with outermost $!_{n-1}$ in place of $!_n$ must yield $4$ (since then $4!=24$). In this case we require $\phi!_1$=4, where $\phi$ stands for the formula without the outermost $!_{n-1}=!_1$. But there is no number $m$ s.t. $m!=4$, contradiction, so $n\neq2$. We repeat this argument for $n>2$. Okay now is the part where we use $v_1=v_2=v_3=v_4=13$, and where I don't check the cases. Suppose $n=1$, then we require $\phi!_0=\phi=4$, which I do not believe is possible, so $n=0$.* (To actually check this would require a lot of computing). Then $\ldots$

As you can see, this devolves into A LOT of case checking. We'd have to eventually check the $!_n$'s in $\phi$ and subsequent subformulas are $!_0$.* I believe this as informally, getting $13!$ back to a lower number requires division by at least $13!$, taking up an operation slot. Maybe this is untrue of smaller numbers; this is another reason I chose 13,13,13,13 and not some other quadruple of same numbers, along with the fact that it doesn't seem you can get factors of 24 easily from 13.

Anyways by fixing ALL the $!_n$'s with a finite number of $n$'s, and noting there are only a finite number of combinations of binary operations, you'd have a finite number of cases to check.

I hope this helps in your future problems.