Show Menu
Cheatography

GNU Multiple Precision - GNU MP - GMP Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Header

#include<gmp.h>

Data Types

mpz_t
multiple precision integer
mpq_t
multiple precision fraction
mpf_t
float is an arbitrary precision mantissa with a limited precision exponent
mp_exp_t
floating point functions accept and return exponents in this type
mp_limb_t
a limb means the part of a multi-­pre­cision number that ts in a single machine word (normally 32bit or 64bit)
mp_size_t
counts of limbs of a multi-­pre­cision number repres­ented
mp_bit­cnt_t
counts of bits of a multi-­pre­cision number
gmp_ra­nds­tate_t
random state means an algorithm selection and current state data

Function Class prefixes

mpz_
Functions for signed integer arithmetic
mpq_
Functions for rational number arithmetic
mpf_
Functions for floati­ng-­point arithmetic
mpn_
Fast low-level functions that operate on natural numbers. These are used by the functions in the preceding groups, and you can also call them directly from very time-c­ritical user programs.

Initia­liz­ation Functions

void mpz_init (mpz_t x)
Initialize x, and set its value to 0.
void mpq_init (mpq_t x)
Initialize x and set it to 0/1.
void mpf_init (mpf_t x)
Initialize x to 0.
void mp[z,q­,f]­_inits (mp[z,q,f] x, ...)
Initialize a NULL-t­erm­inated list of mp[z,q­,f]_t variables, and set their values to 0.
void mp[z,q­,f]­_clear (mp[z,­q,f]_t x)
Free the space occupied by x. Call this function for all mp[z,q­,f]_t variables when you are done with them.
void mp[z,q­,f]­_clears (mp[z,­q,f]_t x, ...)
Free the space occupied by a NULL-t­erm­inated list of mp[z,q­,f]_t variables.

Assignment Functions

void mpz_set (mpz_t rop, const mpz_t op)
Set the value of rop from op.
void mpz_set_ui (mpz_t rop, unsigned long int op)
"
void mpz_set_si (mpz_t rop, signed long int op)
"
void mpz_set_d (mpz_t rop, double op)
truncate
void mpz_set_q (mpz_t rop, const mpq_t op)
"
void mpz_set_f (mpz_t rop, const mpf_t op)
"
void mpq_set (mpq_t rop, const mpq_t op)
Assign rop from op.
void mpq_set_z (mpq_t rop, const mpz_t op)
"
void mpq_set_ui (mpq_t rop, unsigned long int op1, unsigned long int op2)
Set the value of rop to op1/op2. Note that if op1 and op2 have common factors, rop has to be passed to mpq_ca­non­icalize before any operations are performed on rop.
void mpq_set_si (mpq_t rop, signed long int op1, unsigned long int op2)
"
void mpf_set (mpf_t rop, const mpf_t op)
Set the value of rop from op.
void mpf_set_ui (mpf_t rop, unsigned long int op)
"
void mpf_set_si (mpf_t rop, signed long int op)
"
void mpf_set_d (mpf_t rop, double op)
"
void mpf_set_z (mpf_t rop, const mpz_t op)
"
void mpf_set_q (mpf_t rop, const mpq_t op)
"
int mpz_se­t_str (mpz_t rop, const char *str, int base)
Set the value of rop from str, a null-t­erm­inated C string in base base. White space is allowed in the string, and is simply ignored. Should return 0, if error returns -1.
int mpq_se­t_str (mpq_t rop, const char *str, int base)
Set rop from a null-t­erm­inated string str in the given base. Should return 0, if error returns -1.
int mpf_se­t_str (mpf_t rop, const char *str, int base)
et the value of rop from the string in str. Should return 0, if error returns -1.
void mpz_swap (mpz_t rop1, mpz_t rop2)
Swap the values rop1 and rop2 effici­ently.
void mpq_swap (mpq_t rop1, mpq_t rop2)
Swap the values rop1 and rop2 effici­ently.
void mpf_swap (mpf_t rop1, mpf_t rop2)
Swap rop1 and rop2 effici­ently. Both the values and the precisions of the two variables are swapped.

Conversion Functions

unsigned long int mpz_get_ui (const mpz_t op)
Return the value of op as an unsigned long.
signed long int mpz_get_si (const mpz_t op)
If op fits into a signed long int return the value of op. Otherwise return the least signif­icant part of op, with the same sign as op.
double mpz_get_d (const mpz_t op)
Convert op to a double, truncating if necessary (i.e. rounding towards zero).
double mpz_ge­t_d­_2exp (signed long int *exp, const mpz_t op)
Convert op to a double, truncating if necessary (i.e. rounding towards zero), and returning the exponent separa­tely.
char mpz_ge­t_str (char str, int base, const mpz_t op)
Convert op to a string of digits in base base. The base argument may vary from 2 to 62 or from -2 to -36.
double mpq_get_d (const mpq_t op)
Convert op to a double, truncating if necessary (i.e. rounding towards zero).
void mpq_set_d (mpq_t rop, double op)
Set rop to the value of op. There is no rounding, this conversion is exact.
void mpq_set_f (mpq_t rop, const mpf_t op)
Set rop to the value of op. There is no rounding, this conversion is exact.
char mpq_ge­t_str (char str, int base, const mpq_t op)
Convert op to a string of digits in base base. The base may vary from 2 to 36. The string will be of the form ‘num/den’, or if the denomi­nator is 1 then just ‘num’.
double mpf_get_d (const mpf_t op)
Convert op to a double, truncating if necessary (i.e. rounding towards zero).
double mpf_ge­t_d­_2exp (signed long int *exp, const mpf_t op)
Convert op to a double, truncating if necessary (i.e. rounding towards zero), and with an exponent returned separa­tely.
long mpf_get_si (const mpf_t op)
Convert op to a long or unsigned long, truncating any fraction part. If op is too big for the return type, the result is undefined.
unsigned long mpf_get_ui (const mpf_t op)
Convert op to a long or unsigned long, truncating any fraction part. If op is too big for the return type, the result is undefined.
char mpf_ge­t_str (char str, mp_exp_t *expptr, int base, size_t n_digits, const mpf_t op)
Convert op to a string of digits in base base. The base argument may vary from 2 to 62 or from -2 to -36. Up to n_digits digits will be generated. Trailing zeros are not returned. No more digits than can be accurately repres­ented by op are ever generated. If n_digits is 0 then that accurate maximum number of digits are generated.

Arithmetic Functions

void mpz_add (mpz_t rop, const mpz_t op1, const mpz_t op2)
Set rop to op1 + op2.
void mpz_add_ui (mpz_t rop, const mpz_t op1, unsigned long int op2)
Set rop to op1 + op2.
void mpz_sub (mpz_t rop, const mpz_t op1, const mpz_t op2)
Set rop to op1 - op2.
void mpz_sub_ui (mpz_t rop, const mpz_t op1, unsigned long int op2)
Set rop to op1 - op2.
void mpz_ui_sub (mpz_t rop, unsigned long int op1, const mpz_t op2)
Set rop to op1 - op2.
void mpz_mul (mpz_t rop, const mpz_t op1, const mpz_t op2)
Set rop to op1 times op2.
void mpz_mul_si (mpz_t rop, const mpz_t op1, long int op2)
Set rop to op1 times op2.
void mpz_mul_ui (mpz_t rop, const mpz_t op1, unsigned long int op2)
Set rop to op1 times op2.
void mpz_addmul (mpz_t rop, const mpz_t op1, const mpz_t op2)
Set rop to rop + op1 times op2.
void mpz_ad­dmul_ui (mpz_t rop, const mpz_t op1, unsigned long int op2)
Set rop to rop + op1 times op2.
void mpz_submul (mpz_t rop, const mpz_t op1, const mpz_t op2)
Set rop to rop - op1 times op2.
void mpz_su­bmul_ui (mpz_t rop, const mpz_t op1, unsigned long int op2)
Set rop to rop - op1 times op2.
void mpz_mu­l_2exp (mpz_t rop, const mpz_t op1, mp_bit­cnt_t op2)
Set rop to op1 times 2 raised to op2. This operation can also be defined as a left shift by op2 bits.
void mpz_neg (mpz_t rop, const mpz_t op)
Set rop to -op.
void mpz_abs (mpz_t rop, const mpz_t op)
Set rop to the absolute value of op.
 
