Computing the partition function
- Cost
- bit operations for , quasi-optimal since has bits; for all of .
The partition function counts the partitions of , the ways of writing as a sum of positive integers without regard to order. Its generating function is Euler's product
The values grow subexponentially: , so has about bits. FLINT's methods follow [Joh2012]; for the theory see [Andrews1976, Chapter 5] and [Apostol1997, Chapter 5].
All values up to #
By Euler's pentagonal number theorem,
a series with nonzero terms below . Inverting it gives the recurrence
which computes with additions of numbers of 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 operations on coefficients of bits. The recurrence is used for single values up to , in single words: since it involves only additions and subtractions, it is exact modulo , hence exact while .
The Hardy–Ramanujan–Rademacher formula#
Hardy and Ramanujan [HardyRamanujan1918] found an asymptotic series for , and Rademacher [Rademacher1937] modified it into a convergent series. In the form used by FLINT, with
it reads
where is the exponential sum
is a Dedekind sum (see fmpq_dedekind_sum), and the remainder satisfies [Rademacher1937]
The -th term is of the order of , so the first term gives almost all the bits of and the terms decrease rapidly. , and the bound falls below for of the order of : for , . Since is an integer, an approximation with total error below determines it.
Factoring the exponential sums#
Evaluating from its definition costs Dedekind sums, operations for each and in total, which would dominate. Lehmer and Whiteman [Whiteman1956] showed that is multiplicative in a twisted sense and that its prime power factors are single cosines. FLINT uses the following form.
Proposition 1. If with , then for explicit determined by congruences modulo and . For a prime and , let . Then
- if is not a square modulo , ;
- if , for and for ;
otherwise, with such that ,
Similar formulas, involving square roots modulo and , hold for powers of 2 and 3.
So after factoring , is a product of cosines of rational multiples of , an algebraic prefactor , and a sign, computed with modular square roots and inverses (arith_hrr_expsum_factored, which returns the symbolic product as a trig_prod_t). The cost of becomes polylogarithmic in , and many vanish.
Precision and error control#
Each term is evaluated at a precision tied to its magnitude: the term has absolute value at most , and it must be known to an absolute error of about with a few guard bits , so the working precision is
where the last term accounts for the number of operations, which depends on the number of prime factors of . The precision thus decreases like : the first term needs the full bits, the term about of that, and most of the 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 for the evaluation with terms, within polylogarithmic factors of the size of the output.
All operations are done in ball arithmetic, so the computed sum comes with a radius that bounds every rounding error. Adding to the radius gives a ball that is guaranteed to contain ; if it contains exactly one integer, that integer is . 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 (using sines near ), so that every factor is known with full relative precision, and is computed from by a -th root when that is cheaper than a new exponential.
Use in FLINT#
partitions_fmpz_uiandpartitions_fmpz_fmpzuse the tablepartitions_lookupfor , the pentagonal recurrence in words for , andmp_real_partitions_hrrabove.mp_real_partitions_hrrchooses the number of terms with (by bisection on a cheap estimate, then exactly), factors each witharith_hrr_expsum_factored, and sorts the terms by the precision they need: terms of up to about 200 bits are evaluated in batches asdfloatballs of one to four doubles with the vectorisedgr_vecfunctions, and the others asmp_realballs of the needed number of limbs. Above , the constants , and are computed with all threads, and the terms are distributed over the threads offlint_set_num_threadsin ranges of equal estimated cost. The remainder bound comes frompartitions_rademacher_bound, and the result frommp_real_unique_integer.arith_number_of_partitionsis a wrapper ofpartitions_fmpz_ui;arith_number_of_partitions_vecandarith_number_of_partitions_nmod_vecinvert the pentagonal series.partitions_leading_fmpzevaluates the dominant part of the first term, andarb_partitions_fmpzapproximations of to a given precision.
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 and , and arb_partitions_fmpz in real and complex rings.
void partitions_fmpz_fmpz(fmpz_t p, const fmpz_t n, int use_doubles)void partitions_rademacher_bound(arf_t b, const fmpz_t n, ulong N)void arith_hrr_expsum_factored(trig_prod_t prod, ulong k, ulong n)void arith_number_of_partitions_vec(fmpz * res, slong len)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 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
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).
@misc{flint-doc-algorithms-number-theory-partitions,
author = {{The FLINT team}},
title = {{Computing the partition function}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/algorithms/number-theory/partitions.html},
note = {Generated 2026-11-02}
}