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):
..
没有合适的资源?快使用搜索试试~ 我知道了~
gmpy2-2.0.2.zip
共121个文件
py:35个
txt:26个
c:23个
需积分: 1 0 下载量 117 浏览量
2024-02-05
23:32:34
上传
评论
收藏 273KB ZIP 举报
温馨提示
gmpy2-2.0.2.zip
资源推荐
资源详情
资源评论
收起资源包目录
gmpy2-2.0.2.zip (121个子文件)
make.bat 4KB
gmpy_mpfr.c 83KB
gmpy_convert.c 74KB
gmpy_mpz.c 73KB
gmpy_basic.c 66KB
gmpy_mpc.c 47KB
gmpy2.c 46KB
gmpy_mpz_prp.c 42KB
gmpy_context.c 40KB
gmpy_binary.c 39KB
gmpy_mpany.c 34KB
gmpy_mpq.c 30KB
gmpy_xmpz.c 30KB
gmpy_mpz_divmod2exp.c 20KB
gmpy_mpz_lucas.c 18KB
gmpy_mpz_divmod.c 18KB
gmpy_mpmath.c 12KB
gmpy_xmpz_inplace.c 12KB
gmpy_random.c 11KB
gmpy_mpz_inplace.c 10KB
gmpy_cache.c 10KB
py3intcompat.c 9KB
mpz_pylong.c 7KB
gmpy_misc.c 5KB
COPYING 34KB
gmpy_args.h 23KB
gmpy_mpfr.h 18KB
gmpy_mpc.h 13KB
gmpy_convert.h 10KB
gmpy.h 9KB
gmpy_mpz.h 7KB
gmpy_context.h 6KB
gmpy_mpany.h 4KB
gmpy_mpq.h 4KB
gmpy_xmpz.h 4KB
gmpy_cache.h 3KB
gmpy_random.h 3KB
gmpy_binary.h 3KB
gmpy_mpz_divmod2exp.h 3KB
gmpy_xmpz_inplace.h 3KB
gmpy_misc.h 3KB
gmpy_mpz_prp.h 3KB
gmpy_mpz_inplace.h 3KB
gmpy_mpz_divmod.h 3KB
gmpy_basic.h 3KB
gmpy_mpz_lucas.h 2KB
COPYING.LESSER 7KB
Makefile 4KB
PKG-INFO 1KB
gmpy_test_mpz.py 20KB
gmpy_test_mpz.py 19KB
setup.py 14KB
gmpy_test_cvr.py 13KB
gmpy_test_cvr.py 13KB
gmpy_test_xmpz.py 10KB
gmpy_test_mpf.py 8KB
gmpy_test_mpf.py 8KB
conf.py 7KB
gmpy_test_mpq.py 6KB
gmpy_test_mpq.py 5KB
runtests.py 4KB
gmpy_test_thr.py 4KB
gmpy_test_thr.py 3KB
gmpy_test_dec.py 3KB
test_mpz_args.py 2KB
test_mpz_args.py 2KB
test_leaks.py 2KB
gmpy_test_dec.py 2KB
gmpy_test.py 1KB
test_hash.py 1KB
timing1.py 1KB
timing1.py 1KB
gmpy_test.py 1KB
test_large.py 887B
test_large.py 884B
timing3.py 827B
timing3.py 827B
test_pack.py 730B
timing2.py 716B
timing2.py 714B
test_pack.py 684B
gmpy_test.py 364B
gmpy_truediv.py 126B
gmpy_truediv.py 126B
README 3KB
mpfr.rst 27KB
mpz.rst 13KB
intro.rst 11KB
mpc.rst 10KB
advmpz.rst 8KB
overview.rst 5KB
mpq.rst 2KB
index.rst 479B
gmpy2.sln 4KB
gmpy2.sln 3KB
test_mpz_functions.txt 12KB
test_mpfr_trig.txt 12KB
test_context.txt 11KB
test_mpz.txt 11KB
test_mpz_template.txt 9KB
共 121 条
- 1
- 2
资源评论
程序员Chino的日记
- 粉丝: 3035
- 资源: 4万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功