void mpq_add (mpq_t sum, const mpq_t addend1, const mpq_t addend2)
Set sum to addend1 + addend2.
void mpq_sub (mpq_t differ­ence, const mpq_t minuend, const mpq_t subtra­hend)
Set difference to minuend - subtra­hend.
void mpq_mul (mpq_t product, const mpq_t multip­lier, const mpq_t multip­licand)
Set product to multiplier times multip­licand.
void mpq_mu­l_2exp (mpq_t rop, const mpq_t op1, mp_bit­cnt_t op2)
Set rop to op1 times 2 raised to op2. This operation can also be defined as a left shift by op2 bits.
void mpq_div (mpq_t quotient, const mpq_t dividend, const mpq_t divisor)
Set quotient to divide­nd/­div­isor.
void mpq_di­v_2exp (mpq_t rop, const mpq_t op1, mp_bit­cnt_t op2)
Set rop to op1 divided by 2 raised to op2.
void mpq_neg (mpq_t negate­d_o­perand, const mpq_t operand)
Set negate­d_o­perand to -operand.
void mpq_abs (mpq_t rop, const mpq_t op)
Set rop to the absolute value of op.
void mpq_inv (mpq_t invert­ed_­number, const mpq_t number)
Set invert­ed_­number to 1/number. If the new denomi­nator is zero, this routine will divide by zero.
 
void mpf_add (mpf_t rop, const mpf_t op1, const mpf_t op2)
Set rop to op1 + op2.
void mpf_add_ui (mpf_t rop, const mpf_t op1, unsigned long int op2)
Set rop to op1 + op2.
void mpf_sub (mpf_t rop, const mpf_t op1, const mpf_t op2)
Set rop to op1 - op2.
void mpf_ui_sub (mpf_t rop, unsigned long int op1, const mpf_t op2)
Set rop to op1 - op2.
void mpf_sub_ui (mpf_t rop, const mpf_t op1, unsigned long int op2)
Set rop to op1 - op2.
void mpf_mul (mpf_t rop, const mpf_t op1, const mpf_t op2)
Set rop to op1 times op2.
void mpf_mul_ui (mpf_t rop, const mpf_t op1, unsigned long int op2)
Set rop to op1 times op2.
void mpf_div (mpf_t rop, const mpf_t op1, const mpf_t op2)
Set rop to op1/op2.
void mpf_ui_div (mpf_t rop, unsigned long int op1, const mpf_t op2)
Set rop to op1/op2.
void mpf_div_ui (mpf_t rop, const mpf_t op1, unsigned long int op2)
Set rop to op1/op2.
void mpf_sqrt (mpf_t rop, const mpf_t op)
Set rop to the square root of op.
void mpf_sq­rt_ui (mpf_t rop, unsigned long int op)
Set rop to the square root of op.
void mpf_pow_ui (mpf_t rop, const mpf_t op1, unsigned long int op2)
Set rop to op1 raised to the power op2.
void mpf_neg (mpf_t rop, const mpf_t op)
Set rop to -op.
void mpf_abs (mpf_t rop, const mpf_t op)
Set rop to the absolute value of op.
void mpf_mu­l_2exp (mpf_t rop, const mpf_t op1, mp_bit­cnt_t op2)
Set rop to op1 times 2 raised to op2. This operation can also be defined as a left shift by op2 bits.
void mpf_di­v_2exp (mpf_t rop, const mpf_t op1, mp_bit­cnt_t op2)
Set rop to op1 divided by 2 raised to op2.

Integer Division Functions

void mpz_cdiv_q (mpz_t q, const mpz_t n, const mpz_t d)
void mpz_cdiv_r (mpz_t r, const mpz_t n, const mpz_t d)
void mpz_cd­iv_qr (mpz_t q, mpz_t r, const mpz_t n, const mpz_t d)
unsigned long int mpz_cd­iv_q_ui (mpz_t q, const mpz_t n, unsigned long int d)
unsigned long int mpz_cd­iv_r_ui (mpz_t r, const mpz_t n, unsigned long int d)
unsigned long int mpz_cd­iv_­qr_ui (mpz_t q, mpz_t r, const mpz_t n, unsigned long int d)
unsigned long int mpz_cd­iv_ui (const mpz_t n, unsigned long int d)
void mpz_cd­iv_­q_2exp (mpz_t q, const mpz_t n, mp_bit­cnt_t b)
void mpz_cd­iv_­r_2exp (mpz_t r, const mpz_t n, mp_bit­cnt_t b)
 
