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...
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 -th pentagonal number is for .
Definition 2. A positive integer is pentagonal if for some .
Theoretical Development
Theorem 1 (Pentagonal number characterization). A positive integer is pentagonal if and only if is a perfect square with .
Proof. () Let . Then
Set . Since , we have and .
() Suppose with . Write for some (justified since and implies ). Then
Corollary 1 (Equivalent formulation). is pentagonal if and only if is a positive integer. Equivalently, must be a perfect square and must be divisible by .
Proof. Solving via the quadratic formula yields (positive root). Setting , the condition is equivalent to .
Lemma 1 (Difference formula). For ,
Proof.
Lemma 2 (Monotonicity in ). For fixed , the function is strictly decreasing on .
Proof. We have for all . In discrete terms, , so .
Lemma 3 (Monotonicity in gap). For fixed gap , the difference is strictly increasing in .
Proof. By Lemma 1, , which is linear in with positive coefficient .
Lemma 4 (Minimum gap bound). For , the smallest difference .
Proof. .
Theorem 2 (Search strategy and termination). The minimum pentagonal difference can be found by iterating and, for each , iterating . Once a valid pair is found with , we continue only while (by Lemma 4, no future can improve once its minimum gap exceeds the current best).
Proof. For any fixed , the minimum possible difference is (Lemma 4). Once (the current best), no pair involving can achieve a smaller difference. This gives the termination criterion .
Theorem 3. The minimum pentagonal difference is , achieved by .
Proof. By exhaustive search using the strategy of Theorem 2:
Verification of pentagonality:
- For : , and , so .
- For : , and , so .
The search terminates after confirming that no pair with smaller exists: all up to must be checked, but in practice the inner loop terminates early (via Lemma 2) for most values.
Editorial
We iterate over the larger index in increasing order and compute on demand. For each fixed , the smaller index is scanned downward from , so the corresponding differences 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 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 be the index of the larger pentagonal number in the optimal pair. The algorithm performs pentagonal tests in the worst case, each costing . With , the worst-case bound is .
Proof. The outer loop runs from to at most . The inner loop runs from down to in the worst case, giving iterations. Each iteration performs two pentagonal tests (via integer square root).
Proposition (Space complexity). : all pentagonal numbers and tests are computed on the fly.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
import math
def pent(n):
"""Compute the n-th pentagonal number P_n = n(3n-1)/2."""
return n * (3 * n - 1) // 2
def is_pentagonal(v):
"""Test if v is pentagonal: v = P_n iff (1 + sqrt(1+24v)) / 6 is a
positive integer, equivalently 1+24v is a perfect square s^2 with
s = 5 mod 6.
"""
if v <= 0:
return False
disc = 1 + 24 * v
s = math.isqrt(disc)
if s * s != disc:
return False
return (1 + s) % 6 == 0
def solve():
"""Find the pair (P_j, P_k) with j < k such that both P_k - P_j and
P_k + P_j are pentagonal, and D = P_k - P_j is minimised.
Search strategy: iterate k, for each k try j = k-1 downto 1.
Terminate outer loop when the minimum gap 3k-2 exceeds current best.
"""
best = float('inf')
for k in range(2, 10000):
pk = pent(k)
if 3 * k - 2 >= best:
break
for j in range(k - 1, 0, -1):
pj = pent(j)
diff = pk - pj
if diff >= best:
break
if is_pentagonal(diff) and is_pentagonal(pk + pj):
best = diff
print(best)
solve()