All Euler problems
Project Euler

Pentagon Numbers

Pentagonal numbers are generated by P_n = (n(3n-1))/(2) for n >= 1. Find the pair of pentagonal numbers P_j and P_k (with j < k) for which both their sum P_j + P_k and difference P_k - P_j are pent...

Source sync Apr 19, 2026
Problem #0044
Level Level 01
Solved By 65,418
Languages C++, Python
Answer 5482660
Length 497 words
optimizationmodular_arithmeticsearch

Problem Statement

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

Pentagonal numbers are generated by the formula, $P_n=n(3n-1)/2$. The first ten pentagonal numbers are: $$1, 5, 12, 22, 35, 51, 70, 92, 117, 145, \dots$$ It can be seen that $P_4 + P_7 = 22 + 70 = 92 = P_8$. However, their difference, $70 - 22 = 48$, is not pentagonal.

Find the pair of pentagonal numbers, $P_j$ and $P_k$, for which their sum and difference are pentagonal and $D = |P_k - P_j|$ is minimised; what is the value of $D$?

Problem 44: Pentagon Numbers

Mathematical Development

Definitions

Definition 1. The nn-th pentagonal number is Pn=n(3n1)2P_n = \frac{n(3n-1)}{2} for nZ+n \in \mathbb{Z}^+.

Definition 2. A positive integer vv is pentagonal if v=Pnv = P_n for some nZ+n \in \mathbb{Z}^+.

Theoretical Development

Theorem 1 (Pentagonal number characterization). A positive integer vv is pentagonal if and only if 24v+124v + 1 is a perfect square s2s^2 with s5(mod6)s \equiv 5 \pmod{6}.

Proof. (\Rightarrow) Let v=Pn=n(3n1)2v = P_n = \frac{n(3n-1)}{2}. Then

24v+1=12n(3n1)+1=36n212n+1=(6n1)2.24v + 1 = 12n(3n - 1) + 1 = 36n^2 - 12n + 1 = (6n - 1)^2.

Set s=6n1s = 6n - 1. Since n1n \geq 1, we have s5s \geq 5 and s=6n115(mod6)s = 6n - 1 \equiv -1 \equiv 5 \pmod{6}.

(\Leftarrow) Suppose 24v+1=s224v + 1 = s^2 with s5(mod6)s \equiv 5 \pmod{6}. Write s=6n1s = 6n - 1 for some nZ+n \in \mathbb{Z}^+ (justified since s5(mod6)s \equiv 5 \pmod{6} and s5s \geq 5 implies n1n \geq 1). Then

24v+1=(6n1)2=36n212n+1    v=36n212n24=n(3n1)2=Pn.24v + 1 = (6n-1)^2 = 36n^2 - 12n + 1 \implies v = \frac{36n^2 - 12n}{24} = \frac{n(3n-1)}{2} = P_n. \quad \square

Corollary 1 (Equivalent formulation). vv is pentagonal if and only if n=1+1+24v6n = \frac{1 + \sqrt{1 + 24v}}{6} is a positive integer. Equivalently, 1+24v1 + 24v must be a perfect square and (1+1+24v)(1 + \sqrt{1+24v}) must be divisible by 66.

Proof. Solving Pn=vP_n = v via the quadratic formula yields n=1+1+24v6n = \frac{1 + \sqrt{1+24v}}{6} (positive root). Setting s=1+24vs = \sqrt{1+24v}, the condition 6(1+s)6 \mid (1 + s) is equivalent to s5(mod6)s \equiv 5 \pmod{6}. \square

Lemma 1 (Difference formula). For 1j<k1 \leq j < k,

PkPj=(kj)(3(k+j)1)2.P_k - P_j = \frac{(k - j)\bigl(3(k + j) - 1\bigr)}{2}.

Proof.

PkPj=k(3k1)j(3j1)2=3(k2j2)(kj)2=(kj)(3(k+j)1)2.P_k - P_j = \frac{k(3k-1) - j(3j-1)}{2} = \frac{3(k^2 - j^2) - (k - j)}{2} = \frac{(k-j)\bigl(3(k+j) - 1\bigr)}{2}. \quad \square

Lemma 2 (Monotonicity in jj). For fixed kk, the function jPkPjj \mapsto P_k - P_j is strictly decreasing on {1,2,,k1}\{1, 2, \ldots, k-1\}.

Proof. We have ddj(PkPj)=Pj=(3j12)<0\frac{d}{dj}(P_k - P_j) = -P_j' = -(3j - \tfrac{1}{2}) < 0 for all j1j \geq 1. In discrete terms, (PkPj)(PkPj+1)=Pj+1Pj=3j+1>0(P_k - P_j) - (P_k - P_{j+1}) = P_{j+1} - P_j = 3j + 1 > 0, so PkPj>PkPj+1P_k - P_j > P_k - P_{j+1}. \square

Lemma 3 (Monotonicity in gap). For fixed gap d=kj1d = k - j \geq 1, the difference PkPkdP_k - P_{k-d} is strictly increasing in kk.

Proof. By Lemma 1, PkPkd=d(6k3d1)2P_k - P_{k-d} = \frac{d(6k - 3d - 1)}{2}, which is linear in kk with positive coefficient 3d>03d > 0. \square

Lemma 4 (Minimum gap bound). For k2k \geq 2, the smallest difference PkPk1=3k2P_k - P_{k-1} = 3k - 2.

Proof. PkPk1=1(3(2k1)1)2=6k42=3k2P_k - P_{k-1} = \frac{1 \cdot (3(2k-1) - 1)}{2} = \frac{6k - 4}{2} = 3k - 2. \square

