All Euler problems
Project Euler

Investigating Progressive Numbers

A positive integer n is called progressive if, when dividing n by some positive integer d, the quotient q and remainder r satisfy: q, d, r form a geometric sequence (in that order) with q > d > r >...

Source sync Apr 19, 2026
Problem #0141
Level Level 07
Solved By 4,689
Languages C++, Python
Answer 878454337159
Length 355 words
number_theorysequencebrute_force

Problem Statement

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

A positive integer, \(n\), is divided by \(d\) and the quotient and remainder are \(q\) and \(r\) respectively. In addition \(d\), \(q\), and \(r\) are consecutive positive integer terms in a geometric sequence, but not necessarily in that order.

For example, \(58\) divided by \(6\) has quotient \(9\) and remainder \(4\). It can also be seen that \(4, 6, 9\) are consecutive terms in a geometric sequence (common ratio \(3/2\)).

We will call such numbers, \(n\), progressive.

Some progressive numbers, such as \(9\) and \(10404 = 102^2\), happen to also be perfect squares.

The sum of all progressive perfect squares below one hundred thousand is \(124657\).

Find the sum of all progressive perfect squares below one trillion (\(10^{12}\)).

Problem 141: Investigating Progressive Numbers

Mathematical Development

Definition 1. We call nn progressive if there exist integers q,d,rq, d, r with n=qd+rn = qd + r, 0r<d0 \leq r < d, q>d>r0q > d > r \geq 0, and d2=qrd^2 = qr (the geometric sequence condition).

Theorem 1 (Exclusion of r=0r = 0). No progressive number has r=0r = 0.

Proof. If r=0r = 0, then d2=qr=0d^2 = qr = 0, so d=0d = 0. But d>r0d > r \geq 0 requires d1d \geq 1, a contradiction. \qed\qed

Theorem 2 (Parametrization). Every progressive number nn with r1r \geq 1 admits a unique representation

n=a3bc2+b2cn = a^3 b c^2 + b^2 c

where a,b,ca, b, c are positive integers satisfying a>ba > b, gcd(a,b)=1\gcd(a, b) = 1, and c1c \geq 1. The corresponding division parameters are q=a2cq = a^2 c, d=abcd = abc, r=b2cr = b^2 c.

Proof. Suppose q>d>r1q > d > r \geq 1 with d2=qrd^2 = qr and n=qd+rn = qd + r.

Step 1 (Reduction to coprime form). Let g=gcd(d,r)g = \gcd(d, r) and write d=gαd = g\alpha, r=gβr = g\beta where gcd(α,β)=1\gcd(\alpha, \beta) = 1. From d2=qrd^2 = qr:

q=d2r=g2α2gβ=gα2β.q = \frac{d^2}{r} = \frac{g^2 \alpha^2}{g\beta} = \frac{g\alpha^2}{\beta}.

Since gcd(α,β)=1\gcd(\alpha, \beta) = 1 and qq is a positive integer, we require βg\beta \mid g. Write g=βcg = \beta c for some positive integer cc.

Step 2 (Explicit formulas). Substituting g=βcg = \beta c:

r=gβ=β2c,d=gα=αβc,q=gα2β=α2c.r = g\beta = \beta^2 c, \qquad d = g\alpha = \alpha\beta c, \qquad q = \frac{g\alpha^2}{\beta} = \alpha^2 c.

Step 3 (Relabeling). Setting a=αa = \alpha and b=βb = \beta yields q=a2cq = a^2 c, d=abcd = abc, r=b2cr = b^2 c with gcd(a,b)=1\gcd(a,b) = 1.

Step 4 (Ordering verification). We verify q>d>rq > d > r:

  • q>d    a2c>abc    a>bq > d \iff a^2 c > abc \iff a > b, which holds by construction since q>dq > d in the original problem.
  • d>r    abc>b2c    a>bd > r \iff abc > b^2 c \iff a > b, also satisfied.

Step 5 (Formula for nn).

n=qd+r=a2cabc+b2c=a3bc2+b2c=bc(a3c+b).n = qd + r = a^2 c \cdot abc + b^2 c = a^3 bc^2 + b^2 c = bc(a^3 c + b).

Step 6 (Uniqueness). Given (q,d,r)(q, d, r), the ratio a/b=d/ra/b = d/r in lowest terms is uniquely determined, and c=r/b2c = r/b^2 follows. Conversely, distinct (a,b,c)(a,b,c) triples yield distinct (q,d,r)(q,d,r) triples since q=a2cq = a^2 c, d=abcd = abc, r=b2cr = b^2 c are injective in (a,b,c)(a,b,c). \qed\qed

