Computing p(n)
#include <flint/partitions.h>Set to , which is 0 for . 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).
Values with are read from a table, values with (where , on 64-bit machines) are computed with Euler's pentagonal recurrence in single words, and larger values with mp_real_partitions_hrr. The number of threads set with flint_set_num_threads is used from about . partitions_fmpz_fmpz accepts and throws for larger ; the result must also fit in an fmpz, which limits to about on 64-bit machines. Beyond, use mp_real_partitions_hrr directly.
partitions_fmpz_ui and partitions_fmpz_fmpz compute one value of the partition function exactly, in time roughly proportional to the size of the result, . They are the functions to use for a single with beyond a few thousand. For all values up to , arith_number_of_partitions_vec (power series inversion, for the whole vector) is cheaper than separate calls; for modulo a word-size integer and all up to a bound, arith_number_of_partitions_nmod_vec.
Methods#
| Range of | Method |
|---|---|
table partitions_lookup | |
| Euler's pentagonal recurrence in single words (64-bit machines), | |
Hardy–Ramanujan–Rademacher formula, mp_real_partitions_hrr |
The formula is evaluated as a ball whose radius covers the rounding errors of every term and Rademacher's bound for the truncation error; the function returns the unique integer in the ball (Integers from balls). It never returns an incorrect value: if the ball contained no integer or several, it would throw, which is not expected to happen. The terms are computed in parallel from about when threads are enabled with flint_set_num_threads.
The limits are those of the data types: partitions_fmpz_fmpz throws for , and the result must fit in an fmpz, whose size is limited to about bits on 64-bit machines, which holds up to . The memory needed is a small multiple of the size of the result, which is about bytes.
Example#
The program prints and , computes and compares it with the leading term of the series. The relative difference is of the order of , where , which for is about ; at 128 bits the ratio is 1 up to the rounding error.
#include <flint/flint.h>
#include <flint/fmpz.h>
#include <flint/arb.h>
#include <flint/partitions.h>
int main(void)
{
fmpz_t p, n;
arb_t lead, x;
fmpz_init(p);
fmpz_init(n);
arb_init(lead);
arb_init(x);
partitions_fmpz_ui(p, 100);
flint_printf("p(100) = ");
fmpz_print(p);
partitions_fmpz_ui(p, 1000);
flint_printf("\np(1000) = ");
fmpz_print(p);
flint_printf("\n");
fmpz_set_ui(n, 1000000000);
partitions_fmpz_fmpz(p, n, 0);
flint_printf("p(10^9) has %wu bits\n", fmpz_bits(p));
partitions_leading_fmpz(lead, n, 128);
arb_set_fmpz(x, p);
arb_div(x, x, lead, 128);
flint_printf("p(10^9) / leading term = ");
arb_printn(x, 25, 0);
flint_printf("\n");
fmpz_clear(p);
fmpz_clear(n);
arb_clear(lead);
arb_clear(x);
return 0;
}p(100) = 190569292 p(1000) = 24061467864032622473692149727991 p(10^9) has 116993 bits p(10^9) / leading term = [1.000000000000000000000000 +/- 9.48e-38]
The FLINT team, “Computing p(n) (partitions.h),” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/partitions/p.html (generated 2026-11-02).
@misc{flint-doc-partitions-p,
author = {{The FLINT team}},
title = {{Computing p(n) (partitions.h)}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/partitions/p.html},
note = {Generated 2026-11-02}
}