All Euler problems
Project Euler

Generalized Pentagonal Numbers

The generalized pentagonal numbers are omega_k = k(3k-1)/2 for k = 0, +/- 1, +/- 2,..., yielding 0, 1, 2, 5, 7, 12, 15, 22, 26,.... Find the sum of all generalized pentagonal numbers below 10^7.

Source sync Apr 19, 2026
Problem #0964
Level Level 37
Solved By 163
Languages C++, Python
Answer 4.7126135532e-29
Length 295 words
modular_arithmeticsequencebrute_force

Problem Statement

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

A group of $k(k-1) / 2 + 1$ children play a game of $k$ rounds.

At the beginning, they are all seated on chairs arranged in a circle.

During the $i$-th round:

  • The music starts playing and $i$ children are randomly selected, with all combinations being equally likely. The selected children stand up and dance around.

  • When the music stops, these $i$ children sit back down randomly in the $i$ available chairs, with all permutations being equally likely.

Let $P(k)$ be the probability that every child ends up sitting exactly one chair to the right of their original chair when the game ends (at the end of the $k$-th round).

You are given $P(3) \approx 1.3888888889 \mathrm {e}{-2}$.

Find $P(7)$. Give your answer in scientific notation rounded to ten significant digits after the decimal point. Use a lowercase e to separate the mantissa and the exponent.

Problem 964: Generalized Pentagonal Numbers

Mathematical Analysis

Euler’s Pentagonal Number Theorem

The generalized pentagonal numbers arise from one of Euler’s most beautiful identities:

n=1(1xn)=k=(1)kxωk\prod_{n=1}^{\infty} (1 - x^n) = \sum_{k=-\infty}^{\infty} (-1)^k x^{\omega_k}

where ωk=k(3k1)/2\omega_k = k(3k-1)/2. This identity is fundamental to the theory of integer partitions, as it provides a recurrence for the partition function p(n)p(n).

Definition. The generalized pentagonal numbers are given by:

  • For k>0k > 0: ωk=k(3k1)/2=1,5,12,22,35,51,\omega_k = k(3k-1)/2 = 1, 5, 12, 22, 35, 51, \ldots
  • For k<0k < 0, i.e., k=mk = -m: ωm=m(3m+1)/2=2,7,15,26,40,57,\omega_{-m} = m(3m+1)/2 = 2, 7, 15, 26, 40, 57, \ldots
  • ω0=0\omega_0 = 0

Interleaving both branches: 0,1,2,5,7,12,15,22,26,35,40,51,57,70,77,0, 1, 2, 5, 7, 12, 15, 22, 26, 35, 40, 51, 57, 70, 77, \ldots

Connection to Partition Function

Theorem (Euler). The partition function satisfies:

p(n)=k=1(1)k+1[p(nωk)+p(nωk)]p(n) = \sum_{k=1}^{\infty} (-1)^{k+1} \left[p(n - \omega_k) + p(n - \omega_{-k})\right]

where p(m)=0p(m) = 0 for m<0m < 0 and p(0)=1p(0) = 1. This gives an O(n3/2)O(n^{3/2}) algorithm for computing p(n)p(n).

Range Analysis

For N=107N = 10^7:

  • Positive branch: k(3k1)/2<Nk(3k-1)/2 < N gives k<(24N+1+1)/62582k < (\sqrt{24N+1}+1)/6 \approx 2582.
  • Negative branch: m(3m+1)/2<Nm(3m+1)/2 < N gives m<(24N+11)/62581m < (\sqrt{24N+1}-1)/6 \approx 2581.

Total count: approximately 2×2582+1=51652 \times 2582 + 1 = 5165 generalized pentagonal numbers below 10710^7 (including ω0=0\omega_0 = 0, which contributes 0 to the sum).

Growth Rate

The generalized pentagonal numbers grow quadratically: ωk3k22\omega_k \sim \frac{3k^2}{2} for large k|k|. Their density among positive integers decreases as O(1/n)O(1/\sqrt{n}).

Concrete Examples

kkωk\omega_kωk\omega_{-k}
112
257
31215
42226
53540
10145155
1001495015050

Derivation

Editorial

Sum of all generalized pentagonal numbers omega_k = k(3k-1)/2 for k = 0, +/-1, +/-2, … that are below 10^7. The generalized pentagonal numbers arise in Euler’s pentagonal number theorem for the partition function. The sequence begins: 0, 1, 2, 5, 7, 12, 15, 22, 26, 35, 40, … We iterate over k=1,2,k = 1, 2, \ldots generate ωk=k(3k1)/2\omega_k = k(3k-1)/2 until ωkN\omega_k \ge N. We then iterate over m=1,2,m = 1, 2, \ldots generate ωm=m(3m+1)/2\omega_{-m} = m(3m+1)/2 until ωmN\omega_{-m} \ge N. Finally, sum all values below NN.

Pseudocode

For $k = 1, 2, \ldots$ generate $\omega_k = k(3k-1)/2$ until $\omega_k \ge N$
For $m = 1, 2, \ldots$ generate $\omega_{-m} = m(3m+1)/2$ until $\omega_{-m} \ge N$
Sum all values below $N$

Partial Sum Formula

k=1Kk(3k1)2=32k212k=K(K+1)(2K+1)4K(K+1)4=K(K+1)(2K+11)4=K2(K+1)2\sum_{k=1}^{K} \frac{k(3k-1)}{2} = \frac{3}{2}\sum k^2 - \frac{1}{2}\sum k = \frac{K(K+1)(2K+1)}{4} - \frac{K(K+1)}{4} = \frac{K(K+1)(2K+1-1)}{4} = \frac{K^2(K+1)}{2}

Wait: 32K(K+1)(2K+1)612K(K+1)2=K(K+1)(2K+1)4K(K+1)4=K(K+1)2K4=K2(K+1)2\frac{3}{2} \cdot \frac{K(K+1)(2K+1)}{6} - \frac{1}{2} \cdot \frac{K(K+1)}{2} = \frac{K(K+1)(2K+1)}{4} - \frac{K(K+1)}{4} = \frac{K(K+1) \cdot 2K}{4} = \frac{K^2(K+1)}{2}.

Similarly m=1Mm(3m+1)/2=M(M+1)(2M+1)4+M(M+1)4=M(M+1)(M+1)2=M(M+1)22\sum_{m=1}^{M} m(3m+1)/2 = \frac{M(M+1)(2M+1)}{4} + \frac{M(M+1)}{4} = \frac{M(M+1)(M+1)}{2} = \frac{M(M+1)^2}{2}.

But the last terms may exceed NN, requiring truncation.

Proof of Correctness

Each generalized pentagonal number is generated exactly once. The enumeration terminates correctly at the first value exceeding NN.

Complexity Analysis

O(N)O(\sqrt{N}) time and space.

Answer

4.7126135532e29\boxed{4.7126135532e-29}

Code

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

C++ project_euler/problem_964/solution.cpp
/*
 * Problem 964: Generalized Pentagonal Numbers
 *
 * Sum all omega_k = k(3k-1)/2 for k = +-1, +-2, ... below 10^7.
 *
 * Complexity: O(sqrt(N)).
 */

#include <bits/stdc++.h>
using namespace std;

int main() {
    const long long N = 10000000;
    long long total = 0;

    for (long long k = 1; ; k++) {
        long long v1 = k * (3*k - 1) / 2;
        long long v2 = k * (3*k + 1) / 2;
        if (v1 >= N && v2 >= N) break;
        if (v1 < N) total += v1;
        if (v2 < N) total += v2;
    }

    cout << total << endl;
    return 0;
}