FLINT3.7.0-dev

n_is_prime_pseudosquare

Synopsis#include <flint/ulong_extras.h>
int n_is_prime_pseudosquare(ulong n)source

Primality test based on pseudosquares, following Theorem 2.7 of [LukPatWil1996].

n_is_prime_pseudosquare first trial-divides nn by up to FLINT_PSEUDOSQUARES_CUTOFF primes; if no factor is found and the square of the last prime does not exceed nn, let B0B_0 be the next prime. The first pseudosquare Lp>n/B0L_p > n/B_0 is then looked up in a static table (OEIS A002189), and step 3 of the test checks that pj(n−1)/2≡±1(modn)p_j^{(n-1)/2} \equiv \pm 1 \pmod n for the primes pjp_j up to pp. Every prime passes this, and most composites fail. If n≡3,7(mod8)n \equiv 3, 7 \pmod 8, nn is then prime. Otherwise step 4 proves primality: for n≡5(mod8)n \equiv 5 \pmod 8 by checking 2(n−1)/2≡−1(modn)2^{(n-1)/2} \equiv -1 \pmod n, and for n≡1(mod8)n \equiv 1 \pmod 8 by finding some pjp_j with pj(n−1)/2≡−1(modn)p_j^{(n-1)/2} \equiv -1 \pmod n, trying further primes if necessary. Step 4 cannot fail for prime nn; if it ever fails, an exception is thrown, since a composite passing step 3 would be of independent interest.

References#

  • [LukPatWil1996] R. F. Lukes and C. D. Patterson and H. C. Williams "Some results on pseudosquares" Math. Comp. 1996, no. 65, 361–372
Cite this page

The FLINT team, “n_is_prime_pseudosquare (ulong_extras.h),” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/ulong_extras/n_is_prime_pseudosquare.html (generated 2026-11-02).