## A Lazier Way to Solve Knuth’s Skeleton Multiplication Puzzle

`        N N N  (1)    x N N N N  (2)    ---------        N N N 7  (3)    N N N N    (4)  N N N N      (5)  N N N        (6)  -----------    7 7 7 7 7 7  (N != 7)`

The answer found by Knuth is the following:

```        5 3 9  (1)
x 1 4 4 3  (2)
---------
1 6 1 7  (3)
2 1 5 6    (4)
2 1 5 6      (5)
5 3 9        (6)
-----------
7 7 7 7 7 7 ```

I was able to find a second solution where there was a leading zero in row (4). To find it, I considered all the prime factors of 777777 and reasoned about which pairs could fit the pattern of the puzzle.

In this post I want to show you how I originally found this solution.

I wrote a bit of code in Python to express part of the problem as a constraint programming (CSP) model. Instead of trying to work out the solution myself, I write down the conditions that must be true, and let the constraint programming solver do the work.

The basic approach for solving a puzzle using a solver is:

1. Write down the variables (unknowns) that I want the solver to find.
2. Write down all of the constraints (logical conditions) that need to be true.
3. Turn the above into the right API calls for the solver I am using.

Here’s how it works (and here’s a link to my code). I used the python_constraint package as my CSP solver. To specify the puzzle we need a Problem() object:

`problem = Problem()`

In a CSP I first define variables that I want the solver to find. The variables I care most about are n_1 and n_2. I know that n_1 < 1000 and n_2 < 10000. addVariable‘s first argument is the name of the variable and the second is the domain.

```problem.addVariable("n_1", list(range(100, 999+1)))

I also am interested in the digits of each number. I will call the digits for the first (d_11, d_12, d_13), and the second (d_21, d_22, d_23, d_24). Remember that none of these digits can be a seven.

```not_seven = [0, 1, 2, 3, 4, 5, 6, 8, 9] # no sevens allowed!

# n_1 = d_13 d_12 d_11

# n_2 = d_24 d_23 d_22 d_21

Now I start writing down conditions that I know to be true. The first is that the product is a bunch of sevens:

`problem.addConstraint(lambda n_1, n_2: n_1 * n_2 == 777777, ['n_1', 'n_2'])`

Even if you know Python this may be confusing because you may not be familiar with the python_constraint package. The point is that I am providing an anonymous (lambda) function that evaluates the product of n_1 and n_2 as the first argument, and the names of the variables I defined earlier as the second.

Next, if I put the digits d_1x together, I get n_1:

```problem.addConstraint(lambda d_11, d_12, d_13, n_1:
100 * d_13 + 10 * d_12 + d_11 == n_0,
['d_11', 'd_12', 'd_13', 'n_1'])```

Same for n_2:

```problem.addConstraint(lambda d_21, d_22, d_23, d_24, n_2:
1000 * d_24 + 100 * d_23 + 10 * d_22 + d_21 == n_2,
['d_21', 'd_22', 'd_23', 'd_24', 'n_2']) ```

Also, as considered earlier, when I multiply the last two digits of n_1 and n_2, the result must end in 7:

`problem.addConstraint(lambda d_11, d_21: (d_11 * d_21) % 10 == 7, ['d_11', 'd_21'])`

Now, if I wanted to be complete, I would write down a bunch more conditions for all of the Xs that appear in rows 3-6, using the rules of long multiplication. But why would I want to do that? The point is to be lazy.

Let’s just solve the problem, ask for all the solutions, and print them out.

```s = problem.getSolutions()
# s is a list of solutions. Each solution is a dict of variable values.
for t in s:
print(f"{t['n_0']} x {t['n_1']} = {t['n_0'] * t['n_1']}") ```

Wow! The same four that I discovered above. This was “easier” for me in the sense that I already knew how to use the python_constraint package, so I could just do the coding above without having to “think”. Doing it “the old fashioned way” was in the end more fun, but requires more deliberation and focus for me.

## A Skeleton Multiplication Puzzle by Knuth

I want to share with you two ways to solve an interesting puzzle from Don Knuth’s “The Art of Computer Programming“. Here is the first!

The puzzle that caught my attention, quite by chance, is the “skeleton multiplication puzzle” given in Exercise 50 of Pre-Fascicle 9b (called “A Potpourri of Puzzles”). We want to find two numbers whose product is 777777, but we are given very little information. Here is the puzzle:

```        N N N  (1)
x N N N N  (2)
---------
N N N 7  (3)
N N N N    (4)
N N N N      (5)
N N N        (6)
-----------
7 7 7 7 7 7  (N != 7)```

All of the “Ns” are unknown, and none of them can be a 7. Notice that I’ve numbered the rows (1) – (6) because I am going to obsess about them below.

This doesn’t seem like a lot to go on, does it? I’ll give you some space to think about it, then I will show you Knuth’s solution.

….

OK, here’s what Knuth says:

“The only 3-digit divisors of 777777 = 3 x 7 x 7 x 11 x 13 x 37 that contain no 7s and don’t end in 1 are (143, 259, 429, 539). Their cofactors are (5439, 3003, 1813, 1443). And only 539 x 1443 gives just seven 7s.”

So that settles that, right? Well, not quite. Let’s dig into the details.

Let’s call the number in row (1) n_1, and the number in row (2) n_2. We want n_1 * n_2 = 777777.

Remember (or perhaps learn for the very first timeā¦) that for every positive integer, there is a unique set of primes whose product is that number. This is called the prime factorization of a number.  We can use the prime factorization of 777777 to narrow down the possibilities for n_1 and n_2.

From Wolfram Alpha, we can get the prime factors: (3, 7, 7, 11, 13, 37).

Since n_1 * n_2 = 777777,  the prime factors of n_1 and n_2 also come from this set. Let’s focus on n_1. If I go through all the subsets of (3, 7, 7, 11, 13, 37) whose product has three digits, here’s what I find. Hopefully I didn’t miss any!

7 * 37 = 259
11 * 13 = 143
11 * 37 = 407
13 * 37 = 481
3 * 7 * 7 = 147
3 * 7 * 11 = 231
3 * 7 * 13 = 273
3 * 11 * 13 = 429
11 * 13 = 143
3 * 7 * 37 = 777
7 * 7 * 11 = 539
7 * 7 * 13 = 637

But remember that N != 7, so that means:

• n_1 can’t have a 7 as a digit
• The last number of n_1 cannot be 1, otherwise the last digit of n_2 would be 7, because the last digit of the product is 7: look at row (3).

So now we’re down to these possibilities:

259 (cofactor is 777777/259=3003)
143 (cofactor 5439)
429 (cofactor 1813)
539 (cofactor 1443)

But a full solution has to avoid 7s in rows (3)-(6). So we need to work out the long multiplication to see which of these possibilities are actually solutions. Go ahead and do that on paper now, or you can cheat here. When you do, you will see that Knuth’s solution using 539 and 1443 does solve the puzzle. Of course it does; he’s Don Frigging Knuth.

```        4 2 9  (1)
x 1 8 1 3  (2)
---------
1 2 8 7  (3)
0 4 2 9    (4)
3 4 3 2      (5)
4 2 9        (6)
-----------
7 7 7 7 7 7  ```

Hey, this works too! This new solution is a bit strange because it has a leading zero in row (4), but nobody said I can’t do that. When I wrote Prof. Knuth, he agreed.

There is one more plot twist. It turns out that what I have written here is *not* the way I discovered this alternate solution; I am far too lazy for that. Instead, I used Python to do the thinking for me. Read more next time!