Place the numbers 1,2,3,4,5,6,7 and 8 one at each corner of a cube in such a way that the sum of the numbers on each of the 6 faces of the cube is the same.


In [29]:
import numpy as np
import itertools
from pprint import pprint

In [12]:
nums = np.array([1,2,3,4,5,6,7,8])
5--6 | | 6--5--1--2--6 | | | | | 0--7--3--4--0 | | 7--0

In [13]:
faces = [(5,6,7,0),
        (1,3,5,7),
        (1,2,3,4),
        (2,4,6,0),
        (3,4,7,0),
        (1,2,5,6)]

In [14]:
for face in faces:
    print(np.take(nums, face))


[6 7 8 1]
[2 4 6 8]
[2 3 4 5]
[3 5 7 1]
[4 5 8 1]
[2 3 6 7]

In [31]:
results = set()
for perm in itertools.permutations(nums):
    sums = set()
    for face in faces:
        sums.add(sum(np.take(perm, face)))
    
    if len(sums) == 1:
        res = frozenset(frozenset(np.take(perm, face)) for face in faces)
        if res not in results:
            results.add(res)
            pprint(res)


frozenset({frozenset({8, 1, 4, 5}),
           frozenset({2, 3, 6, 7}),
           frozenset({1, 4, 6, 7}),
           frozenset({8, 2, 3, 5}),
           frozenset({8, 1, 3, 6}),
           frozenset({2, 4, 5, 7})})
frozenset({frozenset({8, 1, 4, 5}),
           frozenset({3, 4, 5, 6}),
           frozenset({8, 1, 2, 7}),
           frozenset({2, 3, 6, 7}),
           frozenset({1, 4, 6, 7}),
           frozenset({8, 2, 3, 5})})
frozenset({frozenset({3, 4, 5, 6}),
           frozenset({8, 1, 2, 7}),
           frozenset({1, 4, 6, 7}),
           frozenset({8, 2, 3, 5}),
           frozenset({8, 1, 3, 6}),
           frozenset({2, 4, 5, 7})})

In [ ]: