FLINT3.7.0-dev
Module · 70 functions

arith.h

#include <flint/arith.h>

This module computes integer and rational sequences of number theory and combinatorics: Bernoulli and Euler numbers and polynomials, Stirling numbers of both kinds, Bell numbers (also modulo a word-size integer), the partition function, the number of representations as a sum of squares, Ramanujan's tau function, divisor lists and Landau's function. Most functions come in a version for an isolated value and a version for a whole vector, which use different algorithms: a single large value is typically computed from an analytic formula or a multimodular sum, and a vector by a recurrence or by power series arithmetic, often modulo several word-size primes followed by Chinese remaindering.

Several functions are thin wrappers around specialised modules: Bernoulli numbers come from bernoulli, the partition function from partitions, Euler numbers from arb, the Stirling matrices from gr_mat. The algorithms are described in Bernoulli numbers, Computing the partition function and Stirling and Bell numbers, and for the combinatorial numbers in Stirling numbers and Bell numbers. Functions for primorials, divisor sums, the Möbius and Euler functions and the classical orthogonal and cyclotomic polynomials, formerly in this module, are now in fmpz, fmpz_poly and fmpq_poly; the old names remain as macros (see Deprecated aliases).

Harmonic numbers#

Set the result to the harmonic number H_n = \sum_{k=1}^n 1/k, or to 0 for n \le 0. Wrappers of fmpq_harmonic_ui.

Bernoulli numbers and polynomials#

The Bernoulli numbers are defined by x/(e^x - 1) = \sum_{n \ge 0} B_n x^n/n!, so that B_1 = -1/2 and B_n = 0 for odd n \ge 3. The bernoulli module provides the underlying algorithms and a cache.

AlgorithmBernoulli numbersComputes exact Bernoulli numbers from the zeta function (numerically, with the denominator from von Staudt–Clausen), from Harvey's multimodular algorithm, by a reverse zeta iterator with a table of powers for ranges, and by Ramanujan's recursion or power series modulo primes for short vectors.

Set the result to the Bernoulli number B_n, in lowest terms with a positive denominator. Wrappers of bernoulli_fmpq_ui: the value is read from the Bernoulli cache if present and otherwise computed from the zeta function or by the multimodular algorithm.

Set the entries to B_0, B_1, \ldots, B_{n-1}. For n < 700 the function calls _arith_bernoulli_number_vec_recursive, otherwise _arith_bernoulli_number_vec_multi_mod. For long vectors, bernoulli_fmpq_vec_no_cache (which uses the zeta function and several threads) is usually faster.

Sets the numerators and denominators of B_0, \ldots, B_{n-1} using Ramanujan's recurrence, which expresses B_m in terms of the B_k with k \equiv m \pmod 6. Intended for n up to a few thousand.

More

Sets the numerators and denominators of B_0, \ldots, B_{n-1} by power series inversion modulo word-size primes and Chinese remaindering.

More

Sets den to the denominator of B_n in lowest terms: 1 for odd n \ge 3 and n = 0, 2 for n = 1, and for even n \ge 2 the product of the primes p with (p - 1) \mid n, by the von Staudt–Clausen theorem. Values for n \le 178 are read from a table.

Returns b such that |B_n| < 2^{\lfloor b \rfloor}, from |B_n| < 4 \cdot n!/(2\pi)^n and n! \le (n+1)^{n+1} e^{-n}. Odd n are not treated specially. The bound is evaluated in double precision and is not guaranteed for n > 10^{14}.

Sets poly to the Bernoulli polynomial B_n(x) = \sum_{k=0}^n \binom{n}{k} B_k x^{n-k}, computing B_0, \ldots, B_n with _arith_bernoulli_number_vec and scaling by binomial coefficients over a common denominator.

Euler numbers and polynomials#

The Euler numbers are the integers E_n defined by 1/\cosh(t) = \sum_{n \ge 0} E_n t^n/n!. With this convention the odd-indexed numbers are zero and the even ones alternate in sign: E_0, E_1, E_2, \ldots = 1, 0, -1, 0, 5, 0, -61, 0, 1385, \ldots The Euler polynomials are defined by 2e^{xt}/(e^t + 1) = \sum_{n \ge 0} E_n(x) t^n/n!, and E_n = 2^n E_n(1/2).

