All Euler problems
Project Euler

Coin Partitions

Let p(n) represent the number of different ways in which n coins can be separated into piles. Find the least value of n for which p(n) is divisible by one million.

Source sync Apr 19, 2026
Problem #0078
Level Level 03
Solved By 19,693
Languages C++, Python
Answer 55374
Length 470 words
modular_arithmeticsequencelinear_algebra

Problem Statement

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

Let represent the number of different ways in which coins can be separated into piles. For example, five coins can be separated into piles in exactly seven different ways, so .

OOOOO
OOOO   O
OOO   OO
OOO   O   O
OO   OO   O
OO   O   O   O
O   O   O   O   O

Find the least value of for which is divisible by one million.

Problem 78: Coin Partitions

Mathematical Foundation

Definition 1 (Generalized Pentagonal Numbers). The generalized pentagonal numbers are the values

ωj+=j(3j1)2,ωj=j(3j+1)2,j=1,2,3,\omega_j^{+} = \frac{j(3j-1)}{2}, \qquad \omega_j^{-} = \frac{j(3j+1)}{2}, \qquad j = 1, 2, 3, \ldots

The sequence 1,2,5,7,12,15,22,26,1, 2, 5, 7, 12, 15, 22, 26, \ldots lists these in increasing order.

Theorem 1 (Euler’s Pentagonal Number Theorem). As formal power series (and absolutely for x<1|x| < 1):

k=1(1xk)=1+j=1(1)j(xωj++xωj).\prod_{k=1}^{\infty}(1 - x^k) = 1 + \sum_{j=1}^{\infty} (-1)^j \bigl(x^{\omega_j^{+}} + x^{\omega_j^{-}}\bigr).

Proof (Franklin’s Involution). The coefficient of xnx^n in k=1(1xk)\prod_{k=1}^{\infty}(1-x^k) equals λDn(1)(λ)\sum_{\lambda \in \mathcal{D}_n} (-1)^{\ell(\lambda)}, where Dn\mathcal{D}_n is the set of partitions of nn into distinct parts and (λ)\ell(\lambda) is the number of parts.

For λ=(λ1>λ2>>λr)Dn\lambda = (\lambda_1 > \lambda_2 > \cdots > \lambda_r) \in \mathcal{D}_n, define:

  • s=λrs = \lambda_r: the smallest part.
  • tt: the length of the maximal run of consecutive integers descending from λ1\lambda_1, i.e., t=max{m1:λ1i+1 is a part of λ for all 1im}t = \max\{m \geq 1 : \lambda_1 - i + 1 \text{ is a part of } \lambda \text{ for all } 1 \leq i \leq m\}.

Construct the involution ϕ\phi:

  • Move A (when s<ts < t, or when s=ts = t and the smallest part λr\lambda_r is not part of the top run, i.e., λrλ1t+1\lambda_r \neq \lambda_1 - t + 1): Remove λr=s\lambda_r = s and add 11 to each of the ss largest parts. The result has r1r - 1 distinct parts.
  • Move B (when Move A is inapplicable): Subtract 11 from each of the tt largest parts and insert tt as a new smallest part. The result has r+1r + 1 distinct parts.

These operations satisfy: (i) ϕϕ=id\phi \circ \phi = \mathrm{id} (they are mutual inverses), (ii) each changes the parity of rr, hence reverses the sign (1)r(-1)^r, and (iii) the map ϕ\phi is undefined (fixed point) exactly when:

  • s=ts = t and λr=λ1t+1\lambda_r = \lambda_1 - t + 1: giving λ=(2r1,2r2,,r)\lambda = (2r-1, 2r-2, \ldots, r) and n=r(3r1)/2=ωr+n = r(3r-1)/2 = \omega_r^{+}.
  • s=t+1s = t + 1 and r=sr = s: giving n=r(3r+1)/2=ωrn = r(3r+1)/2 = \omega_r^{-}.

Fixed points contribute (1)r(-1)^r; all other terms cancel pairwise. \blacksquare

Theorem 2 (Partition Recurrence). For n1n \geq 1:

p(n)=j=1(1)j+1[p(nωj+)+p(nωj)],p(n) = \sum_{j=1}^{\infty} (-1)^{j+1} \left[p(n - \omega_j^{+}) + p(n - \omega_j^{-})\right],

where p(0)=1p(0) = 1, p(m)=0p(m) = 0 for m<0m < 0, and the sum terminates when both ωj+\omega_j^{+} and ωj\omega_j^{-} exceed nn.

Proof. From Theorem 1 and the partition generating function np(n)xn=k11xk\sum_n p(n)x^n = \prod_k \frac{1}{1-x^k}, we obtain

