Beispiel #1
0
def test():
    assert is_prime(73)
    assert is_prime(97)
    assert is_prime(3797)
    assert is_left_prime(3797)
    assert is_right_prime(3797)
    assert {3137, 37, 73, 797, 3797, 53, 23, 313, 317,
            373} == truncatable_primes(10**4)
Beispiel #2
0
    def generate_public_key(self):

        # Find suitable primes p, and q

        if 'p' in self.configuration:

            self.p = self.configuration['p']

        else:

            while True:
                self.p = random.randrange(
                    2, 2 << (self.configuration['width'] >> 1))
                if is_prime(self.p):
                    break

        if 'q' in self.configuration:

            self.q = self.configuration['q']

        else:

            while True:
                self.q = random.randrange(
                    2, 2 << (self.configuration['width'] >> 1))
                if is_prime(self.q):
                    break

        self.pq = self.p * self.q

        # Find a suitable exponent e

        if 'e' in self.configuration:

            self.e = self.configuration['e']

        else:

            while True:

                self.e = random.randrange(2, 2 <<
                                          (self.configuration['width']))

                if euclids_gcd(self.e, (self.p - 1) * (self.q - 1)) == 1:
                    break

        # We now have all the pieces for our public key

        self.public_key = (self.pq, self.e)

        # Solve for inverse of e

        self.d = extended_euclids_gcd(self.e, (self.p - 1) * (self.q - 1))[1]

        if self.d < 0:
            self.d += (self.p - 1) * (self.q - 1)
Beispiel #3
0
def prime_counting_function(n):  # pi
    """Count the number of primes not exceeding n"""
    result = 0
    for i in range(2, n + 1):
        if (is_prime(i)):
            result += 1
    return result
Beispiel #4
0
def get_sum_of_primes(upper_limit):
  running_sum = 0
  # This approach works for an input size of 2000000 if we let it run for a
  # minute, but we're going to need something better for anything much larger.
  for i in range(2, upper_limit + 1):
    if primality.is_prime(i):
      running_sum += i
  return running_sum
Beispiel #5
0
def get_nth_prime(n):
  num_primes = 0
  i = 2
  while True:
    if primality.is_prime(i):
      num_primes += 1
      if num_primes == n:
        return i
    i += 1
Beispiel #6
0
def is_right_prime(n):
    s = str(n)
    while s:
        n = int(s)
        if not is_prime(n):
            return False
        elif n in TRUNCATABLE_PRIMES:
            return True
        s = s[:-1]
    return True
Beispiel #7
0
def prime_number_generator(num):
  prime_nums_list = []
  i=2

  while len(prime_nums_list) != num:
    if is_prime(i):
      prime_nums_list.append(i)
    i+=1 


  return prime_nums_list
Beispiel #8
0
def main():
    for a in range(1000, 10000):
        if a == 1487:
            continue

        perms = list(int_permutations(a))
        for b in perms:
            c = a + 2 * (b - a)
            seq = (a, b, c)
            if c in perms and all(is_prime(x) for x in seq):
                print(''.join(str(x) for x in seq))
                return
def test():
    assert not is_prime(123)
    assert not is_prime(132)
    assert not is_prime(213)
    assert not is_prime(231)
    assert not is_prime(312)
    assert not is_prime(321)
    assert is_prime(2143)
    assert is_pandigital(2143)
    assert 2143 in set(pandigital_primes(digits=4))
Beispiel #10
0
def prime_number_generator(limit):
    prime_nums = []

    for n in range(1, limit):

        if is_prime(n):
            prime_nums.append(n)

            if n > limit:
                prime_nums.pop()
                break

    return prime_nums
Beispiel #11
0
def consecutive_primes_that_sum_prime(limit):
    primes = list(primes_upto(limit))
    while primes[0] + primes[-1] > limit:
        del primes[-1]

    for n in range(len(primes), 2, -1):
        s = sum(primes[:n])
        for i in range(len(primes) - n):
            if s > limit:
                break
            elif is_prime(s):
                return s, n
            else:
                s += primes[i + n] - primes[i]
Beispiel #12
0
def find_sides_for_prime_ratio(target_ratio, min_side_len=0):
    numbers = spiral_diagonal_numbers()
    numbers.next()
    count = 1
    primes = 0
    ratio = 0.0
    while True:
        for _ in xrange(4):
            n, side_len, _ = numbers.next()
            count += 1
            if is_prime(n):
                primes += 1
        ratio = primes/float(count)
        if ratio < target_ratio and side_len >= min_side_len:
            return side_len, ratio, primes, count