void mpz_fdiv_q (mpz_t q, const mpz_t n, const mpz_t d)
void mpz_fdiv_r (mpz_t r, const mpz_t n, const mpz_t d)
void mpz_fd­iv_qr (mpz_t q, mpz_t r, const mpz_t n, const mpz_t d)
unsigned long int mpz_fd­iv_q_ui (mpz_t q, const mpz_t n, unsigned long int d)
unsigned long int mpz_fd­iv_r_ui (mpz_t r, const mpz_t n, unsigned long int d)
unsigned long int mpz_fd­iv_­qr_ui (mpz_t q, mpz_t r, const mpz_t n, unsigned long int d)
unsigned long int mpz_fd­iv_ui (const mpz_t n, unsigned long int d)
void mpz_fd­iv_­q_2exp (mpz_t q, const mpz_t n, mp_bit­cnt_t b)
void mpz_fd­iv_­r_2exp (mpz_t r, const mpz_t n, mp_bit­cnt_t b)
 
void mpz_tdiv_q (mpz_t q, const mpz_t n, const mpz_t d)
void mpz_tdiv_r (mpz_t r, const mpz_t n, const mpz_t d)
void mpz_td­iv_qr (mpz_t q, mpz_t r, const mpz_t n, const mpz_t d)
unsigned long int mpz_td­iv_q_ui (mpz_t q, const mpz_t n, unsigned long int d)
unsigned long int mpz_td­iv_r_ui (mpz_t r, const mpz_t n, unsigned long int d)
unsigned long int mpz_td­iv_­qr_ui (mpz_t q, mpz_t r, const mpz_t n, unsigned long int d)
unsigned long int mpz_td­iv_ui (const mpz_t n, unsigned long int d)
void mpz_td­iv_­q_2exp (mpz_t q, const mpz_t n, mp_bit­cnt_t b)
void mpz_td­iv_­r_2exp (mpz_t r, const mpz_t n, mp_bit­cnt_t b)
 
Divide n by d, forming a quotient q and/or remainder r. For the 2exp functions, d=2^b. The rounding is in three styles, each suiting different applic­ations.
cdiv rounds q up towards +infinity, and r will have the opposite sign to d. The c stands for “ceil”.
 
fdiv rounds q down towards -infinity, and r will have the same sign as d. The f stands for “floor”.
 
tdiv rounds q towards zero, and r will have the same sign as n. The t stands for “trunc­ate”.
In all cases q and r will satisfy n=q*d+r, and r will satisfy 0<=­abs­(r)­<ab­s(d).
The q functions calculate only the quotient, the r functions only the remainder, and the qr functions calculate both. Note that for qr the same variable cannot be passed for both q and r, or results will be unpred­ict­able.
For the ui variants the return value is the remainder, and in fact returning the remainder is all the div_ui functions do. For tdiv and cdiv the remainder can be negative, so for those the return value is the absolute value of the remainder.
For the 2exp variants the divisor is 2^b. These functions are implem­ented as right shifts and bit masks, but of course they round the same as the other functions.
For positive n both mpz_fd­iv_­q_2exp and mpz_td­iv_­q_2exp are simple bitwise right shifts. For negative n, mpz_fd­iv_­q_2exp is effect­ively an arithmetic right shift treating n as twos complement the same as the bitwise logical functions do, whereas mpz_td­iv_­q_2exp effect­ively treats n as sign and magnitude.
 
void mpz_mod (mpz_t r, const mpz_t n, const mpz_t d)
Set r to n mod d. The sign of the divisor is ignored; the result is always non-ne­gative.
unsigned long int mpz_mod_ui (mpz_t r, const mpz_t n, unsigned long int d)
Set r to n mod d. The sign of the divisor is ignored; the result is always non-ne­gative.
 
