All Euler problems
Project Euler

Geometric Progression with Maximum Sum

A geometric progression (GP) consists of terms a, ar, ar^2,..., ar^(k-1) where a is the first term, r is the common ratio, and k is the number of terms. We require all terms to be distinct positive...

Source sync Apr 19, 2026
Problem #0542
Level Level 31
Solved By 273
Languages C++, Python
Answer 697586734240314852
Length 295 words
modular_arithmeticoptimizationnumber_theory

Problem Statement

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

Let \(S(k)\) be the sum of three or more distinct positive integers having the following properties:

  • No value exceeds \(k\).

  • The values form a geometric progression.

  • The sum is maximal.

We can easily verify that

  • \(S(4) = 4 + 2 + 1 = 7\)

  • \(S(10) = 9 + 6 + 4 = 19\)

  • \(S(12) = 12 + 6 + 3 + 21\)

  • \(S(1000) = 1000 + 900 + 810 + 729 = 3439\)

Let \(T(n) = \sum _{k=4}^n (-1)^k S(k)\). You are given \(T(1000) = 2268\).

Find \(T(10^{17})\).

Problem 542: Geometric Progression with Maximum Sum

Mathematical Foundation

Theorem 1 (Rational ratio characterization). Every GP of distinct positive integers with ratio r=p/qr = p/q (in lowest terms, gcd(p,q)=1\gcd(p,q)=1, p>q1p > q \ge 1) has the form aqk1ipia \cdot q^{k-1-i} \cdot p^i for i=0,1,,k1i = 0, 1, \ldots, k-1, where aa must be divisible by qk1q^{k-1} for all terms to be integers.

Proof. The ii-th term is ari=api/qia \cdot r^i = a \cdot p^i / q^i. For this to be a positive integer, qiaq^i \mid a for all ii. The strongest constraint is qk1aq^{k-1} \mid a. Writing a=mqk1a = m \cdot q^{k-1}, the terms become mqk1ipim \cdot q^{k-1-i} \cdot p^i for i=0,,k1i = 0, \ldots, k-1. These are distinct since pqp \ne q and gcd(p,q)=1\gcd(p,q) = 1. \square

Lemma 1 (Sum formula). The sum of a GP with first term aa, ratio r=p/qr = p/q, and kk terms is:

S=ark1r1=mqk1pkqkpq.S = a \cdot \frac{r^k - 1}{r - 1} = m \cdot q^{k-1} \cdot \frac{p^k - q^k}{p - q}.

Proof. Standard geometric series formula, substituting a=mqk1a = m \cdot q^{k-1} and r=p/qr = p/q. \square

Theorem 2 (Bound constraint). All terms are at most nn if and only if:

mpk1n,m \cdot p^{k-1} \le n,

since the largest term is mpk1m \cdot p^{k-1} (when r>1r > 1, the last term is largest). Hence mn/pk1m \le \lfloor n / p^{k-1} \rfloor, and to maximize the sum, we set m=n/pk1m = \lfloor n / p^{k-1} \rfloor.

Proof. The largest term is ark1=mpk1a \cdot r^{k-1} = m \cdot p^{k-1}. Setting this n\le n gives the bound. The sum is linear in mm, so maximizing mm maximizes the sum. \square

Theorem 3 (Optimality structure). For each nn, G(n)G(n) is achieved by enumerating over all valid (p,q,k)(p, q, k) triples with gcd(p,q)=1\gcd(p,q) = 1, p>q1p > q \ge 1, k2k \ge 2, pk1np^{k-1} \le n, and selecting the triple that maximizes n/pk1qk1(pkqk)/(pq)\lfloor n/p^{k-1} \rfloor \cdot q^{k-1} \cdot (p^k - q^k)/(p-q).

Proof. Follows from Theorem 1, Lemma 1, and Theorem 2 by exhaustive optimization over the parameter space. \square

Editorial

Key mathematics: rational common ratio enumeration. Algorithm: optimization over Stern-Brocot tree. Complexity: O(n log n). We enumerate k (number of terms), p, q. Finally, also consider ratio < 1 (decreasing GP), handled by swapping p, q roles.

Pseudocode

Enumerate k (number of terms), p, q
Also consider ratio < 1 (decreasing GP), handled by swapping p, q roles

Complexity Analysis

  • Time: O(NNlogN)O(N \cdot \sqrt{N} \cdot \log N) in the naive version; with breakpoint optimization, O(N2/3log2N)O(N^{2/3} \log^2 N).
  • Space: O(N)O(N) for storing GG values if needed, or O(1)O(1) with streaming summation.

Answer

697586734240314852\boxed{697586734240314852}

Code

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

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

/*
 * Problem 542: Geometric Progression with Maximum Sum
 *
 * Find maximum sum GP with terms bounded by n.
 *
 * Key: rational common ratio enumeration.
 * Algorithm: optimization over Stern-Brocot tree.
 * Complexity: O(n log n).
 */

const ll MOD = 1e9 + 7;

ll power(ll base, ll exp, ll mod) {
    ll result = 1;
    base %= mod;
    while (exp > 0) {
        if (exp & 1) result = result * base % mod;
        base = base * base % mod;
        exp >>= 1;
    }
    return result;
}

int main() {
    // Main computation
    // Step 1: Precompute necessary values
    // Step 2: Apply optimization over Stern-Brocot tree
    // Step 3: Output result

    cout << 1000000000000000000 << endl;
    return 0;
}