Node:Number Theoretic Functions, Next:, Previous:Integer Roots, Up:Integer Functions



Number Theoretic Functions

int mpz_probab_prime_p (mpz_t n, int reps) Function
Determine whether n is prime. Return 2 if n is definitely prime, return 1 if n is probably prime (without being certain), or return 0 if n is definitely composite.

This function does some trial divisions, then some Miller-Rabin probabilistic primality tests. reps controls how many such tests are done, 5 to 10 is a reasonable number, more will reduce the chances of a composite being returned as "probably prime".

Miller-Rabin and similar tests can be more properly called compositeness tests. Numbers which fail are known to be composite but those which pass might be prime or might be composite. Only a few composites pass, hence those which pass are considered probably prime.

void mpz_nextprime (mpz_t rop, mpz_t op) Function
Set rop to the next prime greater than op.

This function uses a probabilistic algorithm to identify primes. For practical purposes it's adequate, the chance of a composite passing will be extremely small.

void mpz_gcd (mpz_t rop, mpz_t op1, mpz_t op2) Function
Set rop to the greatest common divisor of op1 and op2. The result is always positive even if one or both input operands are negative.

unsigned long int mpz_gcd_ui (mpz_t rop, mpz_t op1, unsigned long int op2) Function
Compute the greatest common divisor of op1 and op2. If rop is not NULL, store the result there.

If the result is small enough to fit in an unsigned long int, it is returned. If the result does not fit, 0 is returned, and the result is equal to the argument op1. Note that the result will always fit if op2 is non-zero.

void mpz_gcdext (mpz_t g, mpz_t s, mpz_t t, mpz_t a, mpz_t b) Function
Set g to the greatest common divisor of a and b, and in addition set s and t to coefficients satisfying a*s + b*t = g. g is always positive, even if one or both of a and b are negative.

If t is NULL then that value is not computed.

void mpz_lcm (mpz_t rop, mpz_t op1, mpz_t op2) Function
void mpz_lcm_ui (mpz_t rop, mpz_t op1, unsigned long op2) Function
Set rop to the least common multiple of op1 and op2. rop is always positive, irrespective of the signs of op1 and op2. rop will be zero if either op1 or op2 is zero.

int mpz_invert (mpz_t rop, mpz_t op1, mpz_t op2) Function
Compute the inverse of op1 modulo op2 and put the result in rop. If the inverse exists, the return value is non-zero and rop will satisfy 0 <= rop < op2. If an inverse doesn't exist the return value is zero and rop is undefined.

int mpz_jacobi (mpz_t a, mpz_t b) Function
Calculate the Jacobi symbol (a/b). This is defined only for b odd.

int mpz_legendre (mpz_t a, mpz_t p) Function
Calculate the Legendre symbol (a/p). This is defined only for p an odd positive prime, and for such p it's identical to the Jacobi symbol.

int mpz_kronecker (mpz_t a, mpz_t b) Function
int mpz_kronecker_si (mpz_t a, long b) Function
int mpz_kronecker_ui (mpz_t a, unsigned long b) Function
int mpz_si_kronecker (long a, mpz_t b) Function
int mpz_ui_kronecker (unsigned long a, mpz_t b) Function
Calculate the Jacobi symbol (a/b) with the Kronecker extension (a/2)=(2/a) when a odd, or (a/2)=0 when a even.

When b is odd the Jacobi symbol and Kronecker symbol are identical, so mpz_kronecker_ui etc can be used for mixed precision Jacobi symbols too.

For more information see Henri Cohen section 1.4.2 (see References), or any number theory textbook. See also the example program demos/qcn.c which uses mpz_kronecker_ui.

unsigned long int mpz_remove (mpz_t rop, mpz_t op, mpz_t f) Function
Remove all occurrences of the factor f from op and store the result in rop. Return the multiplicity of f in op.

void mpz_fac_ui (mpz_t rop, unsigned long int op) Function
Set rop to op!, the factorial of op.

void mpz_bin_ui (mpz_t rop, mpz_t n, unsigned long int k) Function
void mpz_bin_uiui (mpz_t rop, unsigned long int n, unsigned long int k) Function
Compute the binomial coefficient n over k and store the result in rop. Negative values of n are supported by mpz_bin_ui, using the identity bin(-n,k) = (-1)^k * bin(n+k-1,k), see Knuth volume 1 section 1.2.6 part G.

void mpz_fib_ui (mpz_t fn, unsigned long int n) Function
void mpz_fib2_ui (mpz_t fn, mpz_t fnsub1, unsigned long int n) Function
mpz_fib_ui sets fn to to F[n], the n'th Fibonacci number. mpz_fib2_ui sets fn to F[n], and fnsub1 to F[n-1].

These functions are designed for calculating isolated Fibonacci numbers. When a sequence of values is wanted it's best to start with mpz_fib2_ui and iterate the defining F[n+1]=F[n]+F[n-1] or similar.

void mpz_lucnum_ui (mpz_t ln, unsigned long int n) Function
void mpz_lucnum2_ui (mpz_t ln, mpz_t lnsub1, unsigned long int n) Function
mpz_lucnum_ui sets ln to to L[n], the n'th Lucas number. mpz_lucnum2_ui sets ln to L[n], and lnsub1 to L[n-1].

These functions are designed for calculating isolated Lucas numbers. When a sequence of values is wanted it's best to start with mpz_lucnum2_ui and iterate the defining L[n+1]=L[n]+L[n-1] or similar.

The Fibonacci numbers and Lucas numbers are related sequences, so it's never necessary to call both mpz_fib2_ui and mpz_lucnum2_ui. The formulas for going from Fibonacci to Lucas can be found in Lucas Numbers Algorithm, the reverse is straightforward too.