mpz_mod_ui is identical to mpz_fd­iv_r_ui above, returning the remainder as well as setting r. See mpz_fd­iv_ui above if only the return value is wanted.
 
void mpz_di­vexact (mpz_t q, const mpz_t n, const mpz_t d)
Set q to n/d. These functions produce correct results only when it is known in advance that d divides n.
void mpz_di­vex­act_ui (mpz_t q, const mpz_t n, unsigned long d)
Set q to n/d. These functions produce correct results only when it is known in advance that d divides n.
 
These routines are much faster than the other division functions, and are the best choice when exact division is known to occur, for example reducing a rational to lowest terms.
 
int mpz_di­vis­ible_p (const mpz_t n, const mpz_t d)
Return non-zero if n is exactly divisible by d, or in the case of mpz_di­vis­ibl­e_2­exp_p by 2^b.
int mpz_di­vis­ibl­e_ui_p (const mpz_t n, unsigned long int d)
Return non-zero if n is exactly divisible by d, or in the case of mpz_di­vis­ibl­e_2­exp_p by 2^b.
int mpz_di­vis­ibl­e_2­exp_p (const mpz_t n, mp_bit­cnt_t b)
Return non-zero if n is exactly divisible by d, or in the case of mpz_di­vis­ibl­e_2­exp_p by 2^b.
 
n is divisible by d if there exists an integer q satisfying n = q*d. Unlike the other division functions, d=0 is accepted and following the rule it can be seen that only 0 is considered divisible by 0.
 
int mpz_co­ngr­uent_p (const mpz_t n, const mpz_t c, const mpz_t d)
Return non-zero if n is congruent to c modulo d, or in the case of mpz_co­ngr­uen­t_2­exp_p modulo 2^b.
int mpz_co­ngr­uen­t_ui_p (const mpz_t n, unsigned long int c, unsigned long int d)
Return non-zero if n is congruent to c modulo d, or in the case of mpz_co­ngr­uen­t_2­exp_p modulo 2^b.
int mpz_co­ngr­uen­t_2­exp_p (const mpz_t n, const mpz_t c, mp_bit­cnt_t b)
Return non-zero if n is congruent to c modulo d, or in the case of mpz_co­ngr­uen­t_2­exp_p modulo 2^b.
 
n is congruent to c mod d if there exists an integer q satisfying n = c + q*d. Unlike the other division functions, d=0 is accepted and following the rule it can be seen that n and c are considered congruent mod 0 only when exactly equal.

Integer Expone­nti­ation Functions

void mpz_powm (mpz_t rop, const mpz_t base, const mpz_t exp, const mpz_t mod)
Set rop to (base raised to exp) modulo mod.
void mpz_po­wm_ui (mpz_t rop, const mpz_t base, unsigned long int exp, const mpz_t mod)
Set rop to (base raised to exp) modulo mod.
 
Negative exp is supported if an inverse base^-1 mod mod exists (see mpz_invert in Number Theoretic Functi­ons). If an inverse doesn’t exist then a divide by zero is raised.
 
void mpz_po­wm_sec (mpz_t rop, const mpz_t base, const mpz_t exp, const mpz_t mod)
Set rop to (base raised to exp) modulo mod.
 
It is required that exp > 0 and that mod is odd.
 
This function is designed to take the same time and have the same cache access patterns for any two same-size arguments, assuming that function arguments are placed at the same position and that the machine state is identical upon function entry. This function is intended for crypto­graphic purposes, where resilience to side-c­hannel attacks is desired.
 
void mpz_pow_ui (mpz_t rop, const mpz_t base, unsigned long int exp)
Set rop to base raised to exp. The case 0^0 yields 1.
void mpz_ui­_pow_ui (mpz_t rop, unsigned long int base, unsigned long int exp)
Set rop to base raised to exp. The case 0^0 yields 1.

Integer Root Extraction Functions