Beispiel #13
0
def naive_difference_of_squares_factorization(n,
                                              outer_depth=1000,
                                              inner_depth=1000,
                                              verbose=False):

    is_square = lambda x, float_precision=1e-12: int(x**.5 + float_precision
                                                     )**2 == x

    k = 1

    if n == 1 or is_prime(n): return [n]

    for _ in range(outer_depth):

        k += 1 if not n % 2 else 2

        for b in range(inner_depth):

            if is_square(k * n + b**2):

                a = int(round((k * n + b**2)**.5))

                f1 = euclids_gcd(n, a + b)
                f2 = euclids_gcd(n, a - b)

                if (f1 in [1, n]) and (f2 in [1, n]):
                    continue

                elif (f1 in [1, n]):
                    f1 = n / f2

                elif (f2 in [1, n]):
                    f2 = n / f1

                elif f1 * f2 != n:
                    f1 = max(f1, f2)
                    f2 = n / f1

                if verbose:
                    print str(n) + " => " + str([f1, f2])

                return sorted(
                    naive_difference_of_squares_factorization(f1) +
                    naive_difference_of_squares_factorization(f2))

    # No solution found in given depth
    return [n]
Beispiel #14
0
    def generate_public_key(self):

        # Find suitable prime p

        if 'p' in self.configuration:

            self.p = self.configuration['p']

        else:

            while True:
                self.p = random.randrange(3, 2 <<
                                          (self.configuration['width']))
                if is_prime(self.p):
                    break

        # Select an element g of high order modulo p

        if 'g' in self.configuration:

            self.g = self.configuration['g']

        else:

            for g in range(1, self.p):

                order = 1

                element = g % self.p

                while (element != 1):
                    element = (element * g) % self.p
                    order += 1

                if (order >= self.p - 1):
                    self.g = g
                    break

        # Find a suitable private key ( a )

        self.private_key = self.configuration[
            'a'] if 'a' in self.configuration else random.randrange(1, self.p)

        # Solve for a suitable public key

        self.public_key = fast_powering(self.g, self.private_key, self.p)
Beispiel #15
0
def main():
    max_base = 1000
    max_num_digits = 4

    for num in range(1, max_base, 2):
        for num_digits in range(1, max_num_digits):
            for pos in positions(len(str(num)) + num_digits, num_digits):
                count = 0
                ps = []
                for digit in range(10):
                    trial = insert_digits(num, pos, digit)
                    if (len(str(trial)) == len(str(num)) + num_digits) \
                       and is_prime(insert_digits(num, pos, digit)):
                        ps.append(trial)
                        count += 1
                if count >= 8:
                    print(min(ps))
                    return
Beispiel #16
0
def main():
    # n is the number of digits. For sum numbers of digits, the sum of
    # the digits is a multiple of 3, so there are never any divisors.
    # 1..2 = 3
    # 1..3 = 6
    # 1..5 = 15
    # 1..6 = 21
    # 1..8 = 36
    # 1..9 = 45
    #
    # And we can skip 1 digit, because we know 7 is the largest
    # possible, and that's what we set maxprime to start with.
    maxprime = 7
    for n in [4, 5, 7]:
        for digits in permutations(range(1, n + 1)):
            num = int(''.join(str(i) for i in digits))
            if (num > maxprime) and is_prime(num):
                maxprime = num
    print(maxprime)
Beispiel #17
0
def get_keys(bits):
    '''
    Gets key
    '''

    while(1):
        p = get_prime(bits)
        if p < len(alphabet) * (len(alphabet) - 1) + len(alphabet):
            continue;
        
        p1 = p - 1
        q = p1/2
        if is_prime(q, log(bits)):
            break

    #use a value in the top 50% of the number of p's bits
    g_bits = bits - random.randint(1, bits/2)

    while(1):
        random.seed(os.urandom(int(log(g_bits))))
        g = random.randrange(2, p-1)
        if pow(g, q, p) == 1 and pow(g, 2, p) != 1:
            break


    #use a value in the top 25% of the number of p's bits
    a_bits = bits - random.randint(1, bits/4)
    
    while(1):
        random.seed(os.urandom(int(log(a_bits))))
        a = random.randrange(1, p-1)
        if a > 0 and a < p - 1:
            break
    
    ga = pow(g, a, p)
    
    return {"public":(p, g, ga), "private":a}
def prime_permutations(p):
    def int_permutations(n):
        return set(int(''.join(x)) for x in permutations(sorted(str(n))))

    return (x for x in int_permutations(p) if x >= p and is_prime(x))
Beispiel #19
0
def meets_condition(a, b):
    return primality.is_prime(int(str(a) + str(b))) and \
        primality.is_prime(int(str(b) + str(a)))
Beispiel #20
0
def is_circular_prime(n):
    return '0' not in str(n) and all(is_prime(r) for r in digit_rotations(n))
Beispiel #21
0
def count_consecutive_generated_primes(a, b):
    for n in count():
        if not is_prime(quadratic(n, a, b)):
            return n - 2