コード例 #1
0
ファイル: euler204.py プロジェクト: iynaix/eulerproject
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
0
ファイル: euler134.py プロジェクト: iynaix/eulerproject
def euler134():
    ret = 0
    p1, p2 = 3, 5
    while p2 < 1000000:
        p1, p2 = p2, nextprime(p2)
        ret += S(p1, p2)
    return ret
コード例 #3
0
ファイル: test_factortools.py プロジェクト: B-Rich/sympy
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
0
ファイル: p37.py プロジェクト: azusa0127/ProjectEuler
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
0
ファイル: prob500.py プロジェクト: AndrewWalker/project-euler
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
0
ファイル: crypto.py プロジェクト: Upabjojr/sympy
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
0
ファイル: PE_0134.py プロジェクト: mbh038/PE
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
0
ファイル: ex37.py プロジェクト: david-gang/project_euler
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
0
ファイル: utils.py プロジェクト: iynaix/eulerproject
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
0
ファイル: test_galoistools.py プロジェクト: BDGLunde/sympy
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
0
ファイル: crypto.py プロジェクト: Upabjojr/sympy
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
0
ファイル: test_factortools.py プロジェクト: ENuge/sympy
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
0
def test_powers_Integer():
    """Test Integer._eval_power"""
    # check infinity
    assert S(1)**S.Infinity == S.NaN
    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 sqrt(S(4)) == 2
    assert sqrt(S(-4)) == 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 sqrt(-3) == I * 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 symbols & 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
    # the current limit in factor_trial_division (2**15)
    from sympy import nextprime
    n = nextprime(2**15)
    assert sqrt(n**2) == n
    assert sqrt(n**3) == n * sqrt(n)
    assert sqrt(4 * n) == 2 * sqrt(n)

    # check that factors of base with powers sharing gcd with power are removed
    assert (2**4 * 3)**Rational(1, 6) == 2**Rational(2, 3) * 3**Rational(1, 6)
    assert (2**4 * 3)**Rational(
        5, 6) == 8 * 2**Rational(1, 3) * 3**Rational(5, 6)

    # check that bases sharing a gcd are exptracted
    assert 2**Rational(1, 3)*3**Rational(1, 4)*6**Rational(1, 5) == \
        2**Rational(8, 15)*3**Rational(9, 20)
    assert sqrt(8)*24**Rational(1, 3)*6**Rational(1, 5) == \
        4*2**Rational(7, 10)*3**Rational(8, 15)
    assert sqrt(8)*(-24)**Rational(1, 3)*(-6)**Rational(1, 5) == \
        4*(-3)**Rational(8, 15)*2**Rational(7, 10)
    assert 2**Rational(1, 3) * 2**Rational(8, 9) == 2 * 2**Rational(2, 9)
    assert 2**Rational(2, 3) * 6**Rational(1, 3) == 2 * 3**Rational(1, 3)
    assert 2**Rational(2, 3)*6**Rational(8, 9) == \
        2*2**Rational(5, 9)*3**Rational(8, 9)
    assert (-2)**Rational(2, S(3)) * (-4)**Rational(
        1, S(3)) == -2 * 2**Rational(1, 3)
    assert 3 * Pow(3, 2, evaluate=False) == 3**3
    assert 3 * Pow(3, -1 / S(3), evaluate=False) == 3**(2 / S(3))
    assert (-2)**(1/S(3))*(-3)**(1/S(4))*(-5)**(5/S(6)) == \
        -(-1)**Rational(5, 12)*2**Rational(1, 3)*3**Rational(1, 4) * \
        5**Rational(5, 6)

    assert Integer(-2)**Symbol('', even=True) == \
        Integer(2)**Symbol('', even=True)
    assert (-1)**Float(.5) == 1.0 * I
コード例 #14
0
import sympy
import time

start = time.time()
primes = []
current_prime = 11

while len(primes) < 11:

    i = str(current_prime)

    if all(
            sympy.isprime(int(i[:num + 1])) and sympy.isprime(int(i[num:]))
            for num in range(len(str(i)))):
        primes.append(int(i))
    current_prime = sympy.nextprime(current_prime)

print("Prime's list:", primes)
print("Sum:", sum(primes))
print("Elapsed Time:", time.time() - start)

