FLINT3.7.0-dev
Algorithm

Computing the partition function

Cost
O(n1/2log⁡4+o(1)n)O(n^{1/2} \log^{4+o(1)} n) bit operations for p(n)p(n), quasi-optimal since p(n)p(n) has Θ(n1/2)\Theta(n^{1/2}) bits; O(n3/2+o(1))O(n^{3/2 + o(1)}) for all of p(0),…,p(n)p(0), \ldots, p(n).

The partition function p(n)p(n) counts the partitions of nn, the ways of writing nn as a sum of positive integers without regard to order. Its generating function is Euler's product

∑n=0∞p(n)xn=∏k=1∞11−xk.\sum_{n=0}^{\infty} p(n) x^n = \prod_{k=1}^{\infty} \frac{1}{1 - x^k}.

The values grow subexponentially: log⁡p(n)∼π2n/3\log p(n) \sim \pi \sqrt{2n/3}, so p(n)p(n) has about 3.7n3.7 \sqrt{n} bits. FLINT's methods follow [Joh2012]; for the theory see [Andrews1976, Chapter 5] and [Apostol1997, Chapter 5].

All values up to nn#

By Euler's pentagonal number theorem,

∏k=1∞(1−xk)=∑j=−∞∞(−1)jxj(3j−1)/2=1−x−x2+x5+x7−x12−x15+⋯ ,\prod_{k=1}^{\infty} (1 - x^k) = \sum_{j=-\infty}^{\infty} (-1)^j x^{j(3j-1)/2} = 1 - x - x^2 + x^5 + x^7 - x^{12} - x^{15} + \cdots,

a series with O(n)O(\sqrt n) nonzero terms below xnx^n. Inverting it gives the recurrence

p(n)=∑j≥1(−1)j+1(p(n−j(3j−1)/2)+p(n−j(3j+1)/2)),p(n) = \sum_{j \ge 1} (-1)^{j+1} \bigl( p(n - j(3j-1)/2) + p(n - j(3j+1)/2) \bigr),

which computes p(0),…,p(n)p(0), \ldots, p(n) with O(n3/2)O(n^{3/2}) additions of numbers of O(n)O(\sqrt n) bits. FLINT instead inverts the pentagonal series with fast power series inversion (arith_number_of_partitions_vec, and modulo a word-size integer arith_number_of_partitions_nmod_vec), which costs O(M(n))O(M(n)) operations on coefficients of O(n)O(\sqrt n) bits. The recurrence is used for single values up to n=416n = 416, in single words: since it involves only additions and subtractions, it is exact modulo 2642^{64}, hence exact while p(n)<264p(n) < 2^{64}.

The Hardy–Ramanujan–Rademacher formula#

Hardy and Ramanujan [HardyRamanujan1918] found an asymptotic series for p(n)p(n), and Rademacher [Rademacher1937] modified it into a convergent series. In the form used by FLINT, with

C=π624n−1,U(x)=cosh⁡x−sinh⁡xx,C = \frac{\pi}{6} \sqrt{24n - 1}, \qquad U(x) = \cosh x - \frac{\sinh x}{x},

it reads

p(n)=∑k=1NAk(n)3k424n−1 U ⁣(Ck)+R(n,N),p(n) = \sum_{k=1}^{N} A_k(n) \sqrt{\frac{3}{k}} \frac{4}{24n - 1}\, U\!\left(\frac{C}{k}\right) + R(n, N),

where Ak(n)A_k(n) is the exponential sum

Ak(n)=∑0≤h<kgcd⁡(h,k)=1eπi[s(h,k)−2nh/k],A_k(n) = \sum_{\substack{0 \le h < k \\ \gcd(h, k) = 1}} e^{\pi i [s(h,k) - 2nh/k]},

s(h,k)s(h, k) is a Dedekind sum (see fmpq_dedekind_sum), and the remainder satisfies [Rademacher1937]

∣R(n,N)∣<M(n,N)=44π22253N−1/2+π275(Nn−1)1/2sinh⁡(πN2n3).|R(n, N)| < M(n, N) = \frac{44 \pi^2}{225 \sqrt 3} N^{-1/2} + \frac{\pi \sqrt 2}{75} \left( \frac{N}{n-1} \right)^{1/2} \sinh\left( \frac{\pi}{N} \sqrt{\frac{2n}{3}} \right).

The kk-th term is of the order of eC/ke^{C/k}, so the first term gives almost all the bits of p(n)p(n) and the terms decrease rapidly. ∣Ak(n)∣≤k|A_k(n)| \le k, and the bound M(n,N)M(n, N) falls below 1/21/2 for NN of the order of n\sqrt n: for n=109n = 10^9, M(n,104)≈0.32M(n, 10^4) \approx 0.32. Since p(n)p(n) is an integer, an approximation with total error below 1/21/2 determines it.

Factoring the exponential sums#

Evaluating Ak(n)A_k(n) from its definition costs O(k)O(k) Dedekind sums, O(k2)O(k^2) operations for each kk and O(n3/2)O(n^{3/2}) in total, which would dominate. Lehmer and Whiteman [Whiteman1956] showed that Ak(n)A_k(n) is multiplicative in a twisted sense and that its prime power factors are single cosines. FLINT uses the following form.

