The M2S3 Game

The M2S3 game is a simple arithmetic puzzle. Given two numbers, the task is how to get from one number to the next using only multiplication by two (M2) and subtraction of three (S3). The goal is to minimize the number of steps.

Contents

Definition and Examples

In the M2S3 game, a sequence \(\sigma\) consists of a list of zero, one, or more operations, each of which is either \(\times 2\) or \(-3\). The sequence can be applied to any sufficiently large positive integer \(x\) by performing the operations in the given order; we will write \(\sigma(x)\) for the outcome, which is necessarily also a positive integer. \((\)If any of the steps in calculating \(\sigma(x)\) yields a non-positive value, we will declare \(\sigma(x)\) undefined.\()\)

A concrete M2S3 puzzle is as follows: given an initial value \(x\), find the sequence that leads to a given outcome \(y\) in a minimum number of steps; that is, for which sequence \(\sigma\) of minimum length is \(\sigma(x) = y\)?

Start with \(x = 8\) and apply both of the possible operators. Do the same for the results, and so on. As soon as the outcome is 11, the search is complete, and the solution is guaranteed to be minimal. This solution is illustrated below. The dashed boxes indicate numbers that occurred in an earlier stage of the process, and therefore need not to be explored because it would only lead to longer-than-minimal sequences.

Start with \(y = 11\) and work backward. The inverse operation \(+3\) can be applied to each number, but the inverse operation \(\div 2\) only applies to even numbers. This method is more efficient than the first, because fewer attempts are needed.

Instead of exhaustive searching as in 1 and 2 above, one can also calculate the solution. This will be discussed in the sections below.

Modulo 3 Considerations

Is there a way to determine whether a solution \(\Sigma(x,y)\) exists and what that solution is, without exhaustive search?

Arithmetic modulo 3 gives some interesting insights. The positive numbers can be sorted into three bins: multiples of three, one less than multiples of three, and one more than multiples of three. Thus we will write
\[\begin{cases} x \equiv 0 & \text{if}\ x = 3n, \\
x \equiv -1 & \text{if}\ x = 3n-1, \\
x \equiv 1 & \text{if}\ x = 3n+1. \end{cases}\]
Now consider the effect of the operations on these bins:

Subtracting three results in a number from the same bin. In other words, \(x - 3 \equiv x\).

Multiplication by two has the same effect as flipping the sign: \(x \times 2 \equiv -x\).

Therefore, if we begin with a multiple of three \((x \equiv 0),\) the same will be true for every next number in the calculation. If we begin with any other number (\(x \equiv \pm 1\)), the same will remain true; only the multiplication by two will swap back and forth between the \(+1\) and \(-1\) bins. This leads to the following conclusions:

If \(x\) is a multiple of 3, but \(y\) is not, then there is no solution to the M2S3 game. Likewise, if \(y\) is a multiple of 3, but \(x\) is not, there is no solution.

If \(x\) and \(y\) are both multiples of 3, all intermediate values in the calculation are multiples of 3. We might as well divide everything by three, and play a reduced game M2S1, where we subtract one instead of three. For instance,
\[12 \to 24 \to 21 \to 18 \to 36 \to 33\ \ \ \ \ \text{becomes}\ \ \ \ \ 4 \to 8 \to 7 \to 6 \to 12 \to 11.\]

If \(x\) and \(y\) are not multiples of 3, but belong to the same bin \((x \equiv y),\) then any sequence \(\sigma(x) = y\) must contain an even number of multiplications by two. Likewise, if \(x\) and \(y\) are not multiples of 3, but belong to opposite bins \((x \equiv -y),\) then any sequence \(\sigma(x) = y\) must contain an odd number of multiplications by two.

Existence of a Solution

As long as either both or neither of \(x\) and \(y\) are multiples of three, a solution \(\Sigma(x,y)\) is guaranteed to exist. We prove this by generating a sequence such that \(\sigma(x) = y\); it is usually not the minimal sequence, but if one such sequence exists, there must be a minimal one as well.

The idea is first to multiply by two as much as necessary to make the number big enough, and then to subtract three until the desired value is reached.

Step A. Choose a number \(n\) such that \(2^n x > y\), and moreover,

if \(x,y\) are not multiples of three, \(n\) should be even if \(x \equiv y\) and odd if \(x \not\equiv y\);

Exercise: Suppose we play the game M4S5, with the operations \(\times 4\) and \(-5\). Under what conditions does a sequence \(\sigma(x,y)\) exist?

Minimizing Subtractions

In the previous section we ended up with many subtractions at the end of the sequence. The number of subtractions may be reduced by placing them earlier in the sequence. The idea is that any subsequent multiplication by two "amplifies" the subtraction:
\[x\cdot 2\cdot 2\cdot 2 - 3 = 8x - 3,\ \ \ \text{but}\ \ \ (x\cdot 2-3)\cdot 2\cdot 2 = 8x - 12.\]

We can repeat this process until no two successive subtractions remain, except possibly at the beginning of the sequence. Thus we find a sequence of the form
\[\sigma = [\underbrace{-3, -3, \dots, -3}_{q\ \text{times}}, \times 2, (-3), \times 2, (-3), \dots, \times 2, (-3)].\]
Here the brackets \((-3)\) indicate that this step is optional.

The sequence contains \(n\) multiplications by two. Let us number them from right to left, from \(k = 0\) on the right to \(k = n-1\) on the left. Define
\[r_k = \begin{cases} 0 & \text{if multiplication}\ n\ \text{is not immediately followed by a subtraction}, \\
1 & \text{if multiplication}\ n \text{ is immediately followed by a subtraction}. \end{cases}\]
The numbers \(r_k\) may be interpreted as bits--digits in a binary numeral \(r\). We have
\[r = \sum_{k=0}^{n-1} r_k 2^k,\ \ \ p = 2^n q + r.\]
Here, \(p\) is the "effective" number of subtractions of three, as defined above.

Challenge: Convince yourself that this formula for \(p\) is correct.

We now have a sequence starting with \(q\) subtractions, containing \(n\) multiplications, and inserting \(\#r = \sum r_k\) subtractions after the multiplications. (I will write \(\#r\) for the number of ones in the binary representation of \(r\).) Thus the sequence has length
\[\ell(\sigma) = n + q + \#r.\]
Since we have minimized the number of subtractions, this sequence is guaranteed to be the shortest for the chosen value of multiplications, \(n\). We will call the sequence generated this way \(\Sigma(x,y,n)\).

Let \(x = 16\) and \(y = 142\). Then \(x \equiv y \equiv 1\), so that we need an even number of multiplications \(n\). Let us choose \(n = 6\). \((\)Note that \(\tilde n(x,y) = 4\) in this case; we do not pick the minimal value for \(n.)\)

Exercise: Repeat this calculation with the minimal number of multiplications \(n = 4\). How does the sequence you find compare to the one with \(n = 6\)?

Wrapping it up

For a complete solution of the M2S3 puzzle, all that remains is to determine the value of \(n\) for which \(\Sigma(x,y,n)\) is minimal. It is tempting to conclude immediately that this happens for the minimal possible value \(n = \tilde n(x,y)\); that is, \(\Sigma(x,y) = \Sigma(x,y,\tilde n)\). But this must be proven first.

Can you prove this?

Conjecture: If a sequence \(\sigma(x) = y\) has \(n\) multiplications, and \(n > \tilde n = \tilde n(x,y)\), then it contains at least one cycle. Removing the longest cycle will remove precisely \(n - \tilde n\) multiplications, and will result in the shortest possible sequence.