I first saw this puzzle in CSIRO’s Double Helix here.
The problem we are solving is described as:
Given a triangle with circle on each point and on each side:
I first saw this puzzle in CSIRO’s Double Helix here.
The Magic Triangle problem we are solving is described as:
You are given a triangle with circle on each point and on each side:
Then, using the numbers from 1 to 6, arrange them in a triangle with three numbers on each side. Swap them around until the sides all add up to the same number.
Finally, sum each side to 10.
Method
Let’s label the triangle: starting from any vertex label the nodes:
The method to solve this problem is broken into the following steps:
get all permutations of numbers 1 to 6 as a, b, c, d, e, f
filter permutation to satisfy conditions:
a + b + c == c + d + e == e + f + a
and final condition:
a + b +c == 10
Using Haskell
All permutations of numbers 1 to 6:
This will give 6! = 720
permutations.
Filter on sides summing up to the same value:
[
[(a,b,c), (c,d,e), (e,f,a)] | [a,b,c,d,e,f] <- permutations [1..6],
a+b+c == c+d+e &&
c+d+e == e+f+a
]
Which gives all solutions where the sides are equal sums:
[(3,2,5),(5,4,1),(1,6,3)]
[(2,4,3),(3,5,1),(1,6,2)]
[(1,5,3),(3,4,2),(2,6,1)]
[(1,4,5),(5,2,3),(3,6,1)]
[(5,3,4),(4,2,6),(6,1,5)]
[(6,2,4),(4,3,5),(5,1,6)]
[(4,5,2),(2,3,6),(6,1,4)]
[(6,3,2),(2,5,4),(4,1,6)]
[(2,3,6),(6,1,4),(4,5,2)]
[(4,1,6),(6,3,2),(2,5,4)]
[(3,4,2),(2,6,1),(1,5,3)]
[(1,6,2),(2,4,3),(3,5,1)]
[(5,4,1),(1,6,3),(3,2,5)]
[(4,3,5),(5,1,6),(6,2,4)]
[(6,1,5),(5,3,4),(4,2,6)]
[(3,6,1),(1,4,5),(5,2,3)]
[(5,2,3),(3,6,1),(1,4,5)]
[(2,6,1),(1,5,3),(3,4,2)]
[(3,5,1),(1,6,2),(2,4,3)]
[(1,6,3),(3,2,5),(5,4,1)]
[(5,1,6),(6,2,4),(4,3,5)]
[(2,5,4),(4,1,6),(6,3,2)]
[(6,1,4),(4,5,2),(2,3,6)]
[(4,2,6),(6,1,5),(5,3,4)]
Filter on sides summing to 10:
[
[(a,b,c), (c,d,e), (e,f,a)] | [a,b,c,d,e,f] <- permutations [1..6],
a+b+c == 10 &&
c+d+e == 10 &&
e+f+a == 10
]
Which gives our final list of solutions:
[(3,2,5),(5,4,1),(1,6,3)]
[(1,4,5),(5,2,3),(3,6,1)]
[(5,4,1),(1,6,3),(3,2,5)]
[(3,6,1),(1,4,5),(5,2,3)]
[(5,2,3),(3,6,1),(1,4,5)]
[(1,6,3),(3,2,5),(5,4,1)]
Note here that the solutions aren’t unique: there are repetitions if you consider rotations or node reversals. Can we filter these out to get the only unique solution?
Try ordering:
[
[(a,b,c), (c,d,e), (e,f,a)] | [a,b,c,d,e,f] <- permutations [1..6],
a+b+c == 10 &&
c+d+e == 10 &&
e+f+a == 10 &&
a > c &&
c > e
]
The idea here is that as the nodes are unique, we can order them. This yields our final solution:
[(5,2,3),(3,6,1),(1,4,5)]
Using one other Haskell refinement we can write this as:
[
[(a,b,c), (c,d,e), (e,f,a)] | [a,b,c,d,e,f] <- permutations [1..6],
all (==10) [a+b+c, c+d+e, e+f+a] &&
a > c &&
c > e
]
Check your answer on CSIRO page here.
Using Python
Python now has list comprehensions just like many other programming languages, so the solution is much the same. Also, use the built-in permutations function from itertools:
import itertools
[
[(a,b,c),(c,d,e),(e,f,a)]
for a,b,c,d,e,f in list(itertools.permutations(range(1,7)))
if a+b+c == 10 and c+d+e == 10 and e+f+a == 10
and a > c and c > e
]
Which yields the same results as our previous solution in Haskell:
>>> [[(5, 2, 3), (3, 6, 1), (1, 4, 5)]]