FLINT3.7.0-dev
Module · 5 functions

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.

AlgorithmComputing the partition functionComputes p(n) exactly by the Hardy–Ramanujan–Rademacher series, truncated with Rademacher's error bound, with the exponential sums factored into products of cosines and each term evaluated in ball arithmetic at the precision it needs; small values come from Euler's pentagonal recurrence.

The partition function#

void partitions_fmpz_fmpz(fmpz_t p, const fmpz_t n, int use_doubles)

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).

Full documentation

Bounds and approximations#

Sets b to an upper bound for

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),

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),

\frac{\sqrt{12}}{24n - 1} \left(1 - \frac{1}{t}\right) e^{t}, \qquad t = \frac{\pi \sqrt{24n - 1}}{6},

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#

Cite this page

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).