Lemma 1 (Search bounds). The following bounds hold:

  1. aN1/3a \leq \lfloor N^{1/3} \rfloor where N=1012N = 10^{12}.
  2. For fixed (a,b)(a,b): cN/(a3b)c \leq \lfloor \sqrt{N / (a^3 b)} \rfloor.

Proof. Since n=a3bc2+b2ca3bc2a311=a3n = a^3 bc^2 + b^2 c \geq a^3 bc^2 \geq a^3 \cdot 1 \cdot 1 = a^3, the constraint n<Nn < N forces a<N1/3a < N^{1/3}. For fixed (a,b)(a,b) with c1c \geq 1, na3bc2n \geq a^3 bc^2 gives cN/(a3b)c \leq \sqrt{N/(a^3 b)}. \qed\qed

Remark. Different (a,b,c)(a,b,c) triples may produce the same value of nn (though different (q,d,r)(q,d,r) triples). Hence we collect progressive numbers in a set to avoid double-counting before checking for perfect squares.

Editorial

n is progressive if n = qd + r where q, d, r form a geometric sequence with q > d > r >= 0. Parametrization: n = a^3bc^2 + b^2c with gcd(a,b)=1, a>b>=1, c>=1. We enumerate the admissible parameter triples, test each generated value for being a square, and sum the distinct progressive squares that satisfy the bound.

Pseudocode

INPUT:  N = 10^12
OUTPUT: Sum of all progressive perfect squares below N
S <- empty set
FOR a = 2, 3, ..., floor(N^{1/3}):
FOR b = 1, 2, ..., a - 1:
IF gcd(a, b) != 1 THEN CONTINUE
A <- a^3 * b
IF A >= N THEN BREAK
FOR c = 1, 2, ...:
n <- A * c^2 + b^2 * c
IF n >= N THEN BREAK
s <- floor(sqrt(n))
IF s^2 = n THEN S <- S union {n}
RETURN sum of all elements in S

Complexity Analysis

Time. The outer loop over aa runs O(N1/3)O(N^{1/3}) iterations. For each aa, the loop over bb runs at most aa iterations, reduced by the coprimality filter to O(a6/π2)O(a \cdot 6/\pi^2) on average. The innermost loop over cc runs O(N/(a3b))O(\sqrt{N/(a^3 b)}) iterations. The total work is bounded by:

W=a=2N1/3b=1gcd(a,b)=1a1O ⁣(Na3b)=O ⁣(Na=2N1/31a3/2b=1a11b).W = \sum_{a=2}^{N^{1/3}} \sum_{\substack{b=1 \\ \gcd(a,b)=1}}^{a-1} O\!\left(\sqrt{\frac{N}{a^3 b}}\right) = O\!\left(\sqrt{N} \sum_{a=2}^{N^{1/3}} \frac{1}{a^{3/2}} \sum_{b=1}^{a-1} \frac{1}{\sqrt{b}}\right).

The inner sum is O(a)O(\sqrt{a}) and the resulting series converges, giving W=O(N)W = O(\sqrt{N}).

Space. O(K)O(K) where KK is the number of progressive perfect squares found (empirically very small).

Answer

878454337159\boxed{878454337159}

Code

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

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

int main() {
    // Find sum of all progressive perfect squares below 10^12
    // n = a^3 * b * c^2 + b^2 * c, with gcd(a,b)=1, a>b>=1, c>=1
    // Check if n is a perfect square

    long long LIMIT = 1000000000000LL; // 10^12
    set<long long> squares;

    for (long long a = 2; a * a * a < LIMIT; a++) {
        for (long long b = 1; b < a; b++) {
            if (__gcd(a, b) != 1) continue;
            // n = a^3*b*c^2 + b^2*c
            // For c=1: n = a^3*b + b^2
            long long a3b = a * a * a * b;
            if (a3b >= LIMIT) break;
            for (long long c = 1; ; c++) {
                long long n = a3b * c * c + b * b * c;
                if (n >= LIMIT) break;
                long long s = (long long)sqrt((double)n);
                // Check around s due to floating point
                for (long long t = max(1LL, s - 2); t <= s + 2; t++) {
                    if (t * t == n) {
                        squares.insert(n);
                        break;
                    }
                }
            }
        }
    }

    long long ans = 0;
    for (long long x : squares) ans += x;
    cout << ans << endl;
    return 0;
}