All Euler problems
Project Euler

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...

Source sync Apr 19, 2026
Problem #0068
Level Level 03
Solved By 23,737
Languages C++, Python
Answer 6531031914842725
Length 527 words
modular_arithmeticcombinatoricsoptimization

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.

Problem illustration

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.

TotalSolution 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 illustration

Problem 68: Magic 5-gon Ring

Mathematical Analysis

Definition

A magic 5-gon ring is a graph consisting of 5 outer nodes o1,o2,o3,o4,o5o_1, o_2, o_3, o_4, o_5 and 5 inner nodes i1,i2,i3,i4,i5i_1, i_2, i_3, i_4, i_5 arranged as a pentagon, together with an assignment of the integers {1,2,,10}\{1, 2, \ldots, 10\} (each used exactly once) such that all five lines share a common sum SS:

ok+ik+i(kmod5)+1=S,k=1,2,3,4,5.o_k + i_k + i_{(k \bmod 5) + 1} = S, \quad k = 1, 2, 3, 4, 5.

The string representation is formed by reading the triples (ok,ik,i(kmod5)+1)(o_k, i_k, i_{(k \bmod 5)+1}) clockwise, starting from the triple with the smallest outer node, and concatenating all digits.

Theorem 1 (Line Sum Constraint)

The common line sum satisfies S=11+15k=15ikS = 11 + \frac{1}{5}\sum_{k=1}^{5} i_k. In particular, 5k=15ik5 \mid \sum_{k=1}^{5} i_k.

Proof. Summing all five line equations:

k=15ok+2k=15ik=5S.\sum_{k=1}^{5} o_k + 2\sum_{k=1}^{5} i_k = 5S.

Since each of 1,,101, \ldots, 10 is used exactly once, ok+ik=55\sum o_k + \sum i_k = 55, whence ok=55ik\sum o_k = 55 - \sum i_k. Substituting:

55ik+2ik=5S    55+ik=5S    S=11+ik5.55 - \sum i_k + 2\sum i_k = 5S \implies 55 + \sum i_k = 5S \implies S = 11 + \frac{\sum i_k}{5}.

Since SS must be a positive integer, 5k=15ik5 \mid \sum_{k=1}^{5} i_k. \square

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 oko_k appears in exactly one triple, contributing d(ok)d(o_k) digits, where d(x)d(x) denotes the number of decimal digits of xx. Each inner node iki_k appears in exactly two triples, contributing 2d(ik)2 \cdot d(i_k) digits. The total digit count is:

D=k=15d(ok)+2k=15d(ik).D = \sum_{k=1}^{5} d(o_k) + 2\sum_{k=1}^{5} d(i_k).

For the numbers 1,,91, \ldots, 9, d=1d = 1; for 10, d=2d = 2.

  • If 10 is outer: D=2+41+251=16D = 2 + 4 \cdot 1 + 2 \cdot 5 \cdot 1 = 16.
  • If 10 is inner: D=51+2(2+41)=5+12=17D = 5 \cdot 1 + 2(2 + 4 \cdot 1) = 5 + 12 = 17.

Hence D=16D = 16 if and only if 10 occupies an outer node. \square

Theorem 3 (Optimal Partition)

Among all valid 16-digit magic 5-gon rings, the maximum string is achieved with inner nodes {1,2,3,4,5}\{1, 2, 3, 4, 5\} and outer nodes {6,7,8,9,10}\{6, 7, 8, 9, 10\}, yielding line sum S=14S = 14.

Proof. By Theorem 2, 10 must be outer. The inner node set I{1,,9}I \subset \{1, \ldots, 9\} has I=5|I| = 5 and must satisfy 5I5 \mid \sum I. 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 I0(mod5)\sum I \equiv 0 \pmod{5} where I{1,,9}I \subset \{1,\ldots,9\}, I=5|I| = 5. The minimum possible sum of any 5-element subset is 1+2+3+4+5=151+2+3+4+5 = 15. The possible sums divisible by 5 are 15,20,25,30,3515, 20, 25, 30, 35 (where 35 = 5+6+7+8+95+6+7+8+9 is the maximum).

To maximize outer node values, we minimize the inner sum, choosing I={1,2,3,4,5}I = \{1,2,3,4,5\} with I=15\sum I = 15 and S=14S = 14.

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 {6,7,8,9,10}\{6,7,8,9,10\} are the outer values and outer nodes lead each triple, this partition places the largest possible digits in the most significant positions. \square

Lemma (Outer Node Determination)

Given the inner permutation (i1,i2,i3,i4,i5)(i_1, i_2, i_3, i_4, i_5) as a cyclic arrangement of {1,2,3,4,5}\{1,2,3,4,5\} and line sum S=14S = 14, each outer node is uniquely determined by ok=14iki(kmod5)+1o_k = 14 - i_k - i_{(k \bmod 5)+1}. The arrangement is valid if and only if (o1,,o5)(o_1, \ldots, o_5) is a permutation of {6,7,8,9,10}\{6,7,8,9,10\}.

Proof. Immediate from the line equation ok+ik+i(kmod5)+1=14o_k + i_k + i_{(k \bmod 5)+1} = 14. The outer values must be distinct elements of {6,7,8,9,10}\{6,7,8,9,10\} since each number is used exactly once. \square

Editorial

The analysis reduces the search to inner nodes {1,2,3,4,5}\{1,2,3,4,5\} and outer nodes {6,7,8,9,10}\{6,7,8,9,10\} with common line sum 1414. 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 {6,7,8,9,10}\{6,7,8,9,10\}. 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: O(5!)=O(1)O(5!) = O(1). Only 120 permutations to check.
  • Space: O(1)O(1).

Answer

6531031914842725\boxed{6531031914842725}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_068/solution.cpp
#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;
}