Sets res to E_n. A wrapper of arb_fmpz_euler_number_ui, which uses a table for small n, the Dirichlet beta function \beta(n+1) for n < 2000 and a combination of the beta function with a multimodular method (as for Bernoulli numbers) for larger n.

Sets res to E_0, E_1, \ldots, E_{n-1}, for n \ge 0.

More

Returns b such that |E_n| < 2^{\lfloor b \rfloor}, from |E_n| < 2^{n+2} n!/\pi^{n+1} and n! \le (n+1)^{n+1} e^{-n}. Odd n are not treated specially. Not guaranteed for n > 10^{14}.

Sets poly to the Euler polynomial E_n(x), using

E_n(x) = \frac{2}{n+1}\left(B_{n+1}(x) - 2^{n+1}B_{n+1}\left(\frac{x}{2}\right)\right)

with B_{n+1}(x) computed once by arith_bernoulli_polynomial and rescaled.

Stirling numbers#

The Stirling numbers of the first kind s(n,k) (signed) and |s(n,k)| (unsigned) and of the second kind S(n,k) are defined by

x(x-1)\cdots(x-n+1) = \sum_{k=0}^n s(n,k)\, x^k, \qquad x(x+1)\cdots(x+n-1) = \sum_{k=0}^n |s(n,k)|\, x^k, \qquad x^n = \sum_{k=0}^n S(n,k)\, x(x-1)\cdots(x-k+1),

and are zero for k > n. |s(n,k)| counts the permutations of n elements with k cycles and S(n,k) the partitions of a set of n elements into k blocks. See Stirling numbers.

Set s to |s(n,k)|, s(n,k) or S(n,k), for any n, k \ge 0. These functions compute an isolated value, with algorithms chosen according to n and k/n (binary splitting, power sums, power series and multimodular methods).

Full documentation

Set row to the values for k = 0, 1, \ldots, \mathit{klen} - 1 of |s(n,k)|, s(n,k) or S(n,k). A full row has \mathit{klen} = n + 1; entries with k > n are set to zero.

Full documentation

Set the entry (n, k) of the r \times c matrix mat to |s(n,k)|, s(n,k) or S(n,k), for 0 \le n < r and 0 \le k < c; the entries above the diagonal are zero. The square matrices of s and S are inverses of each other. Wrappers of gr_mat_stirling, which fills the rows with the triangular recurrences.

Full documentation

Bell numbers#

The Bell number B_n is the number of partitions of a set of n elements, B_n = \sum_{k=0}^n S(n,k), with exponential generating function \sum B_n x^n/n! = e^{e^x - 1}. See Bell numbers.

Set the result to the Bell number B_n. The default version uses a table for n \le 25 (64-bit), the Bell triangle in fixed-size multiword arithmetic for n \le 58, Dobiński's formula for n \le 3400 and the multimodular algorithm above.

Full documentation