# 10 sec solution
'''
import sympy
import time

start = time.time()
# generate primes in range from 8 to 1 000 000
def get_primes():
    x = list(sympy.primerange(8, 1000000))
    return x
コード例 #15
0
ファイル: util.py プロジェクト: jinwei14/Privacy-Engineering
def next_prime(num):			# next prime after num (skip 2)
  return 3 if num < 3 else sympy.nextprime(num)
コード例 #16
0
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
コード例 #17
0
ファイル: test_factortools.py プロジェクト: vchekan/sympy
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
コード例 #18
0
# T = 1
# MN = int(1e5)
# MA = int(1e9)
# L = 1
# R = MN
# # R = MN*(MN-1) + 1
# # L = R - (MN - 1)
#
# print(T)
# for test in range(T):
#     print(MN, L, R)
# print(' '.join([str(MA - i) for i in range(MN)]))

from sympy import nextprime

D = 1
cur = 1
while D <= int(1e15):
    cur = nextprime(cur)
    D *= cur

print(D // cur)
コード例 #19
0
def next_ends_by_1(p):
    np = nextprime(p)
    return (np % 10) == 1
コード例 #20
0

def generate_random_M():
    p = next_usable_prime(x)
    q = next_usable_prime(y)
    M = p * q

    assert (1 < seed < M)
    assert (seed % p != 0)
    assert (seed % q != 0)
    return M


def blum_blum_shub(xn, M):
    return (xn**2) % M


if __name__ == "__main__":
    rn = []
    M = generate_random_M()
    n = int(input("How many numbers do you want? "))
    x = sympy.nextprime(random.randint(0, (int(time()))))
    for _ in range(n):
        x = blum_blum_shub(x, M)
        rn.append(x)

    max = max(rn)
    print("integers:", rn)
    norm = [a / max for a in rn]
    print("normalised:", norm)
コード例 #21
0
        temp=mat[int(val[1])][int(val[0])]
        #char = chr(int(temp))
        print(temp,end ="")   
def decryption(y):
    s=find_s(y,w,m)
    r=superIncressingAlgo(a_sort,s)
    x=permutationPi(pi,r)
    x=map(str,x)
    res=str(int(''.join(x),2))
    if(len(res)<14):
        l=14-len(res)
        str1 = "0"*l
        res=str1+res
    return res

if __name__ == "__main__":
    m=nextprime(2036764117802210446778721319780021001)
    w = nextprime(127552671440279916013001)
    #m=2036764117802210446778721319780021357
    #w=127552671440279916013021
    y=readFile("y")
    b=readFile("knapsack")
    a=find_a(b,m,w)
    a_sort = sorted(a)
    pi=find_pi(a,a_sort)
    res=[]
    for val in y:
        res.append(decryption(val))
    for r in res:
        intTochar(r)
コード例 #22
0
 def __init__(self, shared_secret_value):
     public_key1 = sympy.nextprime(shared_secret_value)
     public_key2 = sympy.nextprime(public_key1 // 2)
     super.__init__(public_key1, public_key2)
     self.flag_generated_key = False
コード例 #23
0
import sympy

print('5 is a Prime Number',sympy.isprime(5))

print(list(sympy.primerange(0,100))


print(sympy.randprime(0,100)) #Output 83
print(sympy.randprime(0,100)) #Output 41
print(sympy.prime(3)) #Output 5
print(sympy.prevprime(50)) #Output 47
print(sympy.nextprime(50)) #Output 53
コード例 #24
0
from sympy import nextprime
from math import gcd
import random
import math

#問題(1)1.公開鍵(n)を求める

#任意の相異なる素数p,qを問題文の条件のもと、選ぶ
a = 42 % 30 + 10  #問題(1)1.(a)より
b = 13 + 10  #問題(1)1.(b)より

p = nextprime(a)  #p>aを満たす最小の素数

q = nextprime(b)
if q == p:  #もしqがpと同値であれば次の素数を選ぶ
    q = nextprime(q)
print("[pとqを出力して確認]")
print("p=", p, ",", "q=", q)  #確認:pとqの値

n = p * q  #nを求める
print("n=q*q =", n)  #確認:n


#最小公倍数lcm(p-1,q-1)を求めて、Lと互いに素でLより小さな任意の整数eを選ぶ
def lcm(p, q):  #最小公倍数を求める関数(Lの計算過程)
    return (p * q) // gcd(p, q)  #lcm(a,b) = ab / gcd(a,b)より


#問題(1)2.公開鍵(暗号鍵):eを生成する
L = lcm(p - 1, q - 1)  #最小公倍数を求める
for i in range(2, L):  #1<e<Lとなるiを生成
コード例 #25
0
ファイル: harshard.py プロジェクト: lokirius/python-euler
	if numero%xifrat != 0:
		return False
	candidat = numero//xifrat
	return isprime(candidat)

def right_harshad(numero):
	while  numero > 0:
		if harshad(numero) == False:
			return False
			break
		numero = numero//10
	else:
		return True


fita = 10**14
primer = 11
suma = 0

while primer < fita:
	candidat = primer//10
	if right_harshad(candidat) == False:
		pass
	elif strong_harshad(candidat) == False:
		pass
	else:
		suma += primer
	primer = nextprime(primer)

print(suma)
コード例 #26
0
ファイル: rsa-factoring.py プロジェクト: charlieob/code
# https://www.johndcook.com/blog/2018/12/19/rsa-with-one-known-prime/
# need Python 3 for secrets module
# py -3 rsa-factoring.py

from secrets import randbits
from sympy import nextprime, gcd
from timeit import default_timer as timer

numbits = 2048

p = nextprime(randbits(numbits))
q = nextprime(randbits(numbits))
r = nextprime(randbits(numbits))

m = p*q
n = p*r

t0 = timer()
g = gcd(m, n)
assert(p == g)
assert(q == m//p)
assert(r == n//p)
t1 = timer()

# Print time in milliseconds
print(1000*(t1 - t0))
print(p)
print(q)
print(r)

コード例 #27
0
ファイル: BBS.py プロジェクト: MoisanuStefan/Crypto
def getPrime(x):
    p = sympy.nextprime(x)
    while p % 4 != 3:
        p = sympy.nextprime(p)
    return p
コード例 #28
0
ファイル: p123.py プロジェクト: sumajirou/ProjectEuler
import sympy as sp

n = 1
p = 2
while True:
    r = 2 * n * p % p**2
    if r > 10**10:
        break
    n += 2
    p = sp.nextprime(sp.nextprime(p))

print(n)
コード例 #29
0
ファイル: p051.py プロジェクト: wephy/project-euler
 def prime_gen():
     n = 1
     while True:
         yield (n := nextprime(n))
コード例 #30
0
import sympy as s
t=int(input())
for k in range(t):
    z=int(input())
    temp=1
    p1=2
    p2=3
    run=True
    while run:
        if p1*p2<=z:
            p1=s.nextprime(p1)
            p2=s.nextprime(p2)
            temp=s.nextprime(temp)
        else:
            run=False
    print('Case #'+str(k+1)+': '+str(temp*p1))
コード例 #31
0
for i in range(100):
    for j in range(100):
        try:
            if f(i,j)==A:
                print i,j
        except Exception:
            continue
x=2
y=83
#p=next_prime(z*x*y)
#q=next_prime(z)
n=117930806043507374325982291823027285148807239117987369609583515353889814856088099671454394340816761242974462268435911765045576377767711593100416932019831889059333166946263184861287975722954992219766493089630810876984781113645362450398009234556085330943125568377741065242183073882558834603430862598066786475299918395341014877416901185392905676043795425126968745185649565106322336954427505104906770493155723995382318346714944184577894150229037758434597242564815299174950147754426950251419204917376517360505024549691723683358170823416757973059354784142601436519500811159036795034676360028928301979780528294114933347127
z=842868045681390934539739959201847552284980179958879667933078453950968566151662147267006293571765463137270594151138695778986165111380428806545593588078365331313084230014618714412959584843421586674162688321942889369912392031882620994944241987153078156389470370195514285850736541078623854327959382156753457477
start=z#gmpy2.iroot(n/x/y,2)[0]
while True:
    p=sympy.nextprime(start*x*y)
    q=sympy.nextprime(start)
    n_=p*q
    #print n_
    if n_==n:
        print start
        z=start
        break
    print n_-n
    start -= 1
#z=842868045681390934539739959201847552284980179958879667933078453950968566151662147267006293571765463137270594151138695778986165111380428806545593588078365331313084230014618714412959584843421586674162688321942889369912392031882620994944241987153078156389470370195514285850736541078623854327959382156753457477
p=sympy.nextprime(z*x*y)
q=sympy.nextprime(z)
assert(p*q==n)
c=21637349983376240298781643137017527643006420437956416224341194119376827720906813134837051662331110800533726249916480511461651883091547167072238889133223663294552334442051093640989347891754536977519831106118699250847317797879843851255826918152121510988443260626502258826108177049550321258420523982587818065892584009185540294356508050406622208242619887465832495988412605479326659942708790414892644525081859890294893951328910205466191795443278975666713957632984396458370561861108820821299355143041504204271934655444506866309772339580031558766661592776211084022129268334078625159825028582506422947933620995804155386524
e=0x10001
コード例 #32
0

a1 = 21856963452461630437348278434191434000066076750419027493852463513469865262064340836613831066602300959772632397773487317560339056658299954464169264467234407
b1 = 21856963452461630437348278434191434000066076750419027493852463513469865262064340836613831066602300959772632397773487317560339056658299954464169264467140596
a2 = 16466113115839228119767887899308820025749260933863446888224167169857612178664139545726340867406790754560227516013796269941438076818194617030304851858418927
b2 = 16466113115839228119767887899308820025749260933863446888224167169857612178664139545726340867406790754560227516013796269941438076818194617030304851858351026
n = 85492663786275292159831603391083876175149354309327673008716627650718160585639723100793347534649628330416631255660901307533909900431413447524262332232659153047067908693481947121069070451562822417357656432171870951184673132554213690123308042697361969986360375060954702920656364144154145812838558365334172935931441424096270206140691814662318562696925767991937369782627908408239087358033165410020690152067715711112732252038588432896758405898709010342467882264362733
c = 75700883021669577739329316795450706204502635802310731477156998834710820770245219468703245302009998932067080383977560299708060476222089630209972629755965140317526034680452483360917378812244365884527186056341888615564335560765053550155758362271622330017433403027261127561225585912484777829588501213961110690451987625502701331485141639684356427316905122995759825241133872734362716041819819948645662803292418802204430874521342108413623635150475963121220095236776428
p = 1
q = 1
i = 1
l = 0
for i in range(b1 + 1, a1 - 1):
    p *= modinv(i, a1)
    p %= a1
p = sympy.nextprime(p)
print "p="
print p
for i in range(b2 + 1, a2 - 1):
    q *= modinv(i, a2)
    q %= a2
q = sympy.nextprime(q)
print "q="
print q
r = n / q / p
print "r="
print r
fn = (p - 1) * (q - 1) * (r - 1)
print "fn="
print fn
e = 4097
コード例 #33
0
ファイル: A326344.py プロジェクト: rwbogl/rwbogl.github.io
def generate_tree_reversal_terms(residue, initial, base, modulus):
    p = lambda n: backwards_base(nextprime(n), base)
    c = lambda n: backwards_base(nextcompo(n), base)
    return generate_tree_terms(residue, initial, base, p, c, modulus)
コード例 #34
0
from sympy import primerange
from sympy import nextprime
# We're going to understand this algorithm by the exemple (151,157): we compute the first integer for which n*157[0] == 151[0] == 1
#which is 157*3 = 471, now we have found the first last integer, it is now unnecessary to try adding n*157, we can go for n*(1570) because the first last integer is already
#found, let's find the first integer for which n*1570 + 471 [1] == 151 [1] == 5, which is found for n==4 and the number is 6751, doing the same thing for the last integer 7
#we find 38151 :3
def s(p1,p2):
     cmp = p2
     s_p1 = str(p1)
     l = len(s_p1)
     inx = 1
     while inx <= l:
         if str(cmp)[len(str(cmp))-inx] == s_p1[l - inx]:
             inx +=1
             p2 = p2*10
         else:
             cmp += p2
     return cmp
pr = [i for i in primerange(5,10**6 +1) ]
pr.append(nextprime(pr[len(pr)-1])) #the problem states p1<= 10**6 ;)
print sum([s(pr[i],pr[i+1]) for i in xrange(0,len(pr)-1)])
コード例 #35
0
ファイル: A326344.py プロジェクト: rwbogl/rwbogl.github.io
def base_b_terms(b):
    p = lambda n: backwards_base(nextprime(n), b)
    c = lambda n: backwards_base(nextcompo(n), b)

    evens, odds = generate_terms(2, p, c)
    return evens, odds
コード例 #36
0
def genprime(n, K):
    return K(nextprime(int((2**n * pi).evalf())))
コード例 #37
0
ファイル: test_numbers.py プロジェクト: robotment/sympy
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)
コード例 #38
0
def nth_prime(n):
    prime_num = 0
    for i in range(n):
        prime_num = sympy.nextprime(prime_num)
    return prime_num
コード例 #39
0
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
コード例 #40
0
ファイル: bench_galoispolys.py プロジェクト: Acebulf/sympy
def genprime(n, K):
    return K(nextprime(int((2**n * pi).evalf())))
コード例 #41
0
def next_prime(n):
    return nextprime(n)
コード例 #42
0
def test_zzX_wang():
    f = zzX_from_poly(W_1)
    p = nextprime(zzX_mignotte_bound(f))

    assert p == 6291469

    V_1, k_1, E_1 = [[1],[]], 1, -14
    V_2, k_2, E_2 = [[1, 0]], 2, 3
    V_3, k_3, E_3 = [[1],[ 1, 0]], 2, -11
    V_4, k_4, E_4 = [[1],[-1, 0]], 1, -17

    V = [V_1, V_2, V_3, V_4]
    K = [k_1, k_2, k_3, k_4]
    E = [E_1, E_2, E_3, E_4]

    V = zip(V, K)

    A = [-14, 3]

    U = zzX_eval_list(f, A)
    cu, u = zzx_primitive(U)

    assert cu == 1 and u == U == \
        [1036728, 915552, 55748, 105621, -17304, -26841, -644]

    assert zzX_wang_non_divisors(E, cu, 4) == [7, 3, 11, 17]
    assert zzx_sqf_p(u) and zzx_degree(u) == zzX_degree(f)

    _, H = zzx_factor_sqf(u)

    h_1 = [44,  42,   1]
    h_2 = [126, -9,  28]
    h_3 = [187,  0, -23]

    assert H == [h_1, h_2, h_3]

    LC_1 = [[-4], [-4,0]]
    LC_2 = [[-1,0,0], []]
    LC_3 = [[1], [], [-1,0,0]]

    LC = [LC_1, LC_2, LC_3]

    assert zzX_wang_lead_coeffs(f, V, cu, E, H, A) == (f, H, LC)

    H_1 = [[44L, 42L, 1L], [126L, -9L, 28L], [187L, 0L, -23L]]
    C_1 = [-70686, -5863, -17826, 2009, 5031, 74]

    H_2 = [[[-4, -12], [-3, 0], [1]], [[-9, 0], [-9], [-2, 0]], [[1, 0, -9], [], [1, -9]]]
    C_2 = [[9, 12, -45, -108, -324], [18, -216, -810, 0], [2, 9, -252, -288, -945], [-30, -414, 0], [2, -54, -3, 81], [12, 0]]

    H_3 = [[[-4, -12], [-3, 0], [1]], [[-9, 0], [-9], [-2, 0]], [[1, 0, -9], [], [1, -9]]]
    C_3 = [[-36, -108, 0], [-27, -36, -108], [-8, -42, 0], [-6, 0, 9], [2, 0]]

    T_1 = [[-3, 0], [-2], [1]]
    T_2 = [[[-1, 0], []], [[-3], []], [[-6]]]
    T_3 = [[[]], [[]], [[-1]]]

    assert zzX_diophantine(H_1, C_1,    [], 5, p) == T_1
    assert zzX_diophantine(H_2, C_2, [-14], 5, p) == T_2
    assert zzX_diophantine(H_3, C_3, [-14], 5, p) == T_3

    factors = zzX_wang_hensel_lifting(f, H, LC, A, p)

    f_1 = zzX_to_poly(factors[0], x, y, z)
    f_2 = zzX_to_poly(factors[1], x, y, z)
    f_3 = zzX_to_poly(factors[2], x, y, z)

    assert f_1 == -(4*(y + z)*x**2 + x*y*z - 1).as_poly(x, y, z)
    assert f_2 == -(y*z**2*x**2 + 3*x*z + 2*y).as_poly(x, y, z)
    assert f_3 ==  ((y**2 - z**2)*x**2 + y - z**2).as_poly(x, y, z)

    assert f_1*f_2*f_3 == W_1
コード例 #43
0
def get_prime_number(digits):
    number = int(time.time() * 10 ** digits)
    prime_number = sympy.nextprime(number)
    while prime_number % 4 != 3:
        prime_number = sympy.nextprime(prime_number)
    return prime_number
コード例 #44
0
ファイル: bench_galoispolys.py プロジェクト: Aang/sympy
def genprime(n):
    return nextprime(int((2**n * pi).evalf()))
コード例 #45
0
ファイル: server.py プロジェクト: dkohlbre/crypto-vm
def random_prime(bits):
    return sympy.nextprime(2 ** bits + random.randint(0, 2 ** bits))
コード例 #46
0
def prime_numbers(digits):
    seed = int(time.time() * 10**digits)
    p = sympy.nextprime(seed)
    q = sympy.nextprime(p)
    return p, q
コード例 #47
0
from sympy import prime
from sympy import nextprime
from sys import exit
""" by doing the math we find out that when n=2p , the remainder is 2, otherwise it's 2*n*Pn :D, alse, 2*n < Pn
for big n """
n = 7037
p = prime(7037)
while 1:
    if (2*p*n) - 10**10 >0:
        print n, 2*(prime(n-2))*(n-2)
        exit(1)
    p = nextprime(nextprime(p))
    n+=2
コード例 #48
0
def test_gf_factor():
    assert gf_factor([], 11, ZZ) == (0, [])
    assert gf_factor([1], 11, ZZ) == (1, [])
    assert gf_factor([1,1], 11, ZZ) == (1, [([1, 1], 1)])

    assert gf_factor_sqf([], 11, ZZ) == (0, [])
    assert gf_factor_sqf([1], 11, ZZ) == (1, [])
    assert gf_factor_sqf([1,1], 11, ZZ) == (1, [[1, 1]])

    assert gf_factor_sqf([], 11, ZZ, method='berlekamp') == (0, [])
    assert gf_factor_sqf([1], 11, ZZ, method='berlekamp') == (1, [])
    assert gf_factor_sqf([1,1], 11, ZZ, method='berlekamp') == (1, [[1, 1]])

    assert gf_factor_sqf([], 11, ZZ, method='zassenhaus') == (0, [])
    assert gf_factor_sqf([1], 11, ZZ, method='zassenhaus') == (1, [])
    assert gf_factor_sqf([1,1], 11, ZZ, method='zassenhaus') == (1, [[1, 1]])

    assert gf_factor_sqf([], 11, ZZ, method='shoup') == (0, [])
    assert gf_factor_sqf([1], 11, ZZ, method='shoup') == (1, [])
    assert gf_factor_sqf([1,1], 11, ZZ, method='shoup') == (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, ZZ, method='berlekamp') == g
    assert gf_factor(f, p, ZZ, method='zassenhaus') == g
    assert gf_factor(f, p, ZZ, method='shoup') == g

    g = (1, [[1, 0],
             [1, 1],
             [1, 1, 1]])

    assert gf_factor_sqf(f, p, ZZ, method='berlekamp') == g
    assert gf_factor_sqf(f, p, ZZ, method='zassenhaus') == g
    assert gf_factor_sqf(f, p, ZZ, method='shoup') == g

    f, p = gf_from_int_poly([1,-3,1,-3,-1,-3,1], 11), 11

    g = (1, [([1, 1], 1),
             ([1, 5, 3], 1),
             ([1, 2, 3, 4], 1)])

    assert gf_factor(f, p, ZZ, method='berlekamp') == g
    assert gf_factor(f, p, ZZ, method='zassenhaus') == g
    assert gf_factor(f, p, ZZ, method='shoup') == g

    f, p = [1, 5, 8, 4], 11

    g = (1, [([1, 1], 1), ([1, 2], 2)])

    assert gf_factor(f, p, ZZ, method='berlekamp') == g
    assert gf_factor(f, p, ZZ, method='zassenhaus') == g
    assert gf_factor(f, p, ZZ, method='shoup') == g

    f, p = [1, 1, 10, 1, 0, 10, 10, 10, 0, 0], 11

    g = (1, [([1, 0], 2), ([1, 9, 5], 1), ([1, 3, 0, 8, 5, 2], 1)])

    assert gf_factor(f, p, ZZ, method='berlekamp') == g
    assert gf_factor(f, p, ZZ, method='zassenhaus') == g
    assert gf_factor(f, p, ZZ, method='shoup') == g

    f, p = gf_from_dict({32: 1, 0: 1}, 11, ZZ), 11

    g = (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(f, p, ZZ, method='berlekamp') == g
    assert gf_factor(f, p, ZZ, method='zassenhaus') == g
    assert gf_factor(f, p, ZZ, method='shoup') == g

    f, p = gf_from_dict({32: 8, 0: 5}, 11, ZZ), 11

    g = (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(f, p, ZZ, method='berlekamp') == g
    assert gf_factor(f, p, ZZ, method='zassenhaus') == g
    assert gf_factor(f, p, ZZ, method='shoup') == g

    f, p = gf_from_dict({63: 8, 0: 5}, 11, ZZ), 11

    g = (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)])

    assert gf_factor(f, p, ZZ, method='berlekamp') == g
    assert gf_factor(f, p, ZZ, method='zassenhaus') == g
    assert gf_factor(f, p, ZZ, method='shoup') == g

    # Gathen polynomials: x**n + x + 1 (mod p > 2**n * pi)

    p = ZZ(nextprime(int((2**15 * pi).evalf())))
    f = gf_from_dict({15: 1, 1: 1, 0: 1}, p, ZZ)

    assert gf_sqf_p(f, p, ZZ) == 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, ZZ, method='zassenhaus') == g
    assert gf_factor(f, p, ZZ, 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, ZZ, method='zassenhaus') == g
    assert gf_factor_sqf(f, p, ZZ, 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 = ZZ(nextprime(int((2**4 * pi).evalf())))
    f = [1, 2, 5, 26, 41, 39, 38]

    assert gf_sqf_p(f, p, ZZ) == True

    g = (1, [([1, 44, 26], 1),
             ([1, 11, 25, 18, 30], 1)])

    assert gf_factor(f, p, ZZ, method='zassenhaus') == g
    assert gf_factor(f, p, ZZ, method='shoup') == g

    g = (1, [[1, 44, 26],
             [1, 11, 25, 18, 30]])

    assert gf_factor_sqf(f, p, ZZ, method='zassenhaus') == g
    assert gf_factor_sqf(f, p, ZZ, method='shoup') == g
コード例 #49
0
ファイル: euler146.py プロジェクト: iynaix/eulerproject
import math
from collections import deque
from sympy import nextprime

start = 10 * 10 + 1
primes = deque([start] + [nextprime(start, x + 1) for x in range(5)])

def prime_pattern(arr):
    if not ((arr[5] - arr[0] == 26) and \
            (arr[4] - arr[0] == 12) and \
            (arr[3] - arr[0] == 8) and \
            (arr[2] - arr[0] == 6) and \
            (arr[1] - arr[0] == 2)):
        return 0
    n2 = arr[0] - 1
    n = int(math.sqrt(n2))
    if n * n == n2:
        return n
    else:
        return 0

limit = 10 ** 6
ret = 10
while 1:
    primes.popleft()
    primes.append(nextprime(primes[-1]))
    if primes[-1] >= limit:
        break
    ret += prime_pattern(primes)

print ret
コード例 #50
0
ファイル: test_galoistools.py プロジェクト: BDGLunde/sympy
def test_gf_factor():
    assert gf_factor([], 11, ZZ) == (0, [])
    assert gf_factor([1], 11, ZZ) == (1, [])
    assert gf_factor([1,1], 11, ZZ) == (1, [([1, 1], 1)])

    assert gf_factor_sqf([], 11, ZZ) == (0, [])
    assert gf_factor_sqf([1], 11, ZZ) == (1, [])
    assert gf_factor_sqf([1,1], 11, ZZ) == (1, [[1, 1]])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')

    assert gf_factor_sqf([], 11, ZZ) == (0, [])
    assert gf_factor_sqf([1], 11, ZZ) == (1, [])
    assert gf_factor_sqf([1,1], 11, ZZ) == (1, [[1, 1]])

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')

    assert gf_factor_sqf([], 11, ZZ) == (0, [])
    assert gf_factor_sqf([1], 11, ZZ) == (1, [])
    assert gf_factor_sqf([1,1], 11, ZZ) == (1, [[1, 1]])

    config.setup('GF_FACTOR_METHOD', 'shoup')

    assert gf_factor_sqf([], 11, ZZ) == (0, [])
    assert gf_factor_sqf([1], 11, ZZ) == (1, [])
    assert gf_factor_sqf([1,1], 11, ZZ) == (1, [[1, 1]])

    f, p = [1,0,0,1,0], 2

    g = (1, [([1, 0], 1),
             ([1, 1], 1),
             ([1, 1, 1], 1)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    g = (1, [[1, 0],
             [1, 1],
             [1, 1, 1]])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor_sqf(f, p, ZZ) == g

    f, p = gf_from_int_poly([1,-3,1,-3,-1,-3,1], 11), 11

    g = (1, [([1, 1], 1),
             ([1, 5, 3], 1),
             ([1, 2, 3, 4], 1)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = [1, 5, 8, 4], 11

    g = (1, [([1, 1], 1), ([1, 2], 2)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = [1, 1, 10, 1, 0, 10, 10, 10, 0, 0], 11

    g = (1, [([1, 0], 2), ([1, 9, 5], 1), ([1, 3, 0, 8, 5, 2], 1)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = gf_from_dict({32: 1, 0: 1}, 11, ZZ), 11

    g = (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)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = gf_from_dict({32: 8, 0: 5}, 11, ZZ), 11

    g = (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)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = gf_from_dict({63: 8, 0: 5}, 11, ZZ), 11

    g = (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)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    # Gathen polynomials: x**n + x + 1 (mod p > 2**n * pi)

    p = ZZ(nextprime(int((2**15 * pi).evalf())))
    f = gf_from_dict({15: 1, 1: 1, 0: 1}, p, ZZ)

    assert gf_sqf_p(f, p, ZZ) == 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)])

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    g = (1, [[1, 22730, 68144],
             [1, 81553, 77449, 86810, 4724],
             [1, 86276, 56779, 14859, 31575],
             [1, 15347, 95022, 84569, 94508, 92335]])

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor_sqf(f, p, ZZ) == 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 = ZZ(nextprime(int((2**4 * pi).evalf())))
    f = [1, 2, 5, 26, 41, 39, 38]

    assert gf_sqf_p(f, p, ZZ) == True

    g = (1, [([1, 44, 26], 1),
             ([1, 11, 25, 18, 30], 1)])

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    g = (1, [[1, 44, 26],
             [1, 11, 25, 18, 30]])

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'other')
    raises(KeyError, lambda: gf_factor([1,1], 11, ZZ))
    config.setup('GF_FACTOR_METHOD')
コード例 #51
0
ファイル: p007.py プロジェクト: leonlan/projecteuler
def p007(n):
    """Finds the n-th prime number."""
    p = 0
    for i in range(n):
        p = nextprime(p)
    return p
コード例 #52
0
ファイル: test_numbers.py プロジェクト: 101man/sympy
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 sqrt(S(4)) == 2
    assert sqrt(S(-4)) == 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 sqrt(-3)  == I*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 symbols & 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)

    # check that factors of base with powers sharing gcd with power are removed
    assert (2**4*3)**Rational(1, 6) == 2**Rational(2, 3)*3**Rational(1, 6)
    assert (2**4*3)**Rational(5, 6) == 8*2**Rational(1, 3)*3**Rational(5, 6)

    # check that bases sharing a gcd are exptracted
    assert 2**Rational(1, 3)*3**Rational(1, 4)*6**Rational(1, 5) == \
           2**Rational(8, 15)*3**Rational(9, 20)
    assert sqrt(8)*24**Rational(1, 3)*6**Rational(1, 5) == \
           4*2**Rational(7, 10)*3**Rational(8, 15)
    assert sqrt(8)*(-24)**Rational(1, 3)*(-6)**Rational(1, 5) == \
           4*(-3)**Rational(8, 15)*2**Rational(7, 10)
    assert 2**Rational(1, 3)*2**Rational(8, 9) == 2*2**Rational(2, 9)
    assert 2**Rational(2, 3)*6**Rational(1, 3) == 2*3**Rational(1, 3)
    assert 2**Rational(2, 3)*6**Rational(8, 9) == 2*2**Rational(5, 9)*3**Rational(8, 9)
    assert (-2)**Rational(2, S(3))*(-4)**Rational(1, S(3)) == -2*2**Rational(1, 3)
    assert 3*Pow(3, 2, evaluate=False) == 3**3
    assert 3*Pow(3, -1/S(3), evaluate=False) == 3**(2/S(3))
    assert (-2)**(1/S(3))*(-3)**(1/S(4))*(-5)**(5/S(6)) == \
           -(-1)**Rational(5, 12)*2**Rational(1, 3)*3**Rational(1, 4)*5**Rational(5, 6)
コード例 #53
0
from sympy import nextprime
from pwnlib.tubes.remote import remote

r = remote('188.166.133.53', 11059)

while True:
    lines = r.recvlines(2)
    print '\n'.join(lines)
    p = nextprime(lines[1].split()[8][:-1])
    print p
    r.send(str(p))