(n=0p(n)xn)(k=1(1xk))=1.\left(\sum_{n=0}^{\infty} p(n)x^n\right)\left(\prod_{k=1}^{\infty}(1-x^k)\right) = 1.

Extracting the coefficient of xnx^n for n1n \geq 1:

p(n)+j=1(1)j[p(nωj+)+p(nωj)]=0.p(n) + \sum_{j=1}^{\infty} (-1)^j \left[p(n - \omega_j^{+}) + p(n - \omega_j^{-})\right] = 0.

Multiplying through by 1-1 and using (1)j(1)=(1)j+1(-1)^{j} \cdot (-1) = (-1)^{j+1} yields the recurrence. \blacksquare

Lemma 1 (Pentagonal Number Count). The number of generalized pentagonal numbers not exceeding nn is Θ(n)\Theta(\sqrt{n}).

Proof. From ωj+=j(3j1)/2n\omega_j^{+} = j(3j-1)/2 \leq n, we obtain j1+1+24n6=Θ(n)j \leq \frac{1 + \sqrt{1 + 24n}}{6} = \Theta(\sqrt{n}). Similarly for ωj\omega_j^{-}. \blacksquare

Theorem 3 (Validity of Modular Computation). The recurrence in Theorem 2 is a Z\mathbb{Z}-linear combination of previous partition values with coefficients in {1,+1}\{-1, +1\}. Therefore, for any modulus MM, computing p(n)modMp(n) \bmod M at each step using p(k)modMp(k) \bmod M for k<nk < n correctly yields p(n)modMp(n) \bmod M.

Proof. Since addition and subtraction in Z/MZ\mathbb{Z}/M\mathbb{Z} satisfy (a+b)modM=((amodM)+(bmodM))modM(a + b) \bmod M = ((a \bmod M) + (b \bmod M)) \bmod M and (ab)modM=((amodM)(bmodM))modM(a - b) \bmod M = ((a \bmod M) - (b \bmod M)) \bmod M, the modular reduction commutes with the recurrence at each step. \blacksquare

Editorial

Euler’s pentagonal recurrence is the whole algorithm. It expresses p(n)p(n) as an alternating sum of earlier partition values taken at offsets given by the generalized pentagonal numbers. So instead of building partitions explicitly, we generate only the offsets that can actually contribute and combine previously computed values with the required signs.

Because the problem only asks when p(n)p(n) becomes divisible by one million, we never need the full integers. Reducing modulo 10610^6 after every step preserves the divisibility test and keeps the numbers small. The candidates in each recurrence step are the pentagonal offsets not exceeding the current nn; larger offsets are ignored because they would reference negative indices. We advance nn in order and stop as soon as the partition value becomes 0(mod106)0 \pmod{10^6}.

Pseudocode

Precompute the generalized pentagonal numbers up to a safe search limit, together with their alternating signs.
Set p(0) = 1.

For n from 1 upward:
    start the new partition value at 0

    For each precomputed pentagonal offset that does not exceed n:
        add or subtract the previously computed partition value indicated by its sign

    Reduce the result modulo one million

    If the result is 0:
        return n

Complexity Analysis

Time: Each computation of p(n)p(n) involves summing over O(n)O(\sqrt{n}) pentagonal terms (Lemma 1). Summing over all nn from 11 to nn^* (the answer):

n=1nO(n)=O ⁣((n)3/2).\sum_{n=1}^{n^*} O(\sqrt{n}) = O\!\left((n^*)^{3/2}\right).

With n=55374n^* = 55374, this is approximately 1.3×1071.3 \times 10^7 operations.

Space: O(n)O(n^*) entries storing p(k)mod106p(k) \bmod 10^6 for 0kn0 \leq k \leq n^*.

Answer

55374\boxed{55374}

Code

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

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

int main() {
    const int MOD = 1000000;
    const int MAXN = 100000;

    vector<int> p(MAXN + 1, 0);
    p[0] = 1;

    // Precompute generalized pentagonal numbers with signs
    vector<pair<int, int>> pent; // (pentagonal number, sign)
    for (int k = 1; ; k++) {
        int g1 = k * (3 * k - 1) / 2;
        int g2 = k * (3 * k + 1) / 2;
        if (g1 > MAXN) break;
        int sign = (k % 2 == 1) ? 1 : -1;
        pent.push_back({g1, sign});
        if (g2 <= MAXN)
            pent.push_back({g2, sign});
    }

    for (int n = 1; n <= MAXN; n++) {
        long long val = 0;
        for (auto& [g, s] : pent) {
            if (g > n) break;
            val += (long long)s * p[n - g];
        }
        p[n] = ((int)(val % MOD) + MOD) % MOD;
        if (p[n] == 0) {
            cout << n << endl;
            return 0;
        }
    }

    return 0;
}