All Euler problems
Project Euler

Swapping Counters

A horizontal row comprises 2n+1 squares. A set of n red counters occupies the leftmost n squares and a set of n blue counters occupies the rightmost n squares, with a single empty square in the mid...

Source sync Apr 19, 2026
Problem #0321
Level Level 10
Solved By 2,013
Languages C++, Python
Answer 2470433131948040
Length 418 words
sequencebrute_forcelinear_algebra

Problem Statement

This archive keeps the full statement, math, and original media on the page.

A horizontal row comprising of $2n + 1$ squares has $n$ red counters placed at one end and $n$ blue counters at the other end, being separated by a single empty square in the centre. For example, when $n = 3$.

Problem illustration

A counter can move from one square to the next (slide) or can jump over another counter (hop) as long as the square next to that counter is unoccupied.

Problem illustration

Let $M(n)$ represent the minimum number of moves/actions to completely reverse the positions of the coloured counters; that is, move all the red counters to the right and all the blue counters to the left.

It can be verified $M(3) = 15$, which also happens to be a triangle number.

If we create a sequence based on the values of $n$ for which $M(n)$ is a triangle number then the first five terms would be:

$1$, $3$, $10$, $22$, and $63$, and their sum would be $99$.

Find the sum of the first forty terms of this sequence.s

Problem 321: Swapping Counters

Solution

Reduction to a Pell-type equation

Proposition 1. The positive integer nn satisfies the requirement that n(n+2)n(n+2) is triangular if and only if there exists a positive integer kk such that

n(n+2)=k(k+1)2.n(n+2) = \frac{k(k+1)}{2}.

Theorem 1. The equation n(n+2)=k(k+1)2n(n+2) = \frac{k(k+1)}{2} with n,k1n,k \geq 1 is equivalent to the generalised Pell equation

x28y2=7x^2 - 8y^2 = -7

under the substitution x=2k+1x = 2k+1, y=n+1y = n+1.

Proof. Multiplying both sides of n(n+2)=k(k+1)2n(n+2) = \frac{k(k+1)}{2} by 8 yields

8n2+16n=4k2+4k.8n^2 + 16n = 4k^2 + 4k.

Completing the square on each side:

8(n+1)28=(2k+1)21.8(n+1)^2 - 8 = (2k+1)^2 - 1.

Setting x=2k+1x = 2k+1 and y=n+1y = n+1 gives x28y2=7x^2 - 8y^2 = -7. Since n1n \geq 1 forces y2y \geq 2 and k1k \geq 1 forces x3x \geq 3 (with xx odd), the correspondence is bijective: every admissible pair (n,k)(n,k) yields a solution (x,y)(x,y) with y2y \geq 2 and x3x \geq 3 odd, and conversely every such solution yields n=y11n = y - 1 \geq 1 and k=(x1)/21k = (x-1)/2 \geq 1. \square

Enumeration of solutions

Lemma 1 (Fundamental solutions). The equation x28y2=7x^2 - 8y^2 = -7 has exactly two classes of solutions, with fundamental representatives (x,y)=(1,1)(x,y) = (1,1) and (x,y)=(5,2)(x,y) = (5,2).

Proof. Checking y=1y = 1: x2=87=1x^2 = 8 - 7 = 1, so (1,1)(1,1) is a solution. Checking y=2y = 2: x2=327=25x^2 = 32 - 7 = 25, so (5,2)(5,2) is a solution. For y3y \geq 3 we have x2=8y2765x^2 = 8y^2 - 7 \geq 65, and one verifies that the next solutions in each class (generated by the recurrence below) have y=4y = 4 and y=7y = 7 respectively, confirming no intermediate solutions exist. \square

Theorem 2 (Recurrence for solutions). If (xi,yi)(x_i, y_i) is a solution of x28y2=7x^2 - 8y^2 = -7, then so is

xi+1=3xi+8yi,yi+1=xi+3yi.x_{i+1} = 3x_i + 8y_i, \qquad y_{i+1} = x_i + 3y_i.

Moreover, within each family the yy-values satisfy the linear recurrence yi+2=6yi+1yiy_{i+2} = 6y_{i+1} - y_i, and consequently the nn-values satisfy

ni+2=6ni+1ni+4.n_{i+2} = 6\,n_{i+1} - n_i + 4.

Proof. The fundamental solution of the associated Pell equation u28v2=1u^2 - 8v^2 = 1 is (u,v)=(3,1)(u,v) = (3,1). In the ring Z[8]\mathbb{Z}[\sqrt{8}], multiplication by the unit 3+83 + \sqrt{8} maps solutions of x28y2=7x^2 - 8y^2 = -7 to solutions, since the norm is multiplicative:

