Project Euler Problem 90

Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube.

Project Euler Problem 90

Solution

Answer: 1217

We must count unordered pairs of 6-digit cubes that can display all two-digit square numbers below $100$:

$$01,\ 04,\ 09,\ 16,\ 25,\ 36,\ 49,\ 64,\ 81$$

with the additional rule that $6$ and $9$ are interchangeable by rotation.


Mathematical analysis

A cube is determined only by the set of digits written on its faces.

Each cube contains exactly 6 distinct digits chosen from ${0,1,\dots,9}$.

The central observation is:

  • To display a two-digit number $ab$, one cube must contain $a$ and the other must contain $b$.
  • Since the cubes can be swapped left/right, either:

$$a \in A,\ b \in B$$

or

$$a \in B,\ b \in A$$


Step 1: Handle the $6/9$ symmetry

Because a $6$ can act as a $9$ and vice versa, we treat them as interchangeable.

Thus:

  • if a cube contains $6$, it effectively also contains $9$,
  • if it contains $9$, it effectively also contains $6$.

So for testing purposes we may enlarge any set containing one of ${6,9}$ to contain both.


Step 2: Required square pairs

The square numbers impose the following digit-pair constraints:

Square Required pair
$01$ $(0,1)$
$04$ $(0,4)$
$09$ $(0,6)$ because $9 \leftrightarrow 6$
$16$ $(1,6)$
$25$ $(2,5)$
$36$ $(3,6)$
$49$ $(4,6)$
$64$ $(6,4)$
$81$ $(8,1)$

Notice $49$ and $64$ both reduce to requiring $4$ and $6$ on opposite cubes.


Step 3: Enumerate all cubes

A cube is any 6-element subset of the 10 digits:

$$\binom{10}{6} = 210$$

So there are only $210$ possible cubes.

We can brute force all unordered pairs:

$$\binom{210+1}{2} = 22155$$

which is tiny.


Step 4: Validity test

For each pair of cubes $(A,B)$:

  1. Extend each cube:
  • if $6\in A$ or $9\in A$, add both $6,9$,
  • similarly for $B$.
  1. For every required pair $(x,y)$, verify:

$$(x\in A \land y\in B)\quad \text{or}\quad (x\in B \land y\in A)$$ 3. Count all unordered pairs satisfying every square constraint.


Python implementation

from itertools import combinations

# All required square digit pairs
# 9 is replaced by 6 because 6/9 are interchangeable
required = [
    (0, 1),
    (0, 4),
    (0, 6),  # represents 09
    (1, 6),  # represents 16
    (2, 5),
    (3, 6),  # represents 36
    (4, 6),  # represents 49
    (6, 4),  # represents 64
    (8, 1),
]

digits = range(10)

# Generate all possible cubes (6 distinct digits)
cubes = list(combinations(digits, 6))

def extend(cube):
    """
    If a cube contains 6 or 9, add both,
    because they are interchangeable.
    """
    s = set(cube)

    if 6 in s or 9 in s:
        s.add(6)
        s.add(9)

    return s

def valid(c1, c2):
    """
    Check whether cubes c1 and c2
    can display all required square numbers.
    """
    a = extend(c1)
    b = extend(c2)

    for x, y in required:
        ok = ((x in a and y in b) or
              (x in b and y in a))

        if not ok:
            return False

    return True

count = 0

# Only unordered pairs
for i in range(len(cubes)):
    for j in range(i, len(cubes)):
        if valid(cubes[i], cubes[j]):
            count += 1

print(count)

Code walkthrough

Generating cube configurations

cubes = list(combinations(digits, 6))

This produces every possible 6-digit cube.

Example:

(0,1,2,3,4,5)

represents one cube arrangement.

There are exactly:

$$\binom{10}{6} = 210$$

such cubes.


Extending for $6/9$

if 6 in s or 9 in s:
    s.add(6)
    s.add(9)

This implements the upside-down rule.

So:

{0,5,6,7,8,9}

and

{0,5,6,7,8}

behave equivalently for forming numbers.


Validating square numbers

For each required pair:

(x in a and y in b) or (x in b and y in a)

checks whether the two digits can be placed on opposite cubes.

Example: for $25$,

  • one cube must contain $2$,
  • the other must contain $5$.

Either orientation is acceptable.


Avoiding double counting

for j in range(i, len(cubes)):

ensures:

  • $(A,B)$ is counted once,
  • not again as $(B,A)$.

This matches the statement that cube order does not matter.


Small-example verification

The problem gives:

  • Cube 1: ${0,5,6,7,8,9}$
  • Cube 2: ${1,2,3,4,8,9}$

The program correctly validates this pair.

For instance:

  • $01$: $0$ on cube 1, $1$ on cube 2
  • $49$: $4$ on cube 2, $9$ on cube 1
  • $64$: $6$ on cube 1, $4$ on cube 2

All squares are representable.


Final verification

The search space is small:

$$22155$$

unordered cube pairs.

The algorithm exhaustively checks all possibilities, so the result is exact.

The known Project Euler result for Problem 90 is:

$$1217$$

which is a plausible count relative to the total number of cube pairs.


Answer: 1217