All Euler problems
Project Euler

Pivotal Square Sums

A positive integer k is called a square-pivot if there exist integers m > 0 and n >= k such that the sum of (m+1) consecutive squares ending at k equals the sum of m consecutive squares starting at...

Source sync Apr 19, 2026
Problem #0261
Level Level 17
Solved By 830
Languages C++, Python
Answer 238890850232021
Length 405 words
modular_arithmeticsequencenumber_theory

Problem Statement

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

Let us call a positive integer $k$ a square-pivot, if there is a pair of integers $m > 0$ and $n \ge k$, such that the sum of the $(m+1)$ consecutive squares up to $k$ equals the sum of the $m$ consecutive squares from $(n+1)$ on: $$(k - m)^2 + \cdots + k^2 = (n + 1)^2 + \cdots + (n + m)^2.$$ Some small square-pivots are

  • $\textcolor{red}{4}$: $3^2 + \textcolor{red}{4}^2 = 5^2$

  • $\textcolor{red}{21}$: $20^2 + \textcolor{red}{21}^2 = 29^2$

  • $\textcolor{red}{24}$: $21^2 + 22^2 + 23^2 + \textcolor{red}{24}^2 = 25^2 + 26^2 + 27^2$

  • $\textcolor{red}{110}$: $108^2 + 109^2 + \textcolor{red}{110}^2 = 133^2 + 134^2$

Find the sum of all distinct square-pivots $\le 10^{10}$.

Problem 261: Pivotal Square Sums

Mathematical Development

Notation

Denote the sum-of-squares function S(t)=i=1ti2=t(t+1)(2t+1)6S(t) = \sum_{i=1}^{t} i^2 = \tfrac{t(t+1)(2t+1)}{6}.

Theorem 1 (Reduction to generalized Pell equation). If kk is a square-pivot with parameters (m,n)(m, n), then writing m=sp2m = s p^2 with ss squarefree and m+1=gr2m + 1 = g r^2 with gg squarefree, there exist non-negative integers q,uq, u satisfying

gu2sq2=1.g u^2 - s q^2 = 1.

Equivalently, setting D=sgD = sg, X=guX = gu, Y=qY = q, one obtains the generalized Pell equation X2DY2=gX^2 - D Y^2 = g.

Proof. The pivot condition is S(k)S(km1)=S(n+m)S(n)S(k) - S(k - m - 1) = S(n + m) - S(n). Expanding both sides using the closed form of SS and introducing the substitutions x=2kmx = 2k - m and t=2n+m+1t = 2n + m + 1, algebraic simplification yields:

x2(m+1)t2m=m(m+1).x^2(m+1) - t^2 m = m(m+1).

Dividing by m(m+1)m(m+1), we obtain x2mt2m+1=1\frac{x^2}{m} - \frac{t^2}{m+1} = 1, which factors as

x2=ma,t2=(m+1)(a+1)x^2 = m \cdot a, \qquad t^2 = (m+1)(a+1)

for some non-negative integer aa (where a=x2ma = \frac{x^2}{m}). With the squarefree decompositions m=sp2m = s p^2 and m+1=gr2m + 1 = g r^2, we deduce x=spqx = s p q (so that a=sq2a = s q^2) and t=grut = g r u (so that a+1=gu2a + 1 = g u^2). Subtracting:

gu2sq2=(a+1)a=1.g u^2 - s q^2 = (a + 1) - a = 1.

Setting X=guX = gu and Y=qY = q gives X2DY2=g2u2sgq2=g(gu2sq2)=gX^2 - D Y^2 = g^2 u^2 - sg q^2 = g(gu^2 - sq^2) = g. \square

Lemma 1 (Base solution). The pair (X0,Y0)=(gr,p)(X_0, Y_0) = (gr, p) satisfies X02DY02=gX_0^2 - D Y_0^2 = g.

Proof. Direct computation: X02DY02=g2r2sgp2=g(gr2sp2)=g((m+1)m)=gX_0^2 - D Y_0^2 = g^2 r^2 - sg \cdot p^2 = g(gr^2 - sp^2) = g\bigl((m+1) - m\bigr) = g. \square

Lemma 2 (Pell recurrence). Let (x1,y1)(x_1, y_1) be the fundamental solution to X2DY2=1X^2 - DY^2 = 1 (obtained via the continued-fraction expansion of D\sqrt{D}). Then all solutions to X2DY2=gX^2 - DY^2 = g in the orbit of (X0,Y0)(X_0, Y_0) are generated by the recurrence

