All Euler problems
Project Euler

One Million Members

A palindromic partition of n is a multiset of positive integers that sums to n and, when listed in non-decreasing order, reads the same forwards and backwards. Equivalently, every part occurs an ev...

Source sync Apr 19, 2026
Problem #0710
Level Level 12
Solved By 1,555
Languages C++, Python
Answer 1275000
Length 417 words
dynamic_programmingsequenceanalytic_math

Problem Statement

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

The number 6 can be written as a palindromic sum in exactly eight different ways: $$(1, 1, 1, 1, 1, 1), (1, 1, 2, 1, 1), (1, 2, 2, 1), (1, 4, 1), (2, 1, 1, 2), (2, 2, 2), (3, 3), (6)$$

We shall define a twopal to be a palindromic tuple having at least one element with a value of 2. It should also be noted that elements are not restricted to single digits. For example, $(3, 2, 13, 6, 13, 2, 3)$ is a valid twopal.

If we let $t(n)$ be the number of twopals whose elements sum to $n$, then it can be seen that $t(6) = 4$: $$(1, 1, 2, 1, 1), (1, 2, 2, 1), (2, 1, 1, 2), (2, 2, 2)$$ Similarly, $t(20) = 824$.

In searching for the answer to the ultimate question of life, the universe, and everything, it can be verified that $t(42) = 1999923$, which happens to be the first value of $t(n)$ that exceeds one million.

However, your challenge to the "ultimatest" question of life, the universe, and everything is to find the least value of $n \gt 42$ such that $t(n)$ is divisible by one million.

Problem 710: One Million Members

Mathematical Foundation

Theorem 1 (Structure of Palindromic Partitions). A palindromic partition of nn is equivalent to choosing:

  1. A “center” part cc (which may be 0, meaning no center), and
  2. A partition of (nc)/2(n - c)/2 into unrestricted parts (each appearing with even multiplicity in the original, counted once here).

The center cc must satisfy cnc \le n, cn(mod2)c \equiv n \pmod{2}, and (nc)/20(n - c)/2 \ge 0.

Proof. In a palindromic partition, each part except possibly one center part appears an even number of times. Removing the center part cc (or setting c=0c = 0 if all parts have even multiplicity) leaves a multiset where every part has even multiplicity. Halving each multiplicity gives an unrestricted partition of (nc)/2(n - c)/2. This map is a bijection. \square

Theorem 2 (Generating Function for Even-Part Palindromic Partitions). When restricted to even parts, the center cc must be even (or 0), and the “half-partition” uses even parts only. Let pe(m)p_e(m) denote the number of partitions of mm into even parts. Then

t(n)=c=0,2,4,cn,cn(mod2)pe ⁣(nc2).t(n) = \sum_{\substack{c = 0, 2, 4, \ldots \\ c \le n,\; c \equiv n \!\!\pmod{2}}} p_e\!\left(\frac{n - c}{2}\right).

Proof. Direct from Theorem 1 with the constraint that all parts are even. The center c{0,2,4,}c \in \{0, 2, 4, \ldots\} is even (or absent), and the remaining parts form an even-part partition. \square

Lemma 1 (Partition into Even Parts). pe(m)=p(m/2)p_e(m) = p(\lfloor m/2 \rfloor) where p(k)p(k) is the standard partition function, since dividing each even part by 2 gives a bijection with unrestricted partitions.

Proof. A partition of mm into even parts {2a1,2a2,}\{2a_1, 2a_2, \ldots\} bijects with a partition of m/2m/2 into positive integers {a1,a2,}\{a_1, a_2, \ldots\} (when mm is even; when mm is odd, pe(m)=0p_e(m) = 0). \square

Theorem 3 (Recurrence for t(n)t(n)). The values t(n)t(n) satisfy a computable recurrence derived from the partition function recurrence (Euler’s pentagonal number theorem):

p(n)=k=1(1)k+1(p ⁣(nk(3k1)2)+p ⁣(nk(3k+1)2))p(n) = \sum_{k=1}^{\infty} (-1)^{k+1}\left(p\!\left(n - \frac{k(3k-1)}{2}\right) + p\!\left(n - \frac{k(3k+1)}{2}\right)\right)

with p(0)=1p(0) = 1 and p(n)=0p(n) = 0 for n<0n < 0. Using this to build a table of p(n)p(n) values, t(n)t(n) is computed via the sum in Theorem 2.

Proof. Euler’s pentagonal number theorem gives k=1(1xk)=k=(1)kxk(3k1)/2\prod_{k=1}^{\infty}(1-x^k) = \sum_{k=-\infty}^{\infty}(-1)^k x^{k(3k-1)/2}. Inverting yields the recurrence for p(n)p(n). \square

Editorial

We build partition function table using pentagonal recurrence. Finally, compute t(n) for each n and check divisibility by 10^6. We enumerate the admissible parameter range, discard candidates that violate the derived bounds or arithmetic constraints, and update the final set or total whenever a candidate passes the acceptance test.

Pseudocode

Build partition function table using pentagonal recurrence
Compute t(n) for each n and check divisibility by 10^6
if m is even
else p_e(m) = 0

Complexity Analysis

  • Time: O(nmaxnmax)O(n_{\max} \sqrt{n_{\max}}) for building the partition table (each entry uses O(n)O(\sqrt{n}) pentagonal terms). Computing t(n)t(n) for each candidate takes O(n)O(n) per candidate, but can be optimized.
  • Space: O(nmax)O(n_{\max}) for the partition function table.

Answer

1275000\boxed{1275000}

Code

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

C++ project_euler/problem_710/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    int N = 1000;
    vector<long long> dp(N + 1, 0);
    dp[0] = 1;
    for (int k = 1; k <= N; k++)
        for (int j = N; j >= 2*k; j--)
            dp[j] += dp[j - 2*k];

    for (int n = 1; n <= N; n++) {
        long long t = dp[n];
        for (int c = 1; c <= n; c++)
            t += dp[n - c];
        if (t > 1000000) {
            cout << n << endl;
            return 0;
        }
    }
    return 0;
}