Module _gmp.gmpz¶
GMP Integers¶
Object mpz¶
- class _gmp.gmpz.mpz([int|float|str[, base=0]|mpz])¶
- Parameters:
(optional) –
This argument is either an
int, afloat, astr, or ampz.float: must fit into a C
double.str: base must be
0or in range[2-62].
- Returns:
A new
mpzobject.- Raises:
Some examples of
mpzobject construction:>>> z = mpz() # C equiv.: mpz_t z; mpz_init(z); >>> z = mpz(2026) >>> z = mpz(3.14) # C equiv.: mpz_t z; mpz_init_set_d(3.14); >>> z = mpz('10001011', 2) # C equiv.: mpz_t z; mpz_init_set_str('10001011', 2); >>> u = mpz(z) >>> print(u) 2026
Properties of the mpz object¶
Operators¶
Operation |
Result |
Notes |
|---|---|---|
x + y |
sum of x and y |
(1) |
x - y |
difference of x and y |
(1) |
x * y |
product of x and y |
(1) |
x // y |
floored quotient of x and y |
(1) |
x % y |
remainder of |
(1) |
-x |
x negated |
|
+x |
x unchanged |
|
abs(x) |
absolute value of x |
|
int(x) |
x converted to Python integer |
|
divmod(x, y) |
the pair |
(1) |
pow(x, y, z=None) |
x ** y (mod z) if z != None |
(2) |
x ** y |
x to the power y |
(3) |
Notes:
All operands are either instances of
mpzorint, except forz, which can beǸone, thenpow(x, y, None)is equivalent tox ** y. In the latter case,ymust be an instance ofintthat must fit into a Cunsigned long.Operand
xis an instance ofmpzandyis an instance ofintthat must fit into a Cunsigned long.
Note
All corresponding in-place operators are therefore
available, namely: +=, -=, *=, etc.
Number Theoretic Functions¶
Note
In the following, z denotes a generic mpz
object.
- z.probab_prime_p([reps=24])¶
Python wrapper for GNU MP
mpz_probab_prime_p()function.- Parameters:
reps – Number of Miller=Rabin probabilistic primality tests. Must be in range
[15-50], otherwise raisesValueError.- Returns:
2ifzis definitely prime, returns0ifzis probably prime (\(p\leq4^{=reps}\)), or returns0ifzis composite.- Raises:
- z.nextprime()¶
Python wrapper for GNU MP
mpz_nextprime()function.- Returns:
next prime greater than
z.
- z.prevprime()¶
Python wrapper for GNU MP
mpz_prevprime()function.
- z.sqrt()¶
Python wrapper for GNU MP
mpz_sqrt()function.- Returns:
\(\lfloor\sqrt z\rfloor\)
- Raises:
- z.sqrtrem()¶
Python wrapper for GNU MP
mpz_sqrtrem()function.- Returns:
(root, rem), whererootis \(\lfloor\sqrt z\rfloor\) andremis \((z-root^2)\).- Raises:
- z.root(n)¶
Python wrapper for GNU MP
mpz_root()function.
- z.rootrem(n)¶
Python wrapper for GNU MP
mpz_rootrem()function.- Parameters:
n – an instance of
intthat must fit into a Cunsigned long.- Returns:
(root, rem), whererootis \(\lfloor\sqrt[n]z\rfloor\) andremis \((z-root^n)\).- Raises:
- z.perfect_power_p()¶
Python wrapper for GNU MP
mpz_perfect_power_p()function.
Bitwise Operations¶
Operation |
Result |
Notes |
|---|---|---|
x | y |
bitwise or of x and y |
(1) |
x ^ y |
bitwise exclusive or of x and y |
(1) |
x & y |
bitwise and of x and y |
(1) |
x << n |
x shifted left by n bits |
(2) |
x >> n |
x shifted right by n bits |
(2) |
~x |
the bits of x inverted |
Notes:
Note
All corresponding in-place operators are therefore
available, namely: |=, ^=, &=, <<= and
>>=.
Other Bitwise Operations¶
Let z be an instance of mpz, then the following
methods are applicable to z:
- z.setbit(bit_index)¶
Python wrapper for GNU MP
mpz_setbit()function. Set bit bit_index inz.
- z.clrbit(bit_index)¶
Python wrapper for GNU MP
mpz_clrbit()function. Clear bit bit_index inz.
- z.combit(bit_index)¶
Python wrapper for GNU MP
mpz_combit()function. Complement bit bit_index inz.
- z.tstbit(bit_index)¶
Python wrapper for GNU MP
mpz_combit()function. Test bit bit_index inzand returnFalseorTrueaccordingly.
- z.popcount()¶
Python wrapper for GNU MP
mpz_popcount()function.- Returns:
If
z > 0, returns the number of1bits in the binary representation ofz. Ifz < 0, the number of1is infinite, and the return value is the largest possible value of aunsigned long.
- z.scan0(starting_bit=0)¶
Python wrapper for GNU MP
mpz_scan0()function. Scanz, starting from bitstarting bit, towards more significant bits, until the first0bit is found. Return the index of the found bit.
- z.scan1(starting_bit=0)¶
Python wrapper for GNU MP
mpz_scan0()function. Scanz, starting from bitstarting bit, towards more significant bits, until the first1bit is found. Return the index of the found bit.
Some examples:
>>> z = mpz()
>>> z.setbit(31)
>>> print(z)
2147483648
>>> z.clrbit(31)
>>> print(z)
0
>>> z.combit(15)
>>> print(z, z.tstbit(15))
32768 True
>>> z = mpz(0x8fffffff)
>>> print(z.popcount()
29
>>> print((-z).popcount())
18446744073709551615
>>> z = mpz(0x8fffff)
>>> print(z.scan0())
20
>>> z = mpz(1 << 32)
>>> print(z.scan1())
32
Miscellaneous methods¶
Let z be an instance of mpz:
- z.sizeinbase(base=10)¶
Python wrapper for GNU MP
mpz_sizeinbase()function.- Parameters:
base – a
intin range[2-62].- Returns:
The size of
zmeasured in number of digits in the givenbase.- Raises:
- z.get_str(base=10)¶
Python wrapper for GNU MP
mpz_get_str()function.- Parameters:
base – a
intin interval[2, 62]or in interval[-36, -2].- Returns:
The conversion of
zto a string (str) of digits in basebase.- Raises:
Note
The prefix
0b(respectively:0o,0x) is added when the base is2(respectively:8,16).>>> z = mpz(2000) >>> print(z.get_str(7)) 5555 >>> print(z.get_str(16)) 0x7d0 >>> print(z.get_str(2)) 0b11111010000
Comparisons and Booleans¶
Let x and y be two objects that are either instances of
mpz or int. Then all comparison operations are
valid: x < y, x <= y, x > y, x >= y, x ==
y, x != y.
Instructions such as if x: and if not x: are valid and
have their usual meaning.
Some examples:
>>> x = mpz(10)
>>> y = mpz(5)
>>> x < y
False
>>> x == 2 * y
True
>>> -y >= -5
True
>>> 11 > x
True
>>> -2 * -y == x
True
Attributes¶
Note
In the following, z denotes a generic mpz
object.
- z.sgn¶
z.sgnis the sign ofz: +1 ifz > 0, 0 ifz == 0, and −1 ifz < 0.
Module Methods¶
- _gmp.gmpz.gcd(z1, z2)¶
Python wrapper for GNU MP
mpz_gcd()function.
- _gmp.gmpz.gcdext(z1, z2)¶
Python wrapper for GNU MP
mpz_gcdext()function.- Parameters z1, z2:
- Returns:
(g, s, t)wheregis the greatest common divisor ofz1andz2and the pair(s, t)that satisfies the equation: \(g=sz_1+tz_2\).- Raises:
>>> print(gcdext(1728, 356)) (4, 41, -199) # 4 = 41*1728 - 199*356
- _gmp.gmpz.lcm(z1, z2)¶
Python wrapper for GNU MP
mpz_lcm()function.
- _gmp.gmpz.divisible_p(z, d)¶
Python wrapper for GNU MP
mpz_divisible_p()andmpz_divisible_ui_p()functions.
- _gmp.gmpz.divisible_2exp_p(z, d)¶
Python wrapper for GNU MP
mpz_divisible_2exp_p()function.
- _gmp.gmpz.congruent_p(z, c, d)¶
Python wrapper for GNU MP
mpz_congruent_p()andmpz_congruent_ui_p()functions.
- _gmp.gmpz.congruent_2exp_p(z, c, d)¶
Python wrapper for GNU MP
mpz_congruent_2exp_p()function.
- _gmp.gmpz.jacobi(a, b)¶
Python wrapper for GNU MP
mpz_jacobi()function.- Parameters a, b:
aandbare instances ofmpzorint.bmust be odd, otherwiseValueErroris raised.- Returns:
The jacobi symbol \(\left(\dfrac{a}{b}\right)\) of
aandb.- Raises:
- _gmp.gmpz.legendre(a, b)¶
Python wrapper for GNU MP
mpz_legendre()function.- Parameters a, b:
aandbare instances ofmpzorint.bmust be an odd positive prime, otherwiseValueErroris raised.- Returns:
The legendre symbol \(\left(\dfrac{a}{b}\right)\) of
aandb.- Raises:
- _gmp.gmpz.kronecker(a, b)¶
Python wrapper for GNU MP
mpz_kronecker(),mpz_kronecker_si(),mpz_kronecker_ui(),mpz_si_kronecker()andmpz_ui_kronecker()functions.
- _gmp.gmpz.invert(a, b)¶
Python wrapper for GNU MP
mpz_invert()function.- Parameters a, b:
aandbare instances ofmpzorint.bcannot be zero, otherwiseValueErroris raised.- Returns:
The inverse of
amoduloborNoneif it does not exist.`- Raises:
Some examples:
>>> z = mpz(2026) >>> print(invert(1013, z)) None >>> print(invert(-11, z)) 1105
- _gmp.gmpz.fac_ui(n)¶
Python wrapper for GNU MP
mpz_fac_ui()function.
- _gmp.gmpz.fac2_ui(n)¶
Python wrapper for GNU MP
mpz_2fac_ui()function.
- _gmp.gmpz.mfac_uiui(n, m)¶
Python wrapper for GNU MP
mpz_mfac_uiui()function.
- _gmp.gmpz.primorial_ui(n)¶
Python wrapper for GNU MP
mpz_primorial_ui()function.- Parameters n:
a
intthat must fit into a Cunsigned long.- Returns:
\(n\sharp\), the product of all positive prime numbers \(\leq\)
n.- Raises:
>>> print(primorial_ui(10)) 210 # 2*3*5*7
- _gmp.gmpz.bin_ui(n, k)¶
Python wrapper for GNU MP
mpz_bin_ui()andmpz_bin_uiui()functions.
- _gmp.gmpz.remove(z1, z2)¶
Python wrapper for GNU MP
mpz_remove()function.- Parameters z1, z2:
- Returns:
(z, occ)wherezis the numberz1in which all occurrences of the factorz2have been removed andoccis the number of these occurrences.- Raises:
>>> print(remove(1728, 2)) (27, 6)
- _gmp.gmpz.hamdist(z1, z2)¶
Python wrapper for GNU MP
mpz_hamdist()function.
Random Number Functions¶
Note
GMP random number functions use a variable of type
gmp_randstate_t which holds an algorithm selection and a
current state. Such a global (and internal) state variable is
initialized during _gmp.gmpz module initialization via
the GMP function gmp_randinit_default().
- _gmp.gmpz.randseed_ui()¶
Python wrapper for GNU MP
mpz_randseed_ui()function. Set an initial seed value.- Returns:
Ǹone
If the special file
/dev/randomexists on the operating system, seed will be extracted from it. Otherwise, it will be the system’s time that will be used (less secure).
- _gmp.gmpz.urandomb_ui(n)¶
Python wrapper for GNU MP
mpz_urandomb_ui()function.- Parameters n:
A
intthat must be<=(sizeof(unsigned long) << 3), otherwiseValueErroris raised.- Returns:
A random number (
int) ofnbits.- Raises:
- _gmp.gmpz.urandomm_ui(n)¶
Python wrapper for GNU MP
mpz_urandomm_ui()function.
Some examples:
>>> randseed_ui()
>>> print(urandomb_ui(64))
3049390686714079220 # random number of 64 bits
>>> print(urandomm_ui(64))
48 # random number in interval [0, 63]
Exceptions¶
- exception _gmp.gmpz.GMPZError¶