int mpz_root (mpz_t rop, const mpz_t op, unsigned long int n)
Set rop to the truncated integer part of the nth root of op. Return non-zero if the comput­ation was exact, i.e., if op is rop to the nth power.
void mpz_ro­otrem (mpz_t root, mpz_t rem, const mpz_t u, unsigned long int n)
Set root to the truncated integer part of the nth root of u. Set rem to the remainder, u-root**n.
void mpz_sqrt (mpz_t rop, const mpz_t op)
Set rop to the truncated integer part of the square root of op.
void mpz_sq­rtrem (mpz_t rop1, mpz_t rop2, const mpz_t op)
Set rop1 to the truncated integer part of the square root of op, like mpz_sqrt. Set rop2 to the remainder op-rop­1*rop1, which will be zero if op is a perfect square.
 
If rop1 and rop2 are the same variable, the results are undefined.
int mpz_pe­rfe­ct_­power_p (const mpz_t op)
Return non-zero if op is a perfect power, i.e., if there exist integers a and b, with b>1, such that op equals a raised to the power b.
 
Under this definition both 0 and 1 are considered to be perfect powers. Negative values of op are accepted, but of course can only be odd perfect powers.
int mpz_pe­rfe­ct_­squ­are_p (const mpz_t op)
Return non-zero if op is a perfect square, i.e., if the square root of op is an integer. Under this definition both 0 and 1 are considered to be perfect squares.

Integer Number Theoretic Functions

int mpz_pr­oba­b_p­rime_p (const mpz_t n, int reps)
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 non-prime.
 
This function performs some trial divisions, then reps Miller­-Rabin probab­ilistic primality tests. A higher reps value will reduce the chances of a non-prime being identified as “probably prime”. A composite number will be identified as a prime with a probab­ility of less than 4^(-reps). Reasonable values of reps are between 15 and 50.
void mpz_ne­xtprime (mpz_t rop, const mpz_t op)
Set rop to the next prime greater than op.
 
This function uses a probab­ilistic 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, const mpz_t op1, const mpz_t op2)
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. Except if both inputs are zero; then this function defines gcd(0,0) = 0.
unsigned long int mpz_gcd_ui (mpz_t rop, const mpz_t op1, unsigned long int op2)
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, const mpz_t a, const mpz_t b)
Set g to the greatest common divisor of a and b, and in addition set s and t to coeffi­cients satisfying as + bt = g. The value in g is always positive, even if one or both of a and b are negative (or zero if both inputs are zero). The values in s and t are chosen such that normally, abs(s) < abs(b) / (2 g) and abs(t) < abs(a) / (2 g), and these relations define s and t uniquely. There are a few except­ional cases:
 
If abs(a) = abs(b), then s = 0, t = sgn(b).
 
Otherwise, s = sgn(a) if b = 0 or abs(b) = 2 g, and t = sgn(b) if a = 0 or abs(a) = 2 g.
 
In all cases, s = 0 if and only if g = abs(b), i.e., if b divides a or a = b = 0.
 
