# A simple but difficult arithmetic puzzle, and the rabbit hole it took me down

A while back, Mark Dominus proposed an arithmetic puzzle: combine the numbers 6, 6, 5, and 2 with arithmetic operations (addition, subtraction, multiplication, and division) to get 17. After fiddling with the problem for a bit on my own and not being able to solve it, I decided to write a solver, and I ended up falling down an unexpected rabbit hole and dragging a few friends down with me.

(If you want to try to solve the puzzle on your own, or if you want to write a solver without having seen someone else’s, you may want to go do that before continuing to read this post.)

## First draft

In the spirit of sharing process, here’s my first attempt at a solver. It’s a mess, but it works, sort of. It will find a way, if one exists, of combining four given integers using arithmetic operations to get an expression that evaluates to a given target integer (assuming that the expression is parenthesized in a particular way; more on that later). I happened to hard-code 17 as the target, but it would have been just as easy to have that be an argument. The approach I took is pretty boring: it first generates all permutations of the list of four inputs, then generates all the arithmetic expressions that can be built by sticking +, -, *, or / in between the numbers in each of those permutations. Finally, it evaluates all the resulting expressions and sees which ones, if any, evaluate to the target number.

I wrote it in Scheme because I had originally intended to use miniKanren, but at some point I changed my mind about that and just wrote vanilla Scheme. The less that’s said about my ugly first draft, the better, probably, but I do want to make one process-related observation. I was having trouble writing the permutations function until I rewrote it in Haskell. It turned out that I had been missing a concat in the Scheme version, and I couldn’t figure out what was missing until I rewrote it in Haskell and looked at the type error. I mentioned this to Mark — who had already written at least one solver, but had decided to try to do another one in Scheme when I told him that that’s what I had done — and he said, “I’m at exactly that point in my Scheme implementation also! I have some function that is returning a [[[T]]] when it ought to be a [[T]] and I’m not sure yet where the fault is.” I think it’s interesting that we both ran into more or less the same bug.

Mark also remarked, “I saw your first solution and said to myself ‘She used way too much code, I could write this much shorter’ and I tried and it exploded and now the walls and floor are covered with dripping masses of Scheme code.” Well, we’ve all been there.

## Second draft

Next, I wrote a Racket version that used the same strategy as the Scheme one, but was shorter and produced much easier-to-understand output. I found out that permutations was built into Racket, so I didn’t even need to write it. Here’s my code:

I think the ugliest thing about my Racket solution is the combine2, combine3, and combine4 functions, especially all the repetition between combine4 and combine3. And, despite all that code, it can still only produce solutions with the (op a (op b (op c d))) expression tree shape and therefore misses a lot of solutions.

## Down the rabbit hole

I thought I was done thinking about this problem, but after I tweeted about my solver, a few other people shared their attempts, many of which do clever things. For instance, Darius Bacon’s Python solution distinguishes between commutative and non-commutative operations, which I hadn’t thought to do. Then, Sebastian Fischer shared a cute solution in Haskell, specific to the “combine 6, 6, 5, and 2 to get 17” problem. However, it looks like it’s choosing the three operations to be distinct, which overconstrains the problem.

But the real rabbit hole started with a now-deleted tweet in whch someone offered an incredibly concise Haskell version that didn’t have the distinctness issue. It used replicateM 3 ["+","-","*","/"] to create a list of combinations of operations, which looks like [["+","+","+"],["+","+","-"],...] and has length 64. So, there’s some list monad trickery going on there. The most surprising thing about their solution, though, was that they wrote something like do { opList <- replicateM 3 ["+","-","*","/"]; exprs <- permutations ([6, 6, 5, 2] ++ opList); ... } to generate a massive list containing all valid RPN programs written with those operators and operands, as well as many, many invalid ones. When I questioned this, the author was like, “It’s only 300K programs; it’s not a big deal to evaluate them.” (!)1

I wanted to come up with a nice way to refactor my Racket code to do what replicateM 3 ["+","-","*","/"] was doing, but then I got distracted just trying to figure out what the thing I wanted was even called. Given a set of items (in this case, $\lbrace \texttt{+}, \texttt{–}, \texttt{*}, \texttt{/} \rbrace$), I wanted to enumerate all of the lists of a given length (in this case, 3) whose elements are drawn from the set, with repetitions allowed. My first instinct was to call these things “ordered $k$-multisets”, where $k$ is the length of the lists we’re producing, but that terminology doesn’t seem to be much in use; there are only a handful of Google results for it. These lecture notes from someone’s 2013 discrete math course at TU Vienna, at least, seem to be using it in the way I intended:

The number of ordered $k$-multisets over $A$: $n^{k}$. (Take a fixed number of positions $k$ and for each position choose any element from $A$).

I asked some Racketeers if there was a Racket library with a function that would enumerate the ordered $k$-multisets of a set. No one knew of such a library, but Justin Slepak suggested a clever way to do it: for a set with $X$ elements, convert each of $0,\dots,X^{k}-1$ into base $X$, and then convert each digit (or, uh, $X$-it?) of the resulting base-$X$ numbers back into an element of the original $X$-element set. I never got around to actually implementing that approach, but after Justin described it, I realized that it was in fact the approach taken by this Mathematica code that I’d also come across and not really understood (I had seen things like Module and Thread in the Mathematica code and had given up trying to read it). Then, a comment on that page led me to this diagram, which offers another name for “ordered $k$-multisets”: variations with repetition. Further investigation on Wikipedia revealed that there are several competing names for this notion:

$n$-tuples whose entries come from a set of $m$ elements are also called arrangements with repetition, permutations of a multiset and, in some non-English literature, variations with repetition.

Another Wikipedia page also says that “variations with repetition” is “an archaic term in combinatorics still commonly used by non-English authors”. In any case, it’s still a lot more popular than “ordered $k$-multisets”!

At this point, I had satisfied my curiosity and was ready to move on from this problem, but Michael Arntzenius wasn’t quite done: he forked my Racket code and wrote a solver that can produce answers with all of the possible expression tree shapes, which mine can’t do. Then, when I mentioned the Darius Bacon version that distinguishes between commutative and non-commutative operations, he wrote a version that did that, too. Both rntz and Darius also used generators and other fancy stuff that my code doesn’t use.

## Want another puzzle?

Mark mentioned that a couple of people have written to him to suggest an even harder instance of this problem: from 8, 8, 3, and 3, make 24. He said he puzzled over this one for several days before giving up and asking his solver for help. I also had to give up and ask my own solver after a while, and I was pleased that despite its limitations, it coughed up a correct answer. So, that’s a happy ending of sorts.

1. 332,560, to be precise. By contrast, my solver only needs to try 1536 possible solutions (24 permutations of the input integers, times 64 combinations of operations). Admittedly, though, there are five possible expression tree shapes, and I can only produce solutions with one of those shapes. Five times 1536 is 7680, so I think 7680 of the 332,560 “RPN programs” would actually be valid RPN.