Problem 68: Magic 5-gon Ring
View on Project EulerProject Euler Problem 68 Solution
EulerSolve provides an optimized solution for Project Euler Problem 68, Magic 5-gon Ring, with C++, Python, Java, and a step-by-step mathematical explanation.
Problem Summary A magic 5-gon ring uses the numbers \(1\) through \(10\) exactly once. Five numbers sit on the inner pentagon and five on the outer pentagon. Each line has the form \((o_k,i_k,i_{k+1})\), where the outer node \(o_k\) is joined to two consecutive inner nodes \(i_k\) and \(i_{k+1}\), and all five lines must have the same sum. The ring is encoded by concatenating the five line triples, starting from the line whose outer node is numerically smallest and then continuing clockwise. The goal is not merely to find a valid magic ring, but to find the largest possible 16-digit string under that canonical reading rule. Mathematical Approach Write the inner cycle as \(i_1,i_2,i_3,i_4,i_5\) and the outer nodes as \(o_1,o_2,o_3,o_4,o_5\). Then the magic condition is $o_1+i_1+i_2=o_2+i_2+i_3=o_3+i_3+i_4=o_4+i_4+i_5=o_5+i_5+i_1=S.$ The whole problem is controlled by these five equations, by one global sum identity, and by the special role of the number \(10\) in the final string length. Deriving the outer ring from an ordered inner cycle Once an order for the inner cycle is fixed, choosing one outer node already determines the common line sum....
Detailed mathematical approach
Problem Summary
A magic 5-gon ring uses the numbers \(1\) through \(10\) exactly once. Five numbers sit on the inner pentagon and five on the outer pentagon. Each line has the form \((o_k,i_k,i_{k+1})\), where the outer node \(o_k\) is joined to two consecutive inner nodes \(i_k\) and \(i_{k+1}\), and all five lines must have the same sum.
The ring is encoded by concatenating the five line triples, starting from the line whose outer node is numerically smallest and then continuing clockwise. The goal is not merely to find a valid magic ring, but to find the largest possible 16-digit string under that canonical reading rule.
Mathematical Approach
Write the inner cycle as \(i_1,i_2,i_3,i_4,i_5\) and the outer nodes as \(o_1,o_2,o_3,o_4,o_5\). Then the magic condition is
$o_1+i_1+i_2=o_2+i_2+i_3=o_3+i_3+i_4=o_4+i_4+i_5=o_5+i_5+i_1=S.$
The whole problem is controlled by these five equations, by one global sum identity, and by the special role of the number \(10\) in the final string length.
Deriving the outer ring from an ordered inner cycle
Once an order for the inner cycle is fixed, choosing one outer node already determines the common line sum. If we start with \(o_1\), then
$S=o_1+i_1+i_2.$
Every other outer node is forced:
$o_2=S-i_2-i_3,\quad o_3=S-i_3-i_4,\quad o_4=S-i_4-i_5,\quad o_5=S-i_5-i_1.$
So a candidate ring is valid exactly when these five derived outer values are the five numbers not used on the inner ring, with no repetition and no missing value. This is the central structural simplification: the search never needs to permute all ten positions independently.
The total-sum invariant
Each outer node appears in exactly one line, while each inner node appears in exactly two lines. Summing all five line sums therefore gives
$5S=(o_1+o_2+o_3+o_4+o_5)+2(i_1+i_2+i_3+i_4+i_5).$
Because the ten numbers are \(1,2,\dots,10\), their total is \(55\). If \(I=i_1+i_2+i_3+i_4+i_5\), then the outer sum is \(55-I\), and hence
$5S=(55-I)+2I=55+I.$
Therefore \(I\) must be divisible by \(5\), and once the inner set is known, the common line sum is fixed by
$S=\frac{55+I}{5}.$
This does not by itself solve the arrangement problem, but it sharply limits which inner sets can possibly work.
Why a 16-digit solution forces \(10\) onto the outer ring
The concatenated representation contains every outer number once and every inner number twice, because each inner node belongs to two adjacent lines. If every number were one digit long, the string would have \(15\) digits. The only extra digit comes from the number \(10\).
If \(10\) is on the outer ring, it appears once in the concatenation, so the total length becomes \(15+1=16\). If \(10\) is on the inner ring, it appears twice, so the total length becomes \(15+2=17\). Hence every valid 16-digit candidate must place \(10\) on the outer ring.
Canonical order and lexicographic pressure
The reading rule starts at the numerically smallest outer node. That choice removes rotational ambiguity: the same ring may be rotated in five ways geometrically, but only one rotation is allowed in the string representation.
It also matters for maximization. The very first digit block begins with the smallest outer node, so to maximize the full string we want that smallest outer value to be as large as possible. Since \(10\) must already be outside, the best possible outer set is \(\{6,7,8,9,10\}\), whose minimum is \(6\). That forces the inner set to be \(\{1,2,3,4,5\}\), and then
$S=\frac{55+(1+2+3+4+5)}{5}=\frac{70}{5}=14.$
The exhaustive search in the implementations confirms that the maximum 16-digit string does indeed come from this strongest candidate family.
Worked example: the optimal ring
Take the inner cycle in clockwise order as
$ (i_1,i_2,i_3,i_4,i_5)=(5,3,1,4,2). $
With \(S=14\), the outer nodes are forced to be
$o_1=14-5-3=6,\quad o_2=14-3-1=10,\quad o_3=14-1-4=9,\quad o_4=14-4-2=8,\quad o_5=14-2-5=7.$
So the five lines are
$ (6,5,3),\ (10,3,1),\ (9,1,4),\ (8,4,2),\ (7,2,5), $
and every one of them sums to \(14\). The canonical concatenation is therefore
$6531031914842725,$
which is exactly the maximal 16-digit string returned by the search.
How the Code Works
The C++, Python, and Java implementations use a direct enumeration, but they enumerate only the mathematically meaningful degrees of freedom. They split the ten numbers into an inner 5-set and its outer complement, try every ordering of the inner cycle, choose one candidate starting outer node, and then let the equal-sum equations derive the other four outer nodes automatically.
The implementations do not hard-code every theoretical pruning rule. For example, they do not explicitly pre-filter to “\(10\) must be outside” or “the inner sum must be divisible by \(5\).” Instead, they keep the code simple: generate a candidate, compute the implied outer ring, and reject it unless the derived outer multiset matches the unused numbers exactly.
After that, they enforce the canonical reading rule by requiring the chosen first outer node to be the smallest among the five outer nodes. This removes rotational duplicates. The five triples are then concatenated clockwise. If the resulting string has length \(16\) and is lexicographically larger than the current best, it replaces the previous best. Reversed orientations are still encountered through different inner orders, but that is harmless because only the maximum string survives.
Complexity Analysis
The implemented search checks all \(\binom{10}{5}=252\) choices for the inner set, all \(5!=120\) orderings of that set, and \(5\) choices for the starting outer node. That gives
$252 \cdot 120 \cdot 5 = 151{,}200$
candidate starts. Each candidate requires only constant-time arithmetic on five lines, a small membership test on five outer values, and the construction of a short string. For this fixed problem size, the search is effectively instantaneous.
Memory usage is \(O(1)\) apart from tiny temporary containers. The key reason the brute force is practical is that the equal-sum equations collapse the outer ring: the code never needs to permute all ten positions freely.
Footnotes and References
- Problem page: Project Euler 68 - Magic 5-gon Ring
- Permutations: Wikipedia - Permutation
- Combinations: Wikipedia - Combination
- Circular arrangements: Wikipedia - Circular permutation
- Lexicographic order: Wikipedia - Lexicographical order