本文整理汇总了Python中sympy.nextprime函数的典型用法代码示例。如果您正苦于以下问题:Python nextprime函数的具体用法?Python nextprime怎么用?Python nextprime使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了nextprime函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: hamming_n_count
def hamming_n_count(limit, n):
exclusions = 0
prime = nextprime(n)
while prime <= limit:
print prime, limit // prime
exclusions += limit // prime
prime = nextprime(prime)
print limit, exclusions
return limit - exclusions
示例2: euler134
def euler134():
ret = 0
p1, p2 = 3, 5
while p2 < 1000000:
p1, p2 = p2, nextprime(p2)
ret += S(p1, p2)
return ret
示例3: test_dmp_zz_wang
def test_dmp_zz_wang():
R, x,y,z = ring("x,y,z", ZZ)
UV, _x = ring("x", ZZ)
p = ZZ(nextprime(R.dmp_zz_mignotte_bound(w_1)))
assert p == 6291469
t_1, k_1, e_1 = y, 1, ZZ(-14)
t_2, k_2, e_2 = z, 2, ZZ(3)
t_3, k_3, e_3 = y + z, 2, ZZ(-11)
t_4, k_4, e_4 = y - z, 1, ZZ(-17)
T = [t_1, t_2, t_3, t_4]
K = [k_1, k_2, k_3, k_4]
E = [e_1, e_2, e_3, e_4]
T = zip([ t.drop(x) for t in T ], K)
A = [ZZ(-14), ZZ(3)]
S = R.dmp_eval_tail(w_1, A)
cs, s = UV.dup_primitive(S)
assert cs == 1 and s == S == \
1036728*_x**6 + 915552*_x**5 + 55748*_x**4 + 105621*_x**3 - 17304*_x**2 - 26841*_x - 644
assert R.dmp_zz_wang_non_divisors(E, cs, ZZ(4)) == [7, 3, 11, 17]
assert UV.dup_sqf_p(s) and UV.dup_degree(s) == R.dmp_degree(w_1)
_, H = UV.dup_zz_factor_sqf(s)
h_1 = 44*_x**2 + 42*_x + 1
h_2 = 126*_x**2 - 9*_x + 28
h_3 = 187*_x**2 - 23
assert H == [h_1, h_2, h_3]
LC = [ lc.drop(x) for lc in [-4*y - 4*z, -y*z**2, y**2 - z**2] ]
assert R.dmp_zz_wang_lead_coeffs(w_1, T, cs, E, H, A) == (w_1, H, LC)
H_1 = [44*x**2 + 42*x + 1, 126*x**2 - 9*x + 28, 187*x**2 - 23]
H_2 = [-4*x**2*y - 12*x**2 - 3*x*y + 1, -9*x**2*y - 9*x - 2*y, x**2*y**2 - 9*x**2 + y - 9]
H_3 = [-4*x**2*y - 12*x**2 - 3*x*y + 1, -9*x**2*y - 9*x - 2*y, x**2*y**2 - 9*x**2 + y - 9]
c_1 = -70686*x**5 - 5863*x**4 - 17826*x**3 + 2009*x**2 + 5031*x + 74
c_2 = 9*x**5*y**4 + 12*x**5*y**3 - 45*x**5*y**2 - 108*x**5*y - 324*x**5 + 18*x**4*y**3 - 216*x**4*y**2 - 810*x**4*y + 2*x**3*y**4 + 9*x**3*y**3 - 252*x**3*y**2 - 288*x**3*y - 945*x**3 - 30*x**2*y**2 - 414*x**2*y + 2*x*y**3 - 54*x*y**2 - 3*x*y + 81*x + 12*y
c_3 = -36*x**4*y**2 - 108*x**4*y - 27*x**3*y**2 - 36*x**3*y - 108*x**3 - 8*x**2*y**2 - 42*x**2*y - 6*x*y**2 + 9*x + 2*y
# TODO
#assert R.dmp_zz_diophantine(H_1, c_1, [], 5, p) == [-3*x, -2, 1]
#assert R.dmp_zz_diophantine(H_2, c_2, [ZZ(-14)], 5, p) == [-x*y, -3*x, -6]
#assert R.dmp_zz_diophantine(H_3, c_3, [ZZ(-14)], 5, p) == [0, 0, -1]
factors = R.dmp_zz_wang_hensel_lifting(w_1, H, LC, A, p)
assert R.dmp_expand(factors) == w_1
示例4: doProblem
def doProblem():
count = 0
sum = 0
i=10
while count < 11:
i = nextprime(i)
if TruncatableTest(i):
# print(i)
sum += i
count += 1
return sum
示例5: oeis_a037992
def oeis_a037992(maxsize):
cnt = 0
todo = PriorityQueueSet([(2,2,0)])
done = set([2])
while maxsize != cnt:
out, p, k = todo.pop_smallest()
yield out
cnt += 1
np = sympy.nextprime(p)
if np not in done:
todo.add((np,np,0))
done.add(np)
todo.add((p**(2**(k+1)), p, k+1))
示例6: dh_private_key
def dh_private_key(digit = 10):
"""
Return two number tuple as private key.
Diffie-Hellman key exchange is based on the mathematical problem
called the Discrete Logarithm Problem (see ElGamal).
Diffie-Hellman key exchange is divided into the following steps:
* Alice and Bob agree on a base that consist of a prime p and a
primitive root of p called g
* Alice choses a number a and Bob choses a number b where a
and b are random numbers with 1 < a, b < p. These are their
private keys.
* Alice then publicly sends Bob `g^{a} \pmod p` while Bob sends
Alice `g^{b} \pmod p`
* They both raise the received value to their secretly chose number
(a or b) and now have both as their shared key `g^{ab} \pmod p`
Parameters
==========
digit: Key length in binary
Returns
=======
(p, g, a) : p = prime number, g = primitive root of p,
a = random number in between 2 and p - 1
Examples
========
>>> from sympy.crypto.crypto import dh_private_key
>>> from sympy.ntheory import isprime, is_primitive_root
>>> p, g, _ = dh_private_key()
>>> isprime(p)
True
>>> is_primitive_root(g, p)
True
>>> p, g, _ = dh_private_key(5)
>>> isprime(p)
True
>>> is_primitive_root(g, p)
True
"""
p = nextprime(2 ** digit)
g = primitive_root(p)
a = randrange(2, p)
return p, g, a
示例7: p134
def p134(limit):
t=time.clock()
ps=list(primesieve(limit)[2:])
ps.append(sp.nextprime(limit))
S=0
for i in range(len(ps)-1) :
b=ps[i+1]
c=ps[i]
a=-10**(int(math.log10(c))+1)
x1,y1=primeLD(a,b,c)
x=x1%b #b is prime, and a is a multiple of 100, so gcd(a,b)=1
S+=-a*x+c
print(S,time.clock()-t)
示例8: truncatables_primes
def truncatables_primes():
count =0
s = 0
last_prime = 7
while count<11:
prime = nextprime(last_prime)
prime_str = str(prime)
is_cool = True
for i in range(1,len(prime_str)):
p1 = int(prime_str[i:])
p2 = int(prime_str[:-i])
if not (isprime(p1) and isprime(p2)):
is_cool = False
if is_cool:
count = count +1
s = s+prime
last_prime = prime
return s
示例9: primes_gen
def primes_gen(*args):
"""
Prime number generator
Possible usages:
primes_gen(): infinite generator
primes_gen(a): generate all primes below a
primes_gen(a, b): generate all primes below a and b
"""
if len(args) == 1:
for x in primerange(2, args[0]):
yield x
elif len(args) == 2:
for x in primerange(*args):
yield x
elif len(args) == 0:
x = 1
while 1:
x = nextprime(x)
yield x
示例10: test_gf_ddf
def test_gf_ddf():
f = gf_from_dict({15: 1, 0: -1}, 11, ZZ)
g = [([1, 0, 0, 0, 0, 10], 1),
([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], 2)]
assert gf_ddf_zassenhaus(f, 11, ZZ) == g
assert gf_ddf_shoup(f, 11, ZZ) == g
f = gf_from_dict({63: 1, 0: 1}, 2, ZZ)
g = [([1, 1], 1),
([1, 1, 1], 2),
([1, 1, 1, 1, 1, 1, 1], 3),
([1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1], 6)]
assert gf_ddf_zassenhaus(f, 2, ZZ) == g
assert gf_ddf_shoup(f, 2, ZZ) == g
f = gf_from_dict({6: 1, 5: -1, 4: 1, 3: 1, 1: -1}, 3, ZZ)
g = [([1, 1, 0], 1),
([1, 1, 0, 1, 2], 2)]
assert gf_ddf_zassenhaus(f, 3, ZZ) == g
assert gf_ddf_shoup(f, 3, ZZ) == g
f = [1, 2, 5, 26, 677, 436, 791, 325, 456, 24, 577]
g = [([1, 701], 1),
([1, 110, 559, 532, 694, 151, 110, 70, 735, 122], 9)]
assert gf_ddf_zassenhaus(f, 809, ZZ) == g
assert gf_ddf_shoup(f, 809, ZZ) == g
p = ZZ(nextprime(int((2**15 * pi).evalf())))
f = gf_from_dict({15: 1, 1: 1, 0: 1}, p, ZZ)
g = [([1, 22730, 68144], 2),
([1, 64876, 83977, 10787, 12561, 68608, 52650, 88001, 84356], 4),
([1, 15347, 95022, 84569, 94508, 92335], 5)]
assert gf_ddf_zassenhaus(f, p, ZZ) == g
assert gf_ddf_shoup(f, p, ZZ) == g
示例11: elgamal_private_key
def elgamal_private_key(digit=10):
"""
Return three number tuple as private key.
Elgamal encryption is based on the mathmatical problem
called the Discrete Logarithm Problem (DLP). For example,
`a^{b} \equiv c \pmod p`
In general, if a and b are known, c is easily
calculated. If b is unknown, it is hard to use
a and c to get b.
Parameters
==========
digit : Key length in binary
Returns
=======
(p, r, d) : p = prime number, r = primitive root, d = random number
Examples
========
>>> from sympy.crypto.crypto import elgamal_private_key
>>> from sympy.ntheory import is_primitive_root, isprime
>>> a, b, _ = elgamal_private_key()
>>> isprime(a)
True
>>> is_primitive_root(b, a)
True
"""
p = nextprime(2**digit)
return p, primitive_root(p), randrange(2, p)
示例12: test_dmp_zz_wang
def test_dmp_zz_wang():
p = ZZ(nextprime(dmp_zz_mignotte_bound(w_1, 2, ZZ)))
assert p == ZZ(6291469)
t_1, k_1, e_1 = dmp_normal([[1],[]], 1, ZZ), 1, ZZ(-14)
t_2, k_2, e_2 = dmp_normal([[1, 0]], 1, ZZ), 2, ZZ(3)
t_3, k_3, e_3 = dmp_normal([[1],[ 1, 0]], 1, ZZ), 2, ZZ(-11)
t_4, k_4, e_4 = dmp_normal([[1],[-1, 0]], 1, ZZ), 1, ZZ(-17)
T = [t_1, t_2, t_3, t_4]
K = [k_1, k_2, k_3, k_4]
E = [e_1, e_2, e_3, e_4]
T = zip(T, K)
A = [ZZ(-14), ZZ(3)]
S = dmp_eval_tail(w_1, A, 2, ZZ)
cs, s = dup_primitive(S, ZZ)
assert cs == 1 and s == S == \
dup_normal([1036728, 915552, 55748, 105621, -17304, -26841, -644], ZZ)
assert dmp_zz_wang_non_divisors(E, cs, 4, ZZ) == [7, 3, 11, 17]
assert dup_sqf_p(s, ZZ) and dup_degree(s) == dmp_degree(w_1, 2)
_, H = dup_zz_factor_sqf(s, ZZ)
h_1 = dup_normal([44, 42, 1], ZZ)
h_2 = dup_normal([126, -9, 28], ZZ)
h_3 = dup_normal([187, 0, -23], ZZ)
assert H == [h_1, h_2, h_3]
lc_1 = dmp_normal([[-4], [-4,0]], 1, ZZ)
lc_2 = dmp_normal([[-1,0,0], []], 1, ZZ)
lc_3 = dmp_normal([[1], [], [-1,0,0]], 1, ZZ)
LC = [lc_1, lc_2, lc_3]
assert dmp_zz_wang_lead_coeffs(w_1, T, cs, E, H, A, 2, ZZ) == (w_1, H, LC)
H_1 = [ dmp_normal(t, 0, ZZ) for t in [[44L,42L,1L],[126L,-9L,28L],[187L,0L,-23L]] ]
H_2 = [ dmp_normal(t, 1, ZZ) for t in [[[-4,-12],[-3,0],[1]],[[-9,0],[-9],[-2,0]],[[1,0,-9],[],[1,-9]]] ]
H_3 = [ dmp_normal(t, 1, ZZ) for t in [[[-4,-12],[-3,0],[1]],[[-9,0],[-9],[-2,0]],[[1,0,-9],[],[1,-9]]] ]
c_1 = dmp_normal([-70686,-5863,-17826,2009,5031,74], 0, ZZ)
c_2 = dmp_normal([[9,12,-45,-108,-324],[18,-216,-810,0],[2,9,-252,-288,-945],[-30,-414,0],[2,-54,-3,81],[12,0]], 1, ZZ)
c_3 = dmp_normal([[-36,-108,0],[-27,-36,-108],[-8,-42,0],[-6,0,9],[2,0]], 1, ZZ)
T_1 = [ dmp_normal(t, 0, ZZ) for t in [[-3,0],[-2],[1]] ]
T_2 = [ dmp_normal(t, 1, ZZ) for t in [[[-1,0],[]],[[-3],[]],[[-6]]] ]
T_3 = [ dmp_normal(t, 1, ZZ) for t in [[[]],[[]],[[-1]]] ]
assert dmp_zz_diophantine(H_1, c_1, [], 5, p, 0, ZZ) == T_1
assert dmp_zz_diophantine(H_2, c_2, [ZZ(-14)], 5, p, 1, ZZ) == T_2
assert dmp_zz_diophantine(H_3, c_3, [ZZ(-14)], 5, p, 1, ZZ) == T_3
factors = dmp_zz_wang_hensel_lifting(w_1, H, LC, A, p, 2, ZZ)
assert dmp_expand(factors, 2, ZZ) == w_1
示例13: test_powers_Integer
def test_powers_Integer():
"""Test Integer._eval_power"""
# check infinity
assert S(1) ** S.Infinity == 1
assert S(-1)** S.Infinity == S.NaN
assert S(2) ** S.Infinity == S.Infinity
assert S(-2)** S.Infinity == S.Infinity + S.Infinity * S.ImaginaryUnit
assert S(0) ** S.Infinity == 0
# check Nan
assert S(1) ** S.NaN == S.NaN
assert S(-1) ** S.NaN == S.NaN
# check for exact roots
assert S(-1) ** Rational(6, 5) == - (-1)**(S(1)/5)
assert S(4) ** Rational(1, 2) == 2
assert S(-4) ** Rational(1, 2) == I * 2
assert S(16) ** Rational(1, 4) == 2
assert S(-16) ** Rational(1, 4) == 2 * (-1)**Rational(1,4)
assert S(9) ** Rational(3, 2) == 27
assert S(-9) ** Rational(3, 2) == -27*I
assert S(27) ** Rational(2, 3) == 9
assert S(-27) ** Rational(2, 3) == 9 * (S(-1) ** Rational(2, 3))
assert (-2) ** Rational(-2, 1) == Rational(1, 4)
# not exact roots
assert (-3) ** (S(1)/2) == sqrt(-3)
assert (3) ** (S(3)/2) == 3 * sqrt(3)
assert (-3) ** (S(3)/2) == - 3 * sqrt(-3)
assert (-3) ** (S(5)/2) == 9 * I * sqrt(3)
assert (-3) ** (S(7)/2) == - I * 27 * sqrt(3)
assert (2) ** (S(3)/2) == 2 * sqrt(2)
assert (2) ** (S(-3)/2) == sqrt(2) / 4
assert (81) ** (S(2)/3) == 9 * (S(3) ** (S(2)/3))
assert (-81) ** (S(2)/3) == 9 * (S(-3) ** (S(2)/3))
assert (-3) ** Rational(-7, 3) == -(-1)**Rational(2, 3)*3**Rational(2, 3)/27
assert (-3) ** Rational(-2, 3) == -(-1)**Rational(1, 3)*3**Rational(1, 3)/3
# join roots
assert sqrt(6) + sqrt(24) == 3*sqrt(6)
assert sqrt(2) * sqrt(3) == sqrt(6)
# separate sybols & constansts
x = Symbol("x")
assert sqrt(49 * x) == 7 * sqrt(x)
assert sqrt((3 - sqrt(pi)) ** 2) == 3 - sqrt(pi)
# check that it is fast for big numbers
assert (2**64+1) ** Rational(4, 3)
assert (2**64+1) ** Rational(17,25)
# negative rational power and negative base
assert (-3) ** Rational(-7, 3) == -(-1)**Rational(2, 3)*3**Rational(2, 3)/27
assert (-3) ** Rational(-2, 3) == -(-1)**Rational(1, 3)*3**Rational(1, 3)/3
assert S(1234).factors() == {617: 1, 2: 1}
assert Rational(2*3, 3*5*7).factors() == {2: 1, 5: -1, 7: -1}
# test that eval_power factors numbers bigger than limit (2**15)
from sympy import nextprime
n = nextprime(2**15) # bigger than the current limit in factor_trial_division
assert sqrt(n**2) == n
assert sqrt(n**3) == n*sqrt(n)
assert sqrt(4*n) == 2*sqrt(n)
示例14: random_prime
def random_prime(bits):
return sympy.nextprime(2 ** bits + random.randint(0, 2 ** bits))
示例15: test_gf_factor
def test_gf_factor():
assert gf_factor([], 11) == (0, [])
assert gf_factor([1], 11) == (1, [])
assert gf_factor([1,1], 11) == (1, [([1, 1], 1)])
f, p = [1,0,0,1,0], 2
g = (1, [([1, 0], 1),
([1, 1], 1),
([1, 1, 1], 1)])
assert gf_factor(f, p, method='zassenhaus') == g
assert gf_factor(f, p, method='shoup') == g
g = (1, [[1, 0],
[1, 1],
[1, 1, 1]])
assert gf_factor_sqf(f, p, method='zassenhaus') == g
assert gf_factor_sqf(f, p, method='shoup') == g
assert gf_factor([1, 5, 8, 4], 11) == \
(1, [([1, 1], 1), ([1, 2], 2)])
assert gf_factor([1, 1, 10, 1, 0, 10, 10, 10, 0, 0], 11) == \
(1, [([1, 0], 2), ([1, 9, 5], 1), ([1, 3, 0, 8, 5, 2], 1)])
assert gf_factor(gf_from_dict({32: 1, 0: 1}, 11), 11) == \
(1, [([1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 10], 1),
([1, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 10], 1)])
assert gf_factor(gf_from_dict({32: 8, 0: 5}, 11), 11) == \
(8, [([1, 3], 1),
([1, 8], 1),
([1, 0, 9], 1),
([1, 2, 2], 1),
([1, 9, 2], 1),
([1, 0, 5, 0, 7], 1),
([1, 0, 6, 0, 7], 1),
([1, 0, 0, 0, 1, 0, 0, 0, 6], 1),
([1, 0, 0, 0, 10, 0, 0, 0, 6], 1)])
assert gf_factor(gf_from_dict({63: 8, 0: 5}, 11), 11) == \
(8, [([1, 7], 1),
([1, 4, 5], 1),
([1, 6, 8, 2], 1),
([1, 9, 9, 2], 1),
([1, 0, 0, 9, 0, 0, 4], 1),
([1, 2, 0, 8, 4, 6, 4], 1),
([1, 2, 3, 8, 0, 6, 4], 1),
([1, 2, 6, 0, 8, 4, 4], 1),
([1, 3, 3, 1, 6, 8, 4], 1),
([1, 5, 6, 0, 8, 6, 4], 1),
([1, 6, 2, 7, 9, 8, 4], 1),
([1, 10, 4, 7, 10, 7, 4], 1),
([1, 10, 10, 1, 4, 9, 4], 1)])
# Gathen polynomials: x**n + x + 1 (mod p > 2**n * pi)
p = nextprime(int((2**15 * pi).evalf()))
f = gf_from_dict({15: 1, 1: 1, 0: 1}, p)
assert gf_sqf_p(f, p) == True
g = (1, [([1, 22730, 68144], 1),
([1, 81553, 77449, 86810, 4724], 1),
([1, 86276, 56779, 14859, 31575], 1),
([1, 15347, 95022, 84569, 94508, 92335], 1)])
assert gf_factor(f, p, method='zassenhaus') == g
assert gf_factor(f, p, method='shoup') == g
g = (1, [[1, 22730, 68144],
[1, 81553, 77449, 86810, 4724],
[1, 86276, 56779, 14859, 31575],
[1, 15347, 95022, 84569, 94508, 92335]])
assert gf_factor_sqf(f, p, method='zassenhaus') == g
assert gf_factor_sqf(f, p, method='shoup') == g
# Shoup polynomials: f = a_0 x**n + a_1 x**(n-1) + ... + a_n
# (mod p > 2**(n-2) * pi), where a_n = a_{n-1}**2 + 1, a_0 = 1
p = nextprime(int((2**4 * pi).evalf()))
f = [1, 2, 5, 26, 41, 39, 38] # deg(f) = 6
assert gf_sqf_p(f, p) == True
g = (1, [([1, 44, 26], 1),
([1, 11, 25, 18, 30], 1)])
assert gf_factor(f, p, method='zassenhaus') == g
assert gf_factor(f, p, method='shoup') == g
g = (1, [[1, 44, 26],
[1, 11, 25, 18, 30]])
assert gf_factor_sqf(f, p, method='zassenhaus') == g
assert gf_factor_sqf(f, p, method='shoup') == g