Set b to B_0, B_1, \ldots, B_{n-1}. The recursive version uses the Bell triangle (Aitken's array), with O(n^2) additions of numbers of O(n \log n) bits; the multi_mod version computes the vector modulo word-size primes with arith_bell_number_nmod_vec and combines them by Chinese remaindering, in O(n^2 \log^{O(1)} n). The default version uses the recursive algorithm for n < 5000.

Full documentation

Returns B_n modulo the modulus of mod, which can be any integer \ge 1.

Full documentation

Returns b such that B_n < 2^{\lfloor b \rfloor}, from de Bruijn's asymptotic expansion of \log B_n [deB1958] with a margin. The bound is verified for n < 2^{64} but not proved in general; an earlier version used the weaker bound B_n < (0.792n/\log(n+1))^n of [BerTas2010].

Full documentation

The partition function#

The partition function p(n) counts the ways of writing n as a sum of positive integers without regard to order. Its generating function is \sum p(n) x^n = \prod_{k \ge 1} (1 - x^k)^{-1}.

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.

Sets x to p(n). A wrapper of partitions_fmpz_ui, which uses the Hardy–Ramanujan–Rademacher formula for large n.

Set res to p(0), p(1), \ldots, p(\mathit{len} - 1), exactly or modulo the modulus of mod, by inverting Euler's pentagonal series \prod_{k \ge 1} (1 - x^k) = \sum_{j \in \mathbb{Z}} (-1)^j x^{j(3j-1)/2} with _fmpz_poly_inv_series or _nmod_poly_inv_series. The cost is O(M(n)) operations on numbers of O(\sqrt n) bits, or modulo the modulus.

A product of cosines of rational multiples of \pi times an algebraic prefactor, \mathit{prefactor} \cdot \sqrt{\mathit{sqrt\_p} / \mathit{sqrt\_q}} \cdot \prod_{i < \mathit{n}} \cos(\pi\, \mathit{cos\_p}[i] / \mathit{cos\_q}[i]), with the fields named as in the formula (at most FLINT_BITS factors). An array of length one of trig_prod_struct.

Initialises prod to the empty product with prefactor 1. Inline; no memory is allocated.

Multiplies prod (which must be initialised with trig_prod_init) by a symbolic expression for the exponential sum

A_k(n) = \sum_{\substack{0 \le h < k \\ \gcd(h, k) = 1}} \exp\left(\pi i \left[ s(h,k) - \frac{2hn}{k}\right]\right)

of the Hardy–Ramanujan–Rademacher formula, where s(h,k) is a Dedekind sum, as a product of cosines.

More

Sums of squares#

Sets r to the number r_k(n) of representations of n as an ordered sum of k squares of integers (signs and order count), with r_k(0) = 1 and r_k(n) = 0 for n < 0.

More

Sets r_i to r_k(i) for 0 \le i < n, the first n coefficients of

\vartheta_3(q)^k = \Bigl(\sum_{j \in \mathbb{Z}} q^{j^2}\Bigr)^k,

computed from the sparse expansions of \vartheta_3 and \vartheta_3^2 and a truncated power.

Ramanujan's tau function#

void arith_ramanujan_tau(fmpz_t res, const fmpz_t n)

Sets res to \tau(n), the coefficient of q^n in \Delta(q) = q \prod_{k \ge 1} (1 - q^k)^{24}, or to 0 for n \le 0.

More

Sets res to the polynomial with coefficients \tau(0), \tau(1), \ldots, \tau(n-1), using Jacobi's identity

\Delta(q) = q \Biggl( \sum_{k \ge 0} (-1)^k (2k+1) q^{k(k+1)/2} \Biggr)^8,

evaluated with three squarings of which the first, of a very sparse series, is done directly.

Divisors#

void arith_divisors(fmpz_poly_t res, const fmpz_t n)

Sets the coefficients of res to the positive divisors of |n| in increasing order (so res has length \sigma_0(n)), and to the zero polynomial if n = 0. Uses a table for |n| < \mathtt{FLINT\_BITS} and otherwise factors n with fmpz_factor and generates the products of prime powers.

Landau's function#

Sets res to the first len values g(0), g(1), \ldots of Landau's function, the largest order of an element of the symmetric group S_n, equivalently the largest \operatorname{lcm}(n_1, \ldots, n_r) with n_1 + \cdots + n_r \le n.

More

Tables#

The Bell numbers B_n for n < BELL_NUMBER_TAB_SIZE (26 on 64-bit machines, 16 on 32-bit machines).

const unsigned int partitions_lookup[128]

The values p(n) for n < 128, all below 2^{32}.

The absolute values |E_{2k}| for 2k < SMALL_EULER_LIMIT (25 on 64-bit machines) and the numerators of B_{2k} for 2k < BERNOULLI_SMALL_NUMER_LIMIT (35 on 64-bit machines). Static arrays defined in the header.

Deprecated aliases#

These macros map old names of functions that have moved to other modules. New code should use the targets.

References#

  • [deB1958] N. G. de Bruijn, Asymptotic Methods in Analysis, North-Holland, 1958 (§6.2: Bell numbers).
  • [BerTas2010] D. Berend and T. Tassa : Improved bounds on Bell numbers and on moments of sums of random variables, Probability and Mathematical Statistics vol. 30 (2010) 185–205
Cite this page

The FLINT team, “arith.h (module overview),” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/arith/index.html (generated 2026-10-20).