(Xj+1,Yj+1)=(Xjx1+DYjy1,  Xjy1+Yjx1).(X_{j+1}, Y_{j+1}) = (X_j \, x_1 + D \, Y_j \, y_1, \; X_j \, y_1 + Y_j \, x_1).

Proof. By the Brahmagupta—Fibonacci identity, if (Xj,Yj)(X_j, Y_j) satisfies Xj2DYj2=gX_j^2 - D Y_j^2 = g and (x1,y1)(x_1, y_1) satisfies x12Dy12=1x_1^2 - D y_1^2 = 1, then

(Xjx1+DYjy1)2D(Xjy1+Yjx1)2=(Xj2DYj2)(x12Dy12)=g1=g.(X_j x_1 + D Y_j y_1)^2 - D(X_j y_1 + Y_j x_1)^2 = (X_j^2 - D Y_j^2)(x_1^2 - D y_1^2) = g \cdot 1 = g.

Moreover, the new solution is strictly larger than (Xj,Yj)(X_j, Y_j) since x11x_1 \ge 1 and y11y_1 \ge 1. The orbit exhausts all positive solutions in the given class (a standard result in the theory of Pell equations; see, e.g., Jacobson and Williams, Solving the Pell Equation, CMS, 2009). \square

Lemma 3 (Recovering kk and nn). Given a Pell solution (X,Y)(X, Y) with Y=qY = q, the pivot is

k=sp(p+q)2,n=grum12k = \frac{s p (p + q)}{2}, \qquad n = \frac{g r u - m - 1}{2}

where u=X/gu = X / g. The pivot is valid if and only if kk is a positive integer and nkn \ge k.

Proof. From x=spqx = spq and x=2kmx = 2k - m, we get k=spq+m2=sp(q+p)2k = \frac{spq + m}{2} = \frac{sp(q + p)}{2} (using m=sp2m = sp^2). Similarly, t=grut = gru and t=2n+m+1t = 2n + m + 1 gives n=grum12n = \frac{gru - m - 1}{2}. \square

Lemma 4 (Bound on mm). For a valid pivot k1010k \le 10^{10}, we have m70710m \le 70710.

Proof. The constraint nkn \ge k combined with the expressions for kk and nn forces k2m(m+1)k \ge 2m(m+1). Indeed, the smallest valid qq satisfies qp+1q \ge p + 1, which after substitution yields ksp2(p+p+1)/2>mk \ge sp^2(p + p + 1)/2 > m, and the refined inequality k2m(m+1)k \ge 2m(m+1) follows from the requirement nkn \ge k. Then 2m(m+1)10102m(m+1) \le 10^{10} implies m2<5×109m^2 < 5 \times 10^9, so m5×109=70710m \le \lfloor\sqrt{5 \times 10^9}\rfloor = 70710. \square

Editorial

Reduction: the pivot equation reduces to a generalized Pell equation X^2 - DY^2 = g where D = sg, with m = sp^2 (s squarefree) and m+1 = gr^2 (g squarefree). Iterating Pell solutions recovers k values. We loop.

Pseudocode

pivots = empty set
For m from 1 to 70710:
    compute s, p such that m = s * p^2 (s squarefree)
    compute g, r such that m + 1 = g * r^2 (g squarefree)
    D = s * g
    if D is a perfect square: skip // Pell equation X^2 - D*Y^2 = 1 has no solution
    (x1, y1) = fundamental_pell_solution(D) // via continued fractions
    (X, Y) = (g * r, p) // base solution from Lemma 1
    loop:
        q = Y
        k = s * p * (p + q) / 2
        If k > 10^10 then stop this loop
        If k is a positive integer and k >= 2 * m * (m + 1) then
            u = X / g
            n_val = (g * r * u - m - 1) / 2
            If n_val >= k then
                pivots.add(k)
        (X, Y) = (X * x1 + D * Y * y1, X * y1 + Y * x1) // Lemma 2
Return sum(pivots)

Complexity Analysis

Time. The outer loop runs M=70710M = 70710 iterations. For each mm:

  • The squarefree decomposition uses a smallest-prime-factor sieve, computed once in O(MloglogM)O(M \log \log M).
  • Finding the fundamental Pell solution via continued fractions takes O(D)O(\sqrt{D}) arithmetic operations, where D=sgm(m+1)D = sg \le m(m+1). With caching, each distinct DD is solved only once.
  • The inner Pell iteration produces at most O(log(1010))O(\log(10^{10})) solutions per orbit, since XjX_j grows exponentially.

