Magic 5-gon Ring
Consider a "magic" 5-gon ring, filled with the numbers 1 to 10. Working clockwise, and starting from the group of three with the numerically lowest external node, each solution can be described by...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Consider the following "magic" 3-gon ring, filled with the numbers 1 to 6, and each line adding to nine.

Working clockwise, and starting from the group of three with the numerically lowest external node (4,3,2 in this example), each solution can be described uniquely. For example, the above solution can be described by the set: 4,3,2; 6,2,1; 5,1,3.
It is possible to complete the ring with four different totals: 9, 10, 11, and 12. There are eight solutions in total.
| Total | Solution Set |
| $9$ | $4,2,3$; $5,3,1$; $6,1,2$ |
| $9$ | $4,3,2$; $6,2,1$; $5,1,3$ |
| $10$ | $2,3,5$; $4,5,1$; $6,1,3$ |
| $10$ | $2,5,3$; $6,3,1$; $4,1,5$ |
| $11$ | $1,4,6$; $3,6,2$; $5,2,4$ |
| $11$ | $1,6,4$; $5,4,2$; $3,2,6$ |
| $12$ | $1,5,6$; $2,6,4$; $3,4,5$ |
| $12$ | $1,6,5$; $3,5,4$; $2,4,6$ |
By concatenating each group it is possible to form 9-digit strings; the maximum string for a 3-gon ring is 432621513.
Using the numbers 1 to 10, and depending on arrangements, it is possible to form 16- and 17-digit strings. What is the maximum 16-digit string for a "magic" 5-gon ring?

Problem 68: Magic 5-gon Ring
Mathematical Analysis
Definition
A magic 5-gon ring is a graph consisting of 5 outer nodes and 5 inner nodes arranged as a pentagon, together with an assignment of the integers (each used exactly once) such that all five lines share a common sum :
The string representation is formed by reading the triples clockwise, starting from the triple with the smallest outer node, and concatenating all digits.
Theorem 1 (Line Sum Constraint)
The common line sum satisfies . In particular, .
Proof. Summing all five line equations:
Since each of is used exactly once, , whence . Substituting:
Since must be a positive integer, .
Theorem 2 (16-Digit Necessity)
The string representation has exactly 16 digits if and only if the number 10 is assigned to an outer node.
Proof. Each outer node appears in exactly one triple, contributing digits, where denotes the number of decimal digits of . Each inner node appears in exactly two triples, contributing digits. The total digit count is:
For the numbers , ; for 10, .
- If 10 is outer: .
- If 10 is inner: .
Hence if and only if 10 occupies an outer node.
Theorem 3 (Optimal Partition)
Among all valid 16-digit magic 5-gon rings, the maximum string is achieved with inner nodes and outer nodes , yielding line sum .
Proof. By Theorem 2, 10 must be outer. The inner node set has and must satisfy . We seek to maximize the string, which favors large values in the outer positions (since outer values appear in the leading positions of each triple).
We enumerate the feasible inner sums. We need where , . The minimum possible sum of any 5-element subset is . The possible sums divisible by 5 are (where 35 = is the maximum).
To maximize outer node values, we minimize the inner sum, choosing with and .
It remains to verify that a valid assignment exists for this partition and that no other partition yields a lexicographically larger 16-digit string. Since are the outer values and outer nodes lead each triple, this partition places the largest possible digits in the most significant positions.
Lemma (Outer Node Determination)
Given the inner permutation as a cyclic arrangement of and line sum , each outer node is uniquely determined by . The arrangement is valid if and only if is a permutation of .
Proof. Immediate from the line equation . The outer values must be distinct elements of since each number is used exactly once.
Editorial
The analysis reduces the search to inner nodes and outer nodes with common line sum . We therefore enumerate the cyclic order of the inner nodes, derive the outer nodes from the line-sum constraint, and discard any arrangement whose derived outer values are not exactly . Every valid ring is normalized by starting at the smallest outer node, then read clockwise to form its 16-digit string; the largest such string is retained.
Pseudocode
Start with no best string.
For each cyclic arrangement of the inner nodes 1 through 5:
derive the five outer nodes from the common-sum equation
if the derived outer values are not exactly 6 through 10, discard the arrangement
locate the smallest outer node
from that position, read the five triples clockwise and concatenate them
if the resulting 16-digit string is larger than the current best:
replace the current best string
Return the largest string found.
Complexity
- Time: . Only 120 permutations to check.
- Space: .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main() {
// Magic 5-gon ring: inner = {1,2,3,4,5}, outer = {6,7,8,9,10}, S = 14.
// Enumerate permutations of inner ring; outer[k] = 14 - inner[k] - inner[(k+1)%5].
// Valid iff outer is a permutation of {6,7,8,9,10}.
vector<int> inner = {1, 2, 3, 4, 5};
string max_str = "";
do {
vector<int> outer(5);
bool valid = true;
set<int> outer_set;
for (int k = 0; k < 5; k++) {
outer[k] = 14 - inner[k] - inner[(k + 1) % 5];
if (outer[k] < 6 || outer[k] > 10) { valid = false; break; }
outer_set.insert(outer[k]);
}
if (!valid || outer_set.size() != 5) continue;
// Start from the line with the smallest outer node
int start = 0;
for (int k = 1; k < 5; k++) {
if (outer[k] < outer[start]) start = k;
}
string s = "";
for (int k = 0; k < 5; k++) {
int idx = (start + k) % 5;
s += to_string(outer[idx]);
s += to_string(inner[idx]);
s += to_string(inner[(idx + 1) % 5]);
}
if (s.length() == 16 && s > max_str) {
max_str = s;
}
} while (next_permutation(inner.begin(), inner.end()));
cout << max_str << endl;
return 0;
}
"""
Project Euler Problem 68: Magic 5-gon Ring
Find the maximum 16-digit string for a magic 5-gon ring using numbers 1-10.
By Theorem 2, 10 must be outer for a 16-digit string.
By Theorem 3, inner = {1,2,3,4,5}, outer = {6,7,8,9,10}, S = 14.
"""
from itertools import permutations
def main():
max_str = ""
for inner in permutations([1, 2, 3, 4, 5]):
outer = []
valid = True
for k in range(5):
o = 14 - inner[k] - inner[(k + 1) % 5]
if o < 6 or o > 10:
valid = False
break
outer.append(o)
if not valid or sorted(outer) != [6, 7, 8, 9, 10]:
continue
start = outer.index(min(outer))
s = ""
for k in range(5):
idx = (start + k) % 5
s += str(outer[idx])
s += str(inner[idx])
s += str(inner[(idx + 1) % 5])
if len(s) == 16 and s > max_str:
max_str = s
print(max_str)
if __name__ == "__main__":
main()