Proposition 1. If k=k1k2k = k_1 k_2 with gcd⁡(k1,k2)=1\gcd(k_1, k_2) = 1, then Ak(n)=Ak1(n1)Ak2(n2)A_k(n) = A_{k_1}(n_1) A_{k_2}(n_2) for explicit n1,n2n_1, n_2 determined by congruences modulo k1k_1 and k2k_2. For a prime p>3p > 3 and k=pλk = p^\lambda, let r=1−24n mod kr = 1 - 24n \bmod k. Then

  • if rr is not a square modulo pp, Ak(n)=0A_k(n) = 0;
  • if p∣rp \mid r, Ak(n)=(3/p)pA_k(n) = (3/p) \sqrt p for λ=1\lambda = 1 and Ak(n)=0A_k(n) = 0 for λ>1\lambda > 1;
  • otherwise, with mm such that (24m)2≡r(modk)(24m)^2 \equiv r \pmod k,

    Ak(n)=2(3k)kcos⁡(4πmk).A_k(n) = 2 \left(\frac{3}{k}\right) \sqrt k \cos\left( \frac{4\pi m}{k} \right).

Similar formulas, involving square roots modulo 8k8k and 3k3k, hold for powers of 2 and 3.

So after factoring kk, Ak(n)A_k(n) is a product of ω(k)\omega(k) cosines of rational multiples of π\pi, an algebraic prefactor ±2au/v\pm 2^a \sqrt{u/v}, and a sign, computed with O(log⁡k)O(\log k) modular square roots and inverses (arith_hrr_expsum_factored, which returns the symbolic product as a trig_prod_t). The cost of Ak(n)A_k(n) becomes polylogarithmic in kk, and many Ak(n)A_k(n) vanish.

Precision and error control#

Each term is evaluated at a precision tied to its magnitude: the term kk has absolute value at most 12k eC/k/(24n−1)\sqrt{12k}\, e^{C/k}/(24n - 1), and it must be known to an absolute error of about 2−g/N2^{-g}/N with a few guard bits gg, so the working precision is

preck≈C/klog⁡2+log⁡212k24n−1+log⁡2(8N)+O(log⁡log⁡n),\mathit{prec}_k \approx \frac{C/k}{\log 2} + \log_2 \frac{\sqrt{12 k}}{24n - 1} + \log_2(8N) + O(\log \log n),

where the last term accounts for the number of operations, which depends on the number of prime factors of kk. The precision thus decreases like 1/k1/k: the first term needs the full 3.7n3.7 \sqrt n bits, the term kk about 1/k1/k of that, and most of the N=O(n)N = O(\sqrt n) terms need only a few dozen bits. The cost of all terms is dominated by the first few, and Johansson [Joh2012] shows a total cost of O(n1/2log⁡4+o(1)n)O(n^{1/2} \log^{4 + o(1)} n) for the evaluation with N=O(n)N = O(\sqrt n) terms, within polylogarithmic factors of the size of the output.

All operations are done in ball arithmetic, so the computed sum SS comes with a radius ρ\rho that bounds every rounding error. Adding M(n,N)M(n, N) to the radius gives a ball [S±(ρ+M(n,N))][S \pm (\rho + M(n,N))] that is guaranteed to contain p(n)p(n); if it contains exactly one integer, that integer is p(n)p(n). This makes the result a proof; the implementation of [Joh2012] used MPFR with error estimates that were not fully rigorous.

The angles of the cosines are reduced to [0,π/4][0, \pi/4] (using sines near π/2\pi/2), so that every factor is known with full relative precision, and exp⁡(C/k)\exp(C/k) is computed from exp⁡(C)\exp(C) by a kk-th root when that is cheaper than a new exponential.

Use in FLINT#

The generic functions gr_partitions_ui, gr_partitions_fmpz and gr_partitions_vec call partitions_fmpz_ui, partitions_fmpz_fmpz and the vector functions over Z\mathbb{Z} and Z/nZ\mathbb{Z}/n\mathbb{Z}, and arb_partitions_fmpz in real and complex rings.

Referenced from#

References#

  • [Joh2012] F. Johansson, "Efficient implementation of the Hardy-Ramanujan-Rademacher formula", LMS Journal of Computation and Mathematics, Volume 15 (2012), 341-359, http://journals.cambridge.org/action/displayAbstract?fromPage=online&aid=8710297
  • [Andrews1976] G. E. Andrews, The Theory of Partitions, Encyclopedia of Mathematics and its Applications 2, Addison-Wesley, 1976.
  • [Apostol1997] Apostol, Tom : Modular functions and Dirichlet series in number theory, Springer (1997)
  • [HardyRamanujan1918] G. H. Hardy and S. Ramanujan, "Asymptotic formulae in combinatory analysis", Proceedings of the London Mathematical Society (2) 17 (1918), 75–115.
  • [Rademacher1937] Rademacher, Hans : On the partition function p(n)p(n) Proc. London Math. Soc vol. 43 (1937) 241–254
  • [Whiteman1956] Whiteman, A. L. : A sum connected with the series for the partition function, Pacific Journal of Mathematics 6:1 (1956) 159–176
Cite this page

The FLINT team, “Computing the partition function,” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/algorithms/number-theory/partitions.html (generated 2026-11-02).