Theorem 2 (Search strategy and termination). The minimum pentagonal difference DD can be found by iterating k=2,3,k = 2, 3, \ldots and, for each kk, iterating j=k1,k2,,1j = k-1, k-2, \ldots, 1. Once a valid pair (j,k)(j, k) is found with D=PkPjD = P_k - P_j, we continue only while PkPk1<DP_k - P_{k-1} < D (by Lemma 4, no future kk can improve once its minimum gap exceeds the current best).

Proof. For any fixed k>kk' > k, the minimum possible difference is PkPk1=3k2P_{k'} - P_{k'-1} = 3k' - 2 (Lemma 4). Once 3k2D3k' - 2 \geq D^* (the current best), no pair involving kk' can achieve a smaller difference. This gives the termination criterion kD+23k' \leq \frac{D^* + 2}{3}. \square

Theorem 3. The minimum pentagonal difference is D=5482660D = 5482660, achieved by (j,k)=(1020,2167)(j, k) = (1020, 2167).

Proof. By exhaustive search using the strategy of Theorem 2:

  • P1020=102030592=1560090P_{1020} = \frac{1020 \cdot 3059}{2} = 1560090
  • P2167=216765002=7042750P_{2167} = \frac{2167 \cdot 6500}{2} = 7042750
  • D=P2167P1020=5482660D = P_{2167} - P_{1020} = 5482660
  • S=P2167+P1020=8602840S = P_{2167} + P_{1020} = 8602840

Verification of pentagonality:

  • For D=5482660D = 5482660: 245482660+1=131583841=11471224 \cdot 5482660 + 1 = 131583841 = 11471^2, and 11471=61912111471 = 6 \cdot 1912 - 1, so D=P1912D = P_{1912}.
  • For S=8602840S = 8602840: 248602840+1=206468161=14369224 \cdot 8602840 + 1 = 206468161 = 14369^2, and 14369=62395114369 = 6 \cdot 2395 - 1, so S=P2395S = P_{2395}.

The search terminates after confirming that no pair with smaller DD exists: all kk up to 5482660+231827554\frac{5482660 + 2}{3} \approx 1827554 must be checked, but in practice the inner loop terminates early (via Lemma 2) for most kk values. \square

Editorial

We iterate over the larger index kk in increasing order and compute PkP_k on demand. For each fixed kk, the smaller index jj is scanned downward from k1k-1, so the corresponding differences PkPjP_k - P_j are examined from smallest to largest; this lets us stop the inner scan once the current difference is no better than the best solution already known. The outer loop also terminates as soon as the minimum possible gap PkPk1P_k - P_{k-1} exceeds the best difference, and every candidate pair is checked with the pentagonal discriminant criterion for both the sum and the difference.

Pseudocode

Algorithm: Minimum Difference of Pentagonal Pairs
Require: The pentagonal number sequence P_n = n(3n - 1) / 2.
Ensure: The least difference P_k - P_j such that both P_k - P_j and P_k + P_j are pentagonal.
1: Initialize best ← infinity.
2: For k ← 2, 3, 4, ... do:
3:     Compute P_k and, if the smallest possible difference at index k already satisfies 3k - 2 ≥ best, return best.
4:     For j from k - 1 down to 1, set Delta ← P_k - P_j; if Delta ≥ best, stop the inner scan for this k.
5:     If both Delta and P_k + P_j are pentagonal, update best ← Delta.

Complexity Analysis

Proposition (Time complexity). Let KK be the index of the larger pentagonal number in the optimal pair. The algorithm performs O(K2)O(K^2) pentagonal tests in the worst case, each costing O(1)O(1). With K=2167K = 2167, the worst-case bound is O(K2)4.7×106O(K^2) \approx 4.7 \times 10^6.

Proof. The outer loop runs from k=2k = 2 to at most KK. The inner loop runs from j=k1j = k-1 down to 11 in the worst case, giving k=2K(k1)=K(K1)2=O(K2)\sum_{k=2}^{K} (k-1) = \frac{K(K-1)}{2} = O(K^2) iterations. Each iteration performs two O(1)O(1) pentagonal tests (via integer square root). \square

Proposition (Space complexity). O(1)O(1): all pentagonal numbers and tests are computed on the fly.

Answer

5482660\boxed{5482660}

Code

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

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

long long pent(long long n) {
    return n * (3 * n - 1) / 2;
}

bool is_pentagonal(long long v) {
    // v is pentagonal iff 1+24v = s^2 with s = 5 mod 6,
    // equivalently (1 + s) % 6 == 0.
    if (v <= 0) return false;
    long long disc = 1 + 24 * v;
    long long s = (long long)sqrt((double)disc);
    while (s * s < disc) s++;
    while (s * s > disc) s--;
    if (s * s != disc) return false;
    return (1 + s) % 6 == 0;
}

int main() {
    // Search for minimal D = P_k - P_j with both D and P_k + P_j pentagonal.
    // Terminate when minimum gap 3k-2 >= best.
    long long best = LLONG_MAX;
    for (long long k = 2; ; k++) {
        if (3 * k - 2 >= best) break;
        long long pk = pent(k);
        for (long long j = k - 1; j >= 1; j--) {
            long long pj = pent(j);
            long long diff = pk - pj;
            if (diff >= best) break;
            if (is_pentagonal(diff) && is_pentagonal(pk + pj)) {
                best = diff;
            }
        }
    }
    cout << best << endl;
    return 0;
}