(xi28yi2)(32812)=(7)(1)=7.(x_i^2 - 8y_i^2)(3^2 - 8 \cdot 1^2) = (-7)(1) = -7.

Explicitly, (xi+yi8)(3+8)=(3xi+8yi)+(xi+3yi)8(x_i + y_i\sqrt{8})(3 + \sqrt{8}) = (3x_i + 8y_i) + (x_i + 3y_i)\sqrt{8}, giving the stated formulas.

For the second-order recurrence, applying the map twice yields yi+2=(xi+1+3yi+1)y_{i+2} = (x_{i+1} + 3y_{i+1}). Substituting xi+1=3xi+8yix_{i+1} = 3x_i + 8y_i and xi=yi+13yix_i = y_{i+1} - 3y_i (from yi+1=xi+3yiy_{i+1} = x_i + 3y_i), we obtain

yi+2=3(yi+13yi)+8yi+3yi+1=6yi+1yi.y_{i+2} = 3(y_{i+1} - 3y_i) + 8y_i + 3y_{i+1} = 6y_{i+1} - y_i.

Since ni=yi1n_i = y_i - 1, substituting gives (ni+2+1)=6(ni+1+1)(ni+1)(n_{i+2}+1) = 6(n_{i+1}+1) - (n_i+1), whence ni+2=6ni+1ni+4n_{i+2} = 6n_{i+1} - n_i + 4. \square

Two families of valid nn-values

Applying the recurrence ni+2=6ni+1ni+4n_{i+2} = 6n_{i+1} - n_i + 4:

  • Family A (from (x,y)=(1,1)(x,y)=(1,1), i.e., n0=0,n1=3n_0 = 0, n_1 = 3):   0,3,22,133,798,4785,\;0, 3, 22, 133, 798, 4785, \ldots
  • Family B (from (x,y)=(5,2)(x,y)=(5,2), i.e., n0=1,n1=10n_0 = 1, n_1 = 10):   1,10,55,322,1929,11566,\;1, 10, 55, 322, 1929, 11566, \ldots

Discarding n=0n = 0 from Family A and merging both families in sorted order, the first 40 positive values are collected and summed.

Editorial

We seek positive integers n such that M(n) = n(n+2) is a triangular number. The substitution x = 2k+1, y = n+1 transforms n(n+2) = k(k+1)/2 into the generalised Pell equation x^2 - 8y^2 = -7, whose solutions fall into two families, each satisfying n_{i+2} = 6 n_{i+1} - n_i + 4.

Pseudocode

    A = [0, 3]
    B = [1, 10]
    For i from 2 to 24:
        A.append(6 * A[i-1] - A[i-2] + 4)
        B.append(6 * B[i-1] - B[i-2] + 4)
    values = sorted(A[1:] + B)
    Return sum(values[:40])

Complexity

  • Time: O(N)O(N) where N=40N = 40. Each family requires N/2\lceil N/2 \rceil terms, each computed in O(1)O(1) arithmetic operations.
  • Space: O(N)O(N).

Answer

2470433131948040\boxed{2470433131948040}

Code

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

C++ project_euler/problem_321/solution.cpp
#include <bits/stdc++.h>
using namespace std;

/*
 * Problem 321: Swapping Counters
 *
 * n(n+2) = k(k+1)/2  <==>  x^2 - 8y^2 = -7  (x = 2k+1, y = n+1).
 * Two families of solutions with recurrence n_{i+2} = 6 n_{i+1} - n_i + 4.
 * Family A seeds: n = 0, 3.   Family B seeds: n = 1, 10.
 */

int main() {
    vector<long long> vals;

    // Family A (skip n = 0)
    long long a0 = 0, a1 = 3;
    vals.push_back(a1);
    for (int i = 2; i < 25; i++) {
        long long a2 = 6 * a1 - a0 + 4;
        vals.push_back(a2);
        a0 = a1;
        a1 = a2;
    }

    // Family B
    long long b0 = 1, b1 = 10;
    vals.push_back(b0);
    vals.push_back(b1);
    for (int i = 2; i < 25; i++) {
        long long b2 = 6 * b1 - b0 + 4;
        vals.push_back(b2);
        b0 = b1;
        b1 = b2;
    }

    sort(vals.begin(), vals.end());

    long long ans = 0;
    for (int i = 0; i < 40; i++)
        ans += vals[i];

    cout << ans << endl;
    return 0;
}