示例#1
0
    def verify_all_params(self) -> bool:
        """
        verify all parameters including p, q, r, g
        :return: True if all parameters are verified to fit in designated equations or have specific values,
                False otherwise
        """
        error = self.initialize_error()

        # check if p and q are the expected values
        if not number.equals(self.large_prime, self.LARGE_PRIME_EXPECTED):
            # if not, use Miller-Rabin algorithm to check the primality of p and q, 5 iterations by default
            if not number.is_prime(self.large_prime):
                error = self.set_error()
                print("Large prime value error. ")

        if not number.equals(self.small_prime, self.SMALL_PRIME_EXPECTED):
            if not number.is_prime(self.small_prime):
                error = self.set_error()
                print("Small prime value error. ")

        # get basic parameters
        cofactor = self.param_g.get_cofactor()

        # check equation p - 1 = qr
        if not number.equals(self.large_prime - 1, self.small_prime * cofactor):
            error = self.set_error()
            print("p - 1 does not equals to r * q.")

        # check q is not a divisor of r
        if number.is_divisor(self.small_prime, cofactor):
            error = self.set_error()
            print("q is a divisor of r.")

        # check 1 < g < p
        if not number.is_within_range(self.generator, 1, self.large_prime):
            error = self.set_error()
            print("g is not in the range of 1 to p. ")

        # check g^q mod p = 1
        if not number.equals(pow(self.generator, self.small_prime, self.large_prime), 1):
            error = self.set_error()
            print("g^q mod p does not equal to 1. ")

        # print out message
        output = "Baseline parameter check"
        if error:
            output += " failure. "
        else:
            output += " success. "
        print(output)

        return not error
示例#2
0
文件: 545.py 项目: wxv/PyPE
def F_valid(factors):
    ps = powerset(factors, 1, len(factors))
    for x in ps:
        f = product(x)+1
        if not f in (2, 3, 5, 23, 29) and is_prime(f, trials=10):
            return False

    return True
示例#3
0
def test_is_prime():
    unittest.TestCase.assertEqual(number.is_prime(0), True)
    unittest.TestCase.assertEqual(number.is_prime(1), True)
    unittest.TestCase.assertEqual(number.is_prime(2), True)
    unittest.TestCase.assertEqual(number.is_prime(3), False)
    unittest.TestCase.assertEqual(number.is_prime(19), False)
    unittest.TestCase.assertEqual(number.is_prime(-6), False)
示例#4
0
文件: 565.py 项目: wxv/PyPE
def S(n, d):
    s = 0
    # Test first factor (p^2-1)/(p-1) = p+1, so d|p+1, p < 10^11
    for f in range(2*d, n, 2*d):  # Test even f
        if is_prime(f-1):
            if (f-1)%1000 == 727: print(f-1, 1)

            # Multiples of f-1
            pass

    # Test rest of factors
    primes = sieve(int(n**0.5)+1)
    for a_1 in range(2, 40):
        for p_1 in primes:
            if p_1 < n**(1/a_1):
                if (p_1**(a_1+1)-1)//(p_1-1) % d == 0:
                    print(p_1, a_1)
示例#5
0
文件: 111.py 项目: wxv/PyPE
def S(n, d):
    s = 0
    found_M = False
    M = n
    other_digits = [r for r in range(10) if r != d]

    while not found_M:
        for non_rep in rep_combo(other_digits, n-M):
            digits = non_rep + (d,)*M
            for perm in unique_permutations(digits):
                if perm[0] == 0: continue  # Skip nums with leading 0s
                p = 0
                for x in perm: p = 10*p + x

                if is_prime(p):
                    print(p)
                    s += p
                    found_M = True

        M -= 1

    return s
示例#6
0
 def test_is_prime_4(self):
     self.assertFalse(number.is_prime(4))
示例#7
0
 def test_is_prime_2(self):
     self.assertTrue(number.is_prime(2))
示例#8
0
 def test_is_prime_7(self):
     self.assertTrue(number.is_prime(7))
示例#9
0
def test_is_prime():
    assert number.is_prime(2)
    assert number.is_prime(7)
    assert number.is_prime(11)
    assert not number.is_prime(10)
    assert not number.is_prime(9)
示例#10
0
文件: 058.py 项目: wxv/PyPE
from number import is_prime

s, n = 2, 1
a = 0
for i in range(10**5):
    if is_prime(n): a += 1
    if i%1000 == 0: print(s, a/(i+1))
    if i%4 == 0 and a/(i+1) < 0.10 and s > 7:
        print(s+1)
        break

    if i%4 == 0 and i != 0: s += 2
    n += s
示例#11
0
文件: 041.py 项目: wxv/PyPE
import itertools, number
l = 0
for i in range(7, 1, -1):  # 9 and 8 digit can't be prime
    for perm in itertools.permutations(range(1, i+1), i):
        n = int(''.join(map(str, perm)))
        if number.is_prime(n):
            l = max(l, n)

print(l)
示例#12
0
 def test_case(self):
     self.prime = number.is_prime(4)
     self.assertTrue(self.prime,msg="Is not prime!")
示例#13
0
文件: 131.py 项目: wxv/PyPE
# Derivations (mathblog.dk)
# n^3 + n^2p = k^3  <=>  n cbrt(p + n) / cbrt(n) = k
# n = x^3, p + n = y^3;  p = y^3 - x^3 = (y-x)(y^2 + yx + x^2)
# Since p has 2 factors, y-x = 1
# (i+1)^3 - i^3 < 1000000, i < 577
from number import is_prime
print(sum(is_prime((i+1)**3 - i**3) for i in range(1, 577)))
示例#14
0
文件: 118.py 项目: wxv/PyPE
# Brute force, combine together all primes with unique digits

from itertools import permutations
from number import is_prime

primes = []
prime_digits = []
for l in range(1, 10):
    for perm in permutations(range(1, 10), l):
        p = 0
        for x in perm: p = 10*p + x
        if is_prime(p):
            primes.append(p)
            prime_digits.append(perm)

prime_sets = 0

# pool is an array of bools, with pool[d] representing if d has been used
def create_set(i, pool):
    if all(pool):
        global prime_sets
        prime_sets += 1

    digits_left = pool.count(False)

    for j in range(i+1, len(primes)):
        if len(prime_digits[j]) > digits_left: break

        if not any(pool[d] for d in prime_digits[j]):
            for d in prime_digits[j]: pool[d] = True
            create_set(j, pool)
示例#15
0
文件: 358.py 项目: wxv/PyPE
# Cyclic number (10^(p-1)-1)/p = 00000000137...56789
# p*56789 = ...99999, p = ...09891
from number import is_prime

def starts137(p):
    return str((10**30 - 1) / p)[:3] == "137"  # Approximate 999... / p


for p in range(9891, 10**10, 100000):
    if(is_prime(p)) and starts137(p):
        print(p)