本文整理汇总了Python中gmpy2.next_prime方法的典型用法代码示例。如果您正苦于以下问题:Python gmpy2.next_prime方法的具体用法?Python gmpy2.next_prime怎么用?Python gmpy2.next_prime使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gmpy2
的用法示例。
在下文中一共展示了gmpy2.next_prime方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getprimeover
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import next_prime [as 别名]
def getprimeover(N):
"""Return a random N-bit prime number using the System's best
Cryptographic random source.
Use GMP if available, otherwise fallback to PyCrypto
"""
if HAVE_GMP:
randfunc = random.SystemRandom()
r = gmpy2.mpz(randfunc.getrandbits(N))
r = gmpy2.bit_set(r, N - 1)
return int(gmpy2.next_prime(r))
elif HAVE_CRYPTO:
return number.getPrime(N, os.urandom)
else:
randfunc = random.SystemRandom()
n = randfunc.randrange(2**(N-1), 2**N) | 1
while not is_prime(n):
n += 2
return n
示例2: getprimeover
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import next_prime [as 别名]
def getprimeover(n):
"""return a random n-bit prime number
"""
r = gmpy2.mpz(random.SystemRandom().getrandbits(n))
r = gmpy2.bit_set(r, n - 1)
return int(gmpy2.next_prime(r))
示例3: factor_prime_power
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import next_prime [as 别名]
def factor_prime_power(x): # TODO: move this to a separate math/number theory module
"""Return p and d for a prime power x = p**d."""
if x <= 1:
raise ValueError('number not a prime power')
k = 10
# test whether p is below 2**k, for positive k
p = 2
while p < 1<<k:
if x % p == 0:
d = 0
while x > 1:
x, r = divmod(x, p)
if r == 0:
d += 1
else:
raise ValueError('number not a prime power')
return int(p), d
p = next_prime(p)
# find prime factors of d
p, d = x, 1
while is_square(p):
p, d = isqrt(p), 2*d
e = 3
while k * e <= p.bit_length():
w, b = iroot(p, e)
if b:
p, d = w, e * d
else:
e = next_prime(e)
if is_prime(p):
return int(p), int(d)
raise ValueError('number not a prime power')
示例4: next_prime
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import next_prime [as 别名]
def next_prime(x):
"""Return the next probable prime number > x."""
if x <= 1:
x = 2
else:
x += 1 + x%2
while not is_prime(x):
x += 2
return x