Complete factorisation
#include <flint/ulong_extras.h>n_factor_tA factorisation p_1^{e_1} \cdots p_k^{e_k} into distinct primes, with fields num (the number k of distinct primes), p[] (the primes) and exp[] (the exponents).
void n_factor_init(n_factor_t * factors)sourceInitialises factors to the empty factorisation.
void n_factor(n_factor_t * factors, ulong n, int proved)sourceSets factors to the complete factorisation of n (empty for n \le 1); factors need not be initialised. The flag proved has no effect, as all primality tests used are deterministic for word-size integers.
n_factor is the function to use for factoring a word-size integer. It stores the result in an n_factor_t, which has room for the at most 15 distinct prime factors of a 64-bit integer. The primes are not necessarily in increasing order.
The structure has fixed size and needs no memory management: n_factor_init only sets num to zero, and there is no clear function. n_factor overwrites its output, so the initialisation is only needed before functions that add to a factorisation, such as n_factor_trial and n_factor_insert.
Algorithm#
After removing the power of 2, n_factor trial-divides by the primes below 2^{16}, testing the cofactor for primality early on so that numbers with a large prime factor are not trial-divided needlessly. Any remaining composite has two or three prime factors above 2^{16}. These are split by Pollard–Brent rho, Hart's one-line method or SQUFOF, depending on the size, after checking for perfect squares, cubes and fifth powers.
n_factor combines trial division, primality testing, perfect power detection, Pollard–Brent rho, Hart's one-line method and SQUFOF to factor any 64-bit integer.
Integers of more than one word are factored by fmpz_factor; the methods for them are compared in Factoring integers.
Related functions#
n_factor_partialstops once the product of the factors found exceeds a limit;n_factor_trialonly does trial division.n_factor_evaluatemultiplies a factorisation back out, andn_factor_insertadds a prime power to one.
Example#
#include <flint/flint.h>
#include <flint/ulong_extras.h>
int main(void)
{
ulong values[] = { 360, 3215031751, 4294967295, 4294967291 };
n_factor_t fac;
int i, j;
for (i = 0; i < 4; i++)
{
n_factor_init(&fac);
n_factor(&fac, values[i], 1);
flint_printf("%wu =", values[i]);
for (j = 0; j < fac.num; j++)
{
flint_printf(" %wu", fac.p[j]);
if (fac.exp[j] > 1)
flint_printf("^%d", fac.exp[j]);
if (j + 1 < fac.num)
flint_printf(" *");
}
flint_printf("\n");
}
return 0;
}360 = 2^3 * 3^2 * 5 3215031751 = 151 * 751 * 28351 4294967295 = 3 * 5 * 17 * 257 * 65537 4294967291 = 4294967291
The FLINT team, “Complete factorisation (ulong_extras.h),” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/ulong_extras/factor.html (generated 2026-10-20).
@misc{flint-doc-ulong-extras-factor,
author = {{The FLINT team}},
title = {{Complete factorisation (ulong\_extras.h)}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/ulong_extras/factor.html},
note = {Generated 2026-10-20}
}