Skip to content
Vamshi Jandhyala

Books · Solving Puzzles through Mathematical Programming

Chapter 56

The Apex

Stack the digits into a triangle, apex at the top, and let each cell be the sum of the two beneath it. Sums grow, so to keep everything a single digit the addition is taken modulo nine, with nine standing in for zero. That much is a parlour trick: pick any bottom row, add your way up, and the apex is fixed. The puzzle turns it around. Fill the whole triangle, all thirty-six cells, using each of the digits one to nine exactly four times, and still have every cell be the sum of its two supports. Now the bottom row is no longer free; it has to be chosen so the pyramid it grows comes out balanced.

Adding upward, modulo nine

The triangle has eight rows, of one cell, then two, and so on to eight, and 1+2++8=361 + 2 + \cdots + 8 = 36 cells in all, room for four copies of each digit. The rule binding the rows is addition mod nine: a cell equals the sum of the two below it, less nine if that sum runs to ten or more. Nothing in that rule cares about the digit counts, and nothing in the counts cares about the sums; the puzzle is the demand that one arrangement satisfy both at once.

Variables

Index the cells by row ii and position jj. A binary variable places each digit, and the cell’s value is read off it: xijk=1    cell (i,j) holds digit k,Vij=k=19kxijk.x_{ijk} = 1 \iff \text{cell } (i, j) \text{ holds digit } k, \qquad V_{ij} = \sum_{k=1}^{9} k\, x_{ijk} .

Constraints

Each cell holds one digit; each digit is used four times across the triangle; and each cell equals the sum of its two supports, with a binary dijd_{ij} subtracting the nine when the sum overflows: kxijk=1 (cells),i,jxijk=4 (k),\sum_{k} x_{ijk} = 1 \ (\forall\, \text{cells}), \qquad \sum_{i,j} x_{ijk} = 4 \ (\forall\, k), Vij=Vi+1,j+Vi+1,j+19dij(cells with two below).V_{ij} = V_{i+1,\,j} + V_{i+1,\,j+1} - 9\, d_{ij} \qquad (\forall\, \text{cells with two below}) . Because each cell already holds a value between one and nine, the dummy dijd_{ij} is forced: it is one exactly when the two supports sum to ten or more. There is no objective; any filling that meets the three is a solution.

from ortools.sat.python import cp_model

CELLS = [(i, j) for i in range(8) for j in range(i + 1)]

def solve_apex():
    m = cp_model.CpModel()
    x = {(i, j, k): m.NewBoolVar("")
         for (i, j) in CELLS for k in range(1, 10)}
    val = {}
    for (i, j) in CELLS:
        m.Add(sum(x[i, j, k] for k in range(1, 10)) == 1)
        val[i, j] = sum(k * x[i, j, k] for k in range(1, 10))
    for k in range(1, 10):
        m.Add(sum(x[i, j, k] for (i, j) in CELLS) == 4)
    for i in range(7):
        for j in range(i + 1):
            d = m.NewBoolVar("")
            m.Add(val[i, j] == val[i + 1, j]
                  + val[i + 1, j + 1] - 9 * d)
    s = cp_model.CpSolver()
    s.Solve(m)
    return [[s.Value(val[i, j]) for j in range(i + 1)]
            for i in range(8)]

The model returns a filling at once. One of them, apex at the top, reads 954867269783361357519214328192857446\begin{array}{c} 9 \\ 5 \quad 4 \\ 8 \quad 6 \quad 7 \\ 2 \quad 6 \quad 9 \quad 7 \\ 8 \quad 3 \quad 3 \quad 6 \quad 1 \\ 3 \quad 5 \quad 7 \quad 5 \quad 1 \quad 9 \\ 2 \quad 1 \quad 4 \quad 3 \quad 2 \quad 8 \quad 1 \\ 9 \quad 2 \quad 8 \quad 5 \quad 7 \quad 4 \quad 4 \quad 6 \end{array} Read any cell against the two below it: the apex is 5+4=95 + 4 = 9; under it 8+6=148 + 6 = 14 drops a nine to 55, and 6+7=136 + 7 = 13 drops a nine to 44; and so on down, every line checking. Count the digits and each of the nine appears four times. The puzzle carries no clues, so this is not the only filling: its left-to-right mirror is another, since reflecting the triangle leaves both the digit counts and the sum rule intact, and the model returns a different pyramid again whenever this one is forbidden. What the clueless puzzle really asks is whether a balanced pyramid exists at all, and the model answers by building one.

Sources. The Apex puzzle is modelled by Martin J. Chlond, Puzzle—IP Satisfied, INFORMS Transactions on Education volume 1414, number 33 (20142014), pages 140140142142, which gives the single-digit, four-of-each, and mod-nine sum constraints used here. Chlond attributes the puzzle to a card trick of the magician Harry Lorayne, described in Martin Gardner’s Mathematical Carnival (Mathematical Association of America, 19791979). The filling shown is the present chapter’s own, produced and checked against all three rules by the model above; the puzzle as posed has no givens and admits more than one filling, its mirror among them.