/test/test_mpz_functions.txt
Plain Text | 627 lines | 517 code | 110 blank | 0 comment | 0 complexity | b6da4fc0bad1b5892064d487b4a06b89 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, LGPL-2.1
- MPZ Related Functions
- =====================
- >>> import gmpy2 as G
- >>> from gmpy2 import mpz, mpq, mpfr
- >>> a = mpz(123)
- >>> b = mpz(456)
- Test gmpy2.add
- --------------
- >>> G.add(a, b)
- mpz(579)
- >>> G.add(a, 456)
- mpz(579)
- >>> G.add(123, 456)
- mpz(579)
- >>> G.add(123, b)
- mpz(579)
- Test gmpy2.bincoef
- ------------------
- >>> for i in range(10):
- ... print(G.bincoef(10,i))
- ...
- 1
- 10
- 45
- 120
- 210
- 252
- 210
- 120
- 45
- 10
- Test gmpy2.bit_clear
- --------------------
- >>> a.bit_clear(0)
- mpz(122)
- Test gmpy2.bit_flip
- -------------------
- >>> bin(a)
- '0b1111011'
- >>> bin(a.bit_flip(1))
- '0b1111001'
- >>> bin(a.bit_flip(2))
- '0b1111111'
- Test gmpy2.bit_length
- ---------------------
- >>> G.mpz(0).bit_length()
- 0
- >>> G.mpz(12345).bit_length()
- 14
- Test gmpy2.bit_mask
- -------------------
- >>> G.bit_mask(9)
- mpz(511)
- Test gmpy2.bit_scan0
- --------------------
- >>> [a.bit_scan0(j) for j in range(33)]
- [2, 2, 2, 7, 7, 7, 7, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]
- >>> n=G.mpz(-(7+6*16+5*256+7*4092))
- >>> [n.bit_scan0(j) for j in range(18)]
- [1, 1, 3, 3, 6, 6, 6, 8, 8, 10, 10, 12, 12, 13, 14, -1, None, None]
- >>> del n
- Test gmpy2.bit_scan1
- --------------------
- >>> [a.bit_scan1(j) for j in range(10)]
- [0, 1, 3, 3, 4, 5, 6, None, None, None]
- >>> n=G.mpz(-(7+6*16+5*256+7*4092))
- >>> [n.bit_scan1(j) for j in range(33)]
- [0, 2, 2, 4, 4, 5, 7, 7, 9, 9, 11, 11, 15, 15, 15, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]
- >>> del n
- Test gmpy2.bit_set
- ------------------
- >>> a.bit_set(20)
- mpz(1048699)
- Test gmpy2.bit_test
- -------------------
- >>> [a.bit_test(i) for i in range(8)]
- [True, True, False, True, True, True, True, False]
- >>> [b.bit_test(i) for i in range(10)]
- [False, False, False, True, False, False, True, True, True, False]
- Test gmpy2.c_div
- ----------------
- >>> G.c_div(b,64)
- mpz(8)
- >>> G.c_div(b,-64)
- mpz(-7)
- >>> G.c_div(-b,64)
- mpz(-7)
- >>> G.c_div(-b,-64)
- mpz(8)
- Test gmpy2.c_div_2exp
- ---------------------
- >>> G.c_div_2exp(b, 6)
- mpz(8)
- >>> G.c_div_2exp(b, -6)
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- OverflowError: can't convert negative value to unsigned int
- >>> G.c_div_2exp(-b, 6)
- mpz(-7)
- Test gmpy2.c_divmod
- -------------------
- >>> G.c_divmod(b,64)
- (mpz(8), mpz(-56))
- >>> G.c_divmod(b,-64)
- (mpz(-7), mpz(8))
- >>> G.c_divmod(-b,64)
- (mpz(-7), mpz(-8))
- >>> G.c_divmod(-b,-64)
- (mpz(8), mpz(56))
- >>> G.c_divmod(17,5)
- (mpz(4), mpz(-3))
- >>> G.c_divmod(-17,5)
- (mpz(-3), mpz(-2))
- >>> G.c_divmod(17,-5)
- (mpz(-3), mpz(2))
- >>> G.c_divmod(-17,-5)
- (mpz(4), mpz(3))
- >>> G.c_divmod(b, 4.0)
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- TypeError: c_divmod() requires 'mpz','mpz' arguments
- >>> G.c_divmod('a', b)
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- TypeError: c_divmod() requires 'mpz','mpz' arguments
- >>> G.c_divmod(b)
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- TypeError: c_divmod() requires 'mpz','mpz' arguments
- Test gmpy2.c_divmod_2exp
- ------------------------
- >>> G.c_divmod_2exp(b, 6)
- (mpz(8), mpz(-56))
- >>> G.c_divmod_2exp(-b, 6)
- (mpz(-7), mpz(-8))
- Test gmpy2.c_mod
- ----------------
- >>> G.c_mod(b,64)
- mpz(-56)
- >>> G.c_mod(b,-64)
- mpz(8)
- >>> G.c_mod(-b,64)
- mpz(-8)
- >>> G.c_mod(-b,-64)
- mpz(56)
- Test gmpy2.c_mod_2exp
- ---------------------
- >>> G.c_mod_2exp(b, 6)
- mpz(-56)
- >>> G.c_mod_2exp(-b, 6)
- mpz(-8)
- Test gmpy2.comb
- ---------------
- >>> G.comb(3,-1)
- Traceback (most recent call last):
- ...
- ValueError: binomial coefficient with negative k
- >>> G.comb(8,4)
- mpz(70)
- Test gmpy2.divexact
- -------------------
- >>> aa=G.mpz('1234567912345678912345679')
- >>> bb=G.mpz('789789789789789789789789')
- >>> cc=aa*bb
- >>> print(G.divexact(cc,aa))
- 789789789789789789789789
- >>> del aa,bb,cc
- Test gmpy2.divm
- ---------------
- >>> G.divm(b,a,20)
- mpz(12)
- >>> G.divm(a,b,100)
- Traceback (innermost last):
- ...
- ZeroDivisionError: not invertible
- >>> G.divm(6,12,14)
- mpz(4)
- >>> G.divm(0,1,2)
- mpz(0)
- >>> G.divm(4,8,20)
- mpz(3)
- Test gmpy2.f_div
- ----------------
- Test gmpy2.f_div_2exp
- ---------------------
- Test gmpy2.f_divmod
- -------------------
- >>> G.f_divmod(17,5)
- (mpz(3), mpz(2))
- >>> G.f_divmod(-17,5)
- (mpz(-4), mpz(3))
- >>> G.f_divmod(17,-5)
- (mpz(-4), mpz(-3))
- >>> G.f_divmod(-17,-5)
- (mpz(3), mpz(-2))
- >>> G.f_divmod(b,64)
- (mpz(7), mpz(8))
- >>> G.f_divmod(-b,64)
- (mpz(-8), mpz(56))
- >>> G.f_divmod(b,-64)
- (mpz(-8), mpz(-56))
- >>> G.f_divmod(-b,-64)
- (mpz(7), mpz(-8))
- Test gmpy2.f_divmod_2exp
- ------------------------
- >>> G.f_divmod_2exp(b,6)
- (mpz(7), mpz(8))
- >>> G.f_divmod_2exp(-b,6)
- (mpz(-8), mpz(56))
- Test gmpy2.f_mod
- ----------------
- Test gmpy2.f_mod_2exp
- ---------------------
- >>> G.f_mod_2exp(a,5)
- mpz(27)
- >>> G.f_mod_2exp(b,5)
- mpz(8)
- >>> G.f_mod_2exp(b,5) == (b%32)
- True
- >>> G.f_mod_2exp(a,5) == (a%32)
- True
- Test gmpy2.fac
- --------------
- >>> G.fac(7)
- mpz(5040)
- Test gmpy2.fib
- --------------
- >>> G.fib(17)
- mpz(1597)
- Test gmpy2.fib2
- ---------------
- >>> G.fib2(17)
- (mpz(1597), mpz(987))
- Test gmpy2.gcd
- --------------
- >>> G.gcd(a,b)
- mpz(3)
- Test gmpy2.gcdext
- -----------------
- >>> temp=G.gcdext(a,b)
- >>> temp[0]==a*temp[1]+b*temp[2]
- True
- >>> temp=G.gcdext(123,456)
- >>> temp[0]==a*temp[1]+b*temp[2]
- True
- >>> del temp
- Test gmpy2.hamdist
- ------------------
- >>> G.hamdist(a,b)
- 6
- >>> G.hamdist(3)
- Traceback (innermost last):
- ...
- TypeError: hamdist() requires 'mpz','mpz' arguments
- >>> G.hamdist(a)
- Traceback (innermost last):
- ...
- TypeError: hamdist() requires 'mpz','mpz' arguments
- >>> G.hamdist(a, 3, 4)
- Traceback (innermost last):
- ...
- TypeError: hamdist() requires 'mpz','mpz' arguments
- Test gmpy2.invert
- -----------------
- >>> G.invert(a,100)
- mpz(87)
- >>> G.invert(b,100)
- mpz(0)
- >>> G.invert(3)
- Traceback (innermost last):
- ...
- TypeError: invert() requires 'mpz','mpz' arguments
- >>> G.invert()
- Traceback (innermost last):
- ...
- TypeError: invert() requires 'mpz','mpz' arguments
- Test gmpy2.iroot
- ----------------
- >>> for i in range(5):
- ... print(G.iroot(a,i+1),G.iroot(b,i+1))
- ...
- (mpz(123), True) (mpz(456), True)
- (mpz(11), False) (mpz(21), False)
- (mpz(4), False) (mpz(7), False)
- (mpz(3), False) (mpz(4), False)
- (mpz(2), False) (mpz(3), False)
- >>> G.iroot(9,2)
- (mpz(3), True)
- Test gmpy2.iroot_rem
- --------------------
- >>> G.iroot_rem(a,2)
- (mpz(11), mpz(2))
- >>> G.iroot_rem(a,3)
- (mpz(4), mpz(59))
- >>> G.iroot_rem(a*a)
- Traceback (most recent call last):
- ...
- TypeError: iroot_rem() requires 'mpz','int' arguments
- >>> G.iroot_rem(a*a,2)
- (mpz(123), mpz(0))
- Test gmpy2.is_even
- ------------------
- >>> G.is_even(a)
- False
- >>> G.is_even(b)
- True
- Test gmpy2.is_odd
- -----------------
- >>> G.is_odd(a)
- True
- >>> G.is_odd(b)
- False
- Test gmpy2.is_power
- -------------------
- >>> G.is_power()
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- TypeError: is_power() takes exactly one argument (0 given) >>> a.is_power()
- >>> G.is_power(99*99*99)
- True
- >>> G.is_power(99*98)
- False
- Test gmpy2.is_prime
- -------------------
- >>> G.is_prime(3,-3)
- Traceback (most recent call last):
- ...
- ValueError: repetition count for is_prime must be positive
- >>> G.is_prime(12345)
- False
- >>> G.is_prime(80**81 + 81**80)
- True
- Test gmpy2.is_square
- --------------------
- >>> a.is_square()
- False
- >>> G.is_square(99*99)
- True
- >>> G.is_square(99*99*99)
- False
- >>> G.is_square(0)
- True
- >>> G.is_square(-1)
- False
- Test gmpy2.isqrt
- ----------------
- >>> print(G.isqrt(a))
- 11
- >>> print(G.isqrt(b))
- 21
- >>> G.isqrt(-1)
- Traceback (most recent call last):
- ...
- ValueError: isqrt() of negative number
- Test gmpy2.isqrt_rem
- --------------------
- >>> print(G.isqrt_rem(a))
- (mpz(11), mpz(2))
- >>> print(G.isqrt_rem(b))
- (mpz(21), mpz(15))
- >>> G.isqrt_rem(-1)
- Traceback (most recent call last):
- ...
- ValueError: isqrt_rem() of negative number
- Test gmpy2.jacobi
- -----------------
- >>> G.jacobi(10,3)
- 1
- >>> G.jacobi(10,-3)
- Traceback (most recent call last):
- ...
- ValueError: jacobi's y must be odd prime > 0
- >>> G.jacobi(3)
- Traceback (innermost last):
- ...
- TypeError: jacobi() requires 'mpz','mpz' arguments
- >>> G.jacobi()
- Traceback (innermost last):
- ...
- TypeError: jacobi() requires 'mpz','mpz' arguments
- Test gmpy2.kronecker
- --------------------
- >>> G.kronecker(10,3)
- 1
- >>> G.kronecker(10,-3)
- 1
- >>> G.kronecker(3)
- Traceback (innermost last):
- ...
- TypeError: kronecker() requires 'mpz','mpz' arguments
- >>> G.kronecker()
- Traceback (innermost last):
- ...
- TypeError: kronecker() requires 'mpz','mpz' arguments
- >>> aaa = 10**20
- >>> bbb = aaa+39
- >>> G.jacobi(aaa,bbb)
- 1
- >>> G.legendre(aaa,bbb)
- 1
- >>> G.kronecker(aaa,bbb)
- 1
- >>> del aaa,bbb
- Test gmpy2.lcm
- --------------
- >>> G.lcm(a,b)
- mpz(18696)
- Test gmpy2.legendre
- -------------------
- >>> G.legendre(10,3)
- 1
- >>> G.legendre(10,-3)
- Traceback (most recent call last):
- ...
- ValueError: legendre's y must be odd and > 0
- >>> G.legendre(3)
- Traceback (innermost last):
- ...
- TypeError: legendre() requires 'mpz','mpz' arguments
- >>> G.legendre()
- Traceback (innermost last):
- ...
- TypeError: legendre() requires 'mpz','mpz' arguments
- Test gmpy2.lucas
- ----------------
- Test gmpy2.lucas2
- -----------------
- Test gmpy2.mul
- --------------
- Test gmpy2.mul_2exp
- -------------------
- Test gmpy2.next_prime
- ---------------------
- Test gmpy2.numdigits
- --------------------
- >>> G.numdigits(23)
- 2
- >>> G.numdigits(23,2)
- 5
- >>> G.numdigits(23,99)
- Traceback (most recent call last):
- ...
- ValueError: base must be either 0 or in the interval 2 ... 62
- Test gmpy2.pack
- ---------------
- Test gmpy2.popcount
- -------------------
- >>> G.popcount(a)
- 6
- >>> G.popcount(b)
- 4
- >>> G.popcount(-7)
- -1
- >>> G.popcount(0)
- 0
- Test gmpy2.remove
- -----------------
- gmpy2.remove factors out multiple copies of a factor from a larger integer.
- The factor must be greater than or equal to 2.
- >>> G.remove(a,2)
- (mpz(123), 0)
- >>> G.remove(a,3)
- (mpz(41), 1)
- >>> G.remove(b,2)
- (mpz(57), 3)
- >>> G.remove(b,3)
- (mpz(152), 1)
- >>> G.remove(b,1)
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- ValueError: factor must be > 1
- >>> G.remove(b,0)
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- ValueError: factor must be > 1
- >>> G.remove(b,789)
- (mpz(456), 0)
- >>> G.remove(b,-3)
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- ValueError: factor must be > 1
- >>> G.remove(b,float('NaN'))
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- TypeError: remove() requires 'mpz','mpz' arguments
- >>> G.remove(3,-1)
- Traceback (most recent call last):
- ...
- ValueError: factor must be > 1
- >>> G.remove(3)
- Traceback (innermost last):
- ...
- TypeError: remove() requires 'mpz','mpz' arguments
- >>> G.remove()
- Traceback (innermost last):
- ...
- TypeError: remove() requires 'mpz','mpz' arguments
- Test gmpy2.t_div
- ----------------
- Test gmpy2.t_div_2exp
- ---------------------
- Test gmpy2.t_divmod
- -------------------
- >>> G.t_divmod(17,5)
- (mpz(3), mpz(2))
- >>> G.t_divmod(-17,5)
- (mpz(-3), mpz(-2))
- >>> G.t_divmod(17,-5)
- (mpz(-3), mpz(2))
- >>> G.t_divmod(-17,-5)
- (mpz(3), mpz(-2))
- Test gmpy2.t_divmod_2exp
- ------------------------
- Test gmpy2.t_mod
- ----------------
- Test gmpy2.t_mod_2exp
- ---------------------