partitions.h
#include <flint/partitions.h>This module computes the integer partition function p(n), the number of ways of writing n as a sum of positive integers without regard to order: p(4) = 5 (4, 3+1, 2+2, 2+1+1, 1+1+1+1). Small values come from a table and Euler's pentagonal recurrence; large values from the Hardy–Ramanujan–Rademacher formula, a convergent series whose leading term is about e^{\pi \sqrt{2n/3}}/(4\sqrt 3\, n), so that p(n) has about 3.7 \sqrt n bits. The implementation [Joh2012] evaluates a truncation of the series in ball arithmetic with per-term precision, factors the exponential sums symbolically, and adds Rademacher's bound for the remainder, so the result is proved correct; p(10^9), a number of 117 thousand bits, takes a few hundredths of a second.
The evaluation itself is done by mp_real_partitions_hrr in mp_real, which uses hardware double-based balls (dfloat) for the many low-precision terms and several threads for large n. For all values p(0), \ldots, p(n-1) at once, use arith_number_of_partitions_vec, and for approximations arb_partitions_fmpz. See Computing p(n) for usage and Computing the partition function for the mathematics.
The partition function#
void partitions_fmpz_fmpz(fmpz_t p, const fmpz_t n, int use_doubles)void partitions_fmpz_ui(fmpz_t p, ulong n)Set p to p(n), which is 0 for n < 0. The argument use_doubles is ignored. The result is exact: the formula is evaluated in ball arithmetic with a proved bound for the truncation error, and the function throws if the final ball does not determine a unique integer (which is not expected to happen). See Computing p(n).
Deprecated alias of partitions_fmpz_ui.
Bounds and approximations#
void partitions_rademacher_bound(arf_t b, const fmpz_t n, ulong N)Sets b to an upper bound for
Rademacher's bound for the error of the Hardy–Ramanujan–Rademacher series truncated after the term k = N [Rademacher1937]. Requires n \ge 2 and N \ge 1. The constants are rounded upwards and the bound is evaluated with directed rounding at about 30 bits.
void partitions_leading_fmpz(arb_t res, const fmpz_t n, slong prec)Sets res to the dominant part of the leading term k = 1 of the Hardy–Ramanujan–Rademacher series for p(n),
which omits the part e^{-t}(1 + 1/t) \sqrt{12}/(24n-1) of the term and approximates p(n) with relative error about e^{-t/2}. The precision of the exponential is increased by half the bit length of n for the argument reduction. Requires n \ge 1.
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
- [Rademacher1937] Rademacher, Hans : On the partition function p(n) Proc. London Math. Soc vol. 43 (1937) 241–254
The FLINT team, “partitions.h (module overview),” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/partitions/index.html (generated 2026-10-20).
@misc{flint-doc-partitions-index,
author = {{The FLINT team}},
title = {{partitions.h (module overview)}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/partitions/index.html},
note = {Generated 2026-10-20}
}