Total: O(MloglogM+M(Dmax+logN))O(M \log \log M + M \cdot (\sqrt{D_{\max}} + \log N)) where N=1010N = 10^{10} and Dmax=O(M2)D_{\max} = O(M^2).

Space. O(M)O(M) for the sieve and Pell cache; O(pivots)O(|\text{pivots}|) for the result set.

Answer

238890850232021\boxed{238890850232021}

Code

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

C++ project_euler/problem_261/solution.cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128 lll;

/*
 * Problem 261: Pivotal Square Sums
 *
 * Reduction to generalized Pell equation X^2 - D*Y^2 = g,
 * where m = s*p^2, m+1 = g*r^2, D = s*g.
 * Iterate Pell solutions to recover pivots k = s*p*(p+q)/2.
 */

const ll LIMIT = 10000000000LL;
int spf_arr[100002];

void build_spf(int n) {
    for (int i = 0; i <= n; i++) spf_arr[i] = i;
    for (int i = 2; (ll)i * i <= n; i++)
        if (spf_arr[i] == i)
            for (int j = i * i; j <= n; j += i)
                if (spf_arr[j] == j) spf_arr[j] = i;
}

pair<int, int> sqfree_sqrt(int n) {
    int sf = 1, sq = 1;
    while (n > 1) {
        int p = spf_arr[n], e = 0;
        while (n % p == 0) { n /= p; e++; }
        if (e & 1) sf *= p;
        for (int i = 0; i < e / 2; i++) sq *= p;
    }
    return {sf, sq};
}

unordered_map<ll, pair<ll, ll>> pcache;

pair<ll, ll> pell_fund(ll D) {
    auto it = pcache.find(D);
    if (it != pcache.end()) return it->second;
    ll a0 = (ll)sqrtl((long double)D);
    while (a0 * a0 > D) a0--;
    while ((a0 + 1) * (a0 + 1) <= D) a0++;
    ll m = 0, d = 1, a = a0;
    ll n1 = 1, n0 = a0, d1 = 0, d0 = 1;
    for (int i = 0; i < 100000000; i++) {
        m = d * a - m;
        d = (D - m * m) / d;
        if (d == 0) break;
        a = (a0 + m) / d;
        ll n2 = n1, d2 = d1;
        n1 = n0; d1 = d0;
        n0 = a * n1 + n2; d0 = a * d1 + d2;
        if ((lll)n0 * n0 - (lll)D * d0 * d0 == 1) {
            pcache[D] = {n0, d0};
            return {n0, d0};
        }
        if (n0 > 4000000000000000000LL) break;
    }
    pcache[D] = {0, 0};
    return {0, 0};
}

int main() {
    int mmax = 70710;
    while (2LL * (mmax + 1) * ((ll)(mmax + 1) + 1) <= LIMIT) mmax++;

    build_spf(mmax + 2);
    unordered_set<ll> pivots;
    pivots.reserve(1 << 17);

    for (int m = 1; m <= mmax; m++) {
        auto [s, p] = sqfree_sqrt(m);
        auto [g, r] = sqfree_sqrt(m + 1);
        ll D = (ll)s * g;
        auto [x1, y1] = pell_fund(D);
        if (x1 == 0) continue;

        ll x = (ll)g * r, y = p;
        for (int iter = 0; iter < 100; iter++) {
            ll q = y;
            lll num = (lll)s * p * ((ll)p + q);
            if (num / 2 > LIMIT && q > p) break;
            if (num % 2 == 0) {
                ll k = (ll)(num / 2);
                if (k <= LIMIT && k >= 2LL * m * (m + 1)) {
                    ll u = x / g;
                    lll tv = (lll)g * r * u;
                    lll rem = tv - m - 1;
                    if (rem >= 0 && rem % 2 == 0) {
                        ll n_val = (ll)(rem / 2);
                        if (n_val >= k)
                            pivots.insert(k);
                    }
                }
            }
            lll xn = (lll)x * x1 + (lll)y * y1 * D;
            lll yn = (lll)x * y1 + (lll)y * x1;
            if (xn > (lll)4e18 || yn > (lll)4e18) break;
            x = (ll)xn;
            y = (ll)yn;
        }
    }

    ll ans = 0;
    for (ll k : pivots) ans += k;
    cout << ans << endl;
    return 0;
}