示例#1
0
def is_truncatable(number: int):
    """
    Being prime itself, it is possible to continuously
    remove digits from left to right,
    and remain prime at each stage: 3797, 797, 97, and 7.
    Similarly we can work from right to left: 3797, 379, 37, and 3.
    :param number:
    :return:
    """

    str_number = str(number)
    index = 0

    # Left shift:
    while index < len(str_number):
        if not is_prime(int(str_number[index:])):
            return False

        index += 1

    # Right shift:
    index = len(str_number)
    while index > 0:
        if not is_prime(int(str_number[:index])):
            return False

        index -= 1

    return True
示例#2
0
def print_primes(max_primes: dict):
    product = max_primes['a'] * max_primes['b']
    print('\nmax: {} -> {}, '
          'a: {} -> {}, '
          'b: {} -> {}, '
          'product: {} -> {}'.format(max_primes['max'],
                                     is_prime(max_primes['max']),
                                     max_primes['a'],
                                     is_prime(max_primes['a']),
                                     max_primes['b'],
                                     is_prime(max_primes['b']), product,
                                     is_prime(product)))
示例#3
0
def find_largest_pandigital_prime(numbers: list):
    """
    We shall say that an n-digit number is
    pandigital if it makes use of all
    the digits 1 to n exactly once.

    For example, 2143 is a 4-digit pandigital and is also prime.
    What is the largest n-digit pandigital prime that exists?

    :param numbers:
    :param number:
    :return:
    """

    start_time = time.time()
    permutations = list(
        int(''.join(t))
        for t in itertools.permutations(str(t)
                                        for t in numbers))

    big_pandigital_primes = set()
    for n in permutations:
        if is_pandigital(n) and is_prime(n):
            big_pandigital_primes.add(n)

    result = max(big_pandigital_primes)
    print_time_log(start_time, result)
    return result
示例#4
0
def main():

    start_time = time.time()
    max_primes = {'max': 0, 'a': 0, 'b': 0, 'product': 0}

    for a in range(-40, 41, 1):
        for b in range(-41, 42, 1):
            n = 0
            primes = set()
            while n <= a:
                temp = quadratic_primes(a, b, n)
                if is_prime(temp):
                    primes.add(temp)
                a += 1

            temp_max = len(primes)
            if max_primes['max'] < temp_max:
                max_primes['max'] = temp_max
                max_primes['a'] = a
                max_primes['b'] = b
                max_primes['product'] = a * b
                print_primes(max_primes)

    print_time_log(start_time, max_primes['product'])
    return max_primes
示例#5
0
def check_for_primes(perms):
    primes = []
    for perm in perms:
        num = int(''.join(perm))
        if is_prime(num):
            primes.append(num)
    return primes
示例#6
0
def factor(number):
    result = list()

    for i in range(3, int(math.sqrt(number)) + 1, 2):
        if number % i == 0 and is_prime(i):
            result.append(i)

    return result
示例#7
0
def prime_generator(limit: int):
    start_time = time.time()
    primes = list()
    n = 2

    while len(primes) < limit:
        if is_prime(n):
            primes.append(n)
        n += 1

    print_time_log(start_time, primes[limit - 1])
    return primes[limit - 1]
示例#8
0
def main(max_limit: int):

    start_time = time.time()
    circulars = set()
    primes = [n for n in range(1, max_limit)
              if is_prime(n)
              and is_circular_pattern(n)]

    for n in primes:
        if n not in circulars and is_circular(n):
            for p in get_rotations(n):
                circulars.add(p)

    result = len(circulars)
    print_time_log(start_time, result)
    return result
示例#9
0
def is_circular(digit: int, debug=False):
    """
    The number, 197, is called a circular prime
    because all rotations of the digits: 197, 971, and 719,
    are themselves prime.

    :param digit:
    :param debug:
    :return:
    """

    results = list()
    combinations = get_rotations(digit)

    for digit in combinations:
        result = is_prime(digit)
        if result and debug:
            print('result: {0}, digit: {1}'.format(result, digit))
        results.append(result)

    return all(results)
示例#10
0
 def test_is_prime_4(self):
     self.assertEqual(False, is_prime(4))
示例#11
0
 def test_is_prime_2(self):
     self.assertEqual(True, is_prime(2))
示例#12
0
"""Pandigital Primes
"""

import math, itertools
from utils.utils import get_list_of_primes, is_prime


def is_pandigital(x):
    num_digits = math.ceil(math.log10(x + 1))
    s = set(str(x))
    return len(s) == num_digits and "0" in s


# * Heuristic: in base 10, the max thing can be 9 digits long, so you can terminate then
# * Heuristic: In fact, it can't be 9 or 8 digits as sum of 1-9 is 45, which is divisible by 3 and to test if something's divisible by 3 you sum the digits
# * Same for 8 - 36
# * Heuristic: There's significantly fewer n-digit pandigital numbers than numbers in general, so generate them rather than getting all primes
primes = get_list_of_primes(1000)
cur_max = 0
for n in range(7, 0, -1):
    for number in itertools.permutations(range(1, n + 1), n):
        number = int(str(''.join(map(str, number))))
        if n == 4:
            print(number)
        if is_prime(number, primes):
            cur_max = max(cur_max, number)
    if cur_max > 0:
        break
print(cur_max)