If t is NULL then that value is not computed.
void mpz_lcm (mpz_t rop, const mpz_t op1, const mpz_t op2)
Set rop to the least common multiple of op1 and op2. rop is always positive, irresp­ective of the signs of op1 and op2. rop will be zero if either op1 or op2 is zero.
void mpz_lcm_ui (mpz_t rop, const mpz_t op1, unsigned long op2)
Set rop to the least common multiple of op1 and op2. rop is always positive, irresp­ective of the signs of op1 and op2. rop will be zero if either op1 or op2 is zero.
int mpz_invert (mpz_t rop, const mpz_t op1, const mpz_t op2)
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 < abs(op2) (with rop = 0 possible only when abs(op2) = 1, i.e., in the somewhat degenerate zero ring). If an inverse doesn’t exist the return value is zero and rop is undefined. The behaviour of this function is undefined when op2 is zero.
int mpz_jacobi (const mpz_t a, const mpz_t b)
Calculate the Jacobi symbol (a/b). This is defined only for b odd.
int mpz_le­gendre (const mpz_t a, const mpz_t p)
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_kr­onecker (const mpz_t a, const mpz_t b)
Calculate the Jacobi symbol (a/b) with the Kronecker extension (a/2)=­(2/a) when a odd, or (a/2)=0 when a even.
int mpz_kr­one­cker_si (const mpz_t a, long b)
Calculate the Jacobi symbol (a/b) with the Kronecker extension (a/2)=­(2/a) when a odd, or (a/2)=0 when a even.
int mpz_kr­one­cker_ui (const mpz_t a, unsigned long b)
Calculate the Jacobi symbol (a/b) with the Kronecker extension (a/2)=­(2/a) when a odd, or (a/2)=0 when a even.
int mpz_si­_kr­onecker (long a, const mpz_t b)
Calculate the Jacobi symbol (a/b) with the Kronecker extension (a/2)=­(2/a) when a odd, or (a/2)=0 when a even.
int mpz_ui­_kr­onecker (unsigned long a, const mpz_t b)
Calculate the Jacobi symbol (a/b) with the Kronecker extension (a/2)=­(2/a) when a odd, or (a/2)=0 when a even.
mp_bit­cnt_t mpz_remove (mpz_t rop, const mpz_t op, const mpz_t f)
Remove all occurr­ences of the factor f from op and store the result in rop. The return value is how many such occurr­ences were removed.
void mpz_fac_ui (mpz_t rop, unsigned long int n)
Set rop to the factorial of n: mpz_fac_ui computes the plain factorial n!, mpz_2f­ac_ui computes the double­-fa­ctorial n!!, and mpz_mf­ac_uiui the m-mult­i-f­act­orial n!^(m).
void mpz_2f­ac_ui (mpz_t rop, unsigned long int n)
Set rop to the factorial of n: mpz_fac_ui computes the plain factorial n!, mpz_2f­ac_ui computes the double­-fa­ctorial n!!, and mpz_mf­ac_uiui the m-mult­i-f­act­orial n!^(m).
void mpz_mf­ac_uiui (mpz_t rop, unsigned long int n, unsigned long int m)
Set rop to the factorial of n: mpz_fac_ui computes the plain factorial n!, mpz_2f­ac_ui computes the double­-fa­ctorial n!!, and mpz_mf­ac_uiui the m-mult­i-f­act­orial n!^(m).
void mpz_pr­imo­rial_ui (mpz_t rop, unsigned long int n)
Set rop to the primorial of n, i.e. the product of all positive prime numbers <=n.
void mpz_bin_ui (mpz_t rop, const mpz_t n, unsigned long int k)
Compute the binomial coeffi­cient n over k and store the result in rop. Negative values of n are supported by mpz_bi­n_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_bi­n_uiui (mpz_t rop, unsigned long int n, unsigned long int k)
Compute the binomial coeffi­cient n over k and store the result in rop. Negative values of n are supported by mpz_bi­n_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)
mpz_fib_ui sets fn to to F[n], the n’th Fibonacci number. mpz_fi­b2_ui sets fn to F[n], and fnsub1 to F[n-1].
void mpz_fi­b2_ui (mpz_t fn, mpz_t fnsub1, unsigned long int n)
mpz_fib_ui sets fn to to F[n], the n’th Fibonacci number. mpz_fi­b2_ui sets fn to F[n], and fnsub1 to F[n-1].
 
These functions are designed for calcul­ating isolated Fibonacci numbers. When a sequence of values is wanted it’s best to start with mpz_fi­b2_ui and iterate the defining F[n+1]­=F[­n]+­F[n-1] or similar.
void mpz_lu­cnum_ui (mpz_t ln, unsigned long int n)
mpz_lu­cnum_ui sets ln to to L[n], the n’th Lucas number. mpz_lu­cnu­m2_ui sets ln to L[n], and lnsub1 to L[n-1].
void mpz_lu­cnu­m2_ui (mpz_t ln, mpz_t lnsub1, unsigned long int n)
mpz_lu­cnum_ui sets ln to to L[n], the n’th Lucas number. mpz_lu­cnu­m2_ui sets ln to L[n], and lnsub1 to L[n-1].
 
These functions are designed for calcul­ating isolated Lucas numbers. When a sequence of values is wanted it’s best to start with mpz_lu­cnu­m2_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_fi­b2_ui and mpz_lu­cnu­m2_ui. The formulas for going from Fibonacci to Lucas can be found in Lucas Numbers Algorithm, the reverse is straig­htf­orward too.