예제 #1
0
def is_concatenated_prime(new, existing):
    """
    >>> is_concatenated_prime(7, 109)
    True
    """
    new_first = int(new * 10**(get_number_length(existing)) + existing)

    new_last = int(existing * 10**(get_number_length(new)) + new)

    return is_prime(new_first) \
        and is_prime(new_last)
예제 #2
0
def main():
    """ challenge077 """
    nums = dict()

    num = 2
    while True:
        sums = set()

        # Is the current number a prime?
        if is_prime(num):
            sums.add(tuple([num]))

        # Check for previous sums
        for num_a in range(2, num // 2 + 1):
            num_b = num - num_a

            # Collect each a sum to each b sum
            for ab_sums in [
                    sorted(aSum + bSum) for aSum in nums[num_a]
                    for bSum in nums[num_b]
            ]:
                sums.add(tuple(ab_sums))

        nums[num] = sums

        if len(sums) > 5000:
            return num

        num += 1
예제 #3
0
def main():
    """ challenge046 """
    odd_composite = 3
    while True:

        # Find doubled squares lower than odd_composite
        if not has_criteria(odd_composite):
            return odd_composite

        odd_composite += 2
        while odd_composite == 1 or is_prime(odd_composite):
            odd_composite += 2
예제 #4
0
def main():
    """ challenge041 """
    # loop through number of digits
    highest = 0
    for number_size in [4, 7]:
        chars = "".join([str(c) for c in range(1, number_size + 1)])
        for potential in [int(p)
                          for p in permutate(chars)
                          if is_prime(int(p))]:
            # if potential > highest:
            # Potential is always higher in this scenario.
            highest = potential

    return highest
예제 #5
0
def main():
    """ challenge118 """
    perms = ["1", "2", "3", "5", "7"]
    for size in range(2, 10):
        perms.extend([perm for perm in permutations("123456789", size)
                      if perm[-1] != 2 and perm[-1] != "5"])

    perms = ["".join(p) for p in perms]

    primes = [prime for prime in perms if is_prime(int(prime))]

    sets = build_sets("", primes)

    return sets
예제 #6
0
def has_criteria(potential):
    """
    >>> has_criteria(9)
    True
    >>> has_criteria(5777)
    False
    """
    limit = int(sqrt(potential // 2))
    for square_doubled in [2 * i**2 for i in range(limit, 0, -1)]:
        diff = potential - square_doubled
        if diff == 1 or is_prime(diff):
            return True

    return False
예제 #7
0
파일: main.py 프로젝트: mattjhussey/pemjh
def is_truncated_prime(potential):
    """
    >>> is_truncated_prime(3797)
    True
    >>> is_truncated_prime(3798)
    False
    """
    divisor = 1
    # Check first digit is prime
    while True:
        trunc = potential // divisor
        if trunc <= 0:
            break
        if trunc == 1 or not is_prime(trunc):
            return False
        divisor *= 10

    while (potential % divisor) > 0:
        trunc = potential % divisor
        if trunc == 1 or not is_prime(potential % divisor):
            return False
        divisor /= 10

    return True
예제 #8
0
def main():
    """ challenge051 """
    step = cycle([2, 4])
    current = 5
    while True:
        # Check current
        if is_prime(current) and has_3_same_digits(current):
            # Substitute 0 to 9
            word = str(current)
            for i in range(0, 10):
                number_of_primes, smallest = substitute_primes(word,
                                                               str(i))
                if number_of_primes == 8:
                    return smallest

        current += next(step)
예제 #9
0
파일: main.py 프로젝트: mattjhussey/pemjh
def main():
    """ challenge111 """
    # pylint: disable=invalid-name
    M = [8, 9, 8, 9, 9, 9, 9, 9, 8, 9]
    S = []

    for i in range(10):
        s = 0
        # use M[i] to build up all possible numbers
        for m in [list(("%0" + str(10 - M[i]) + "d") % m)
                  for m in range(0, 10**(10 - M[i]))]:
            if not any(int(c) == i for c in m):
                for num in [int("".join(b))
                            for b in build_nums([str(i)] * M[i], m)]:
                    if num >= 10**(9) and is_prime(num):
                        # Check each for primality
                        s += num
        S.append(s)

    return sum(S)
예제 #10
0
def substitute_primes(template, substitute):
    """
    >>> substitute_primes("13", "1")
    (6, 13)
    >>> substitute_primes("56223", "2")
    (7, 56003)
    """
    count = 0
    smallest = int(template)
    for i in range(0, 10):
        # Swap out the substitute for 0 - 9
        working = template.replace(substitute, str(i))
        # Check if prime
        if working[0] != "0":
            working = int(working)
            if is_prime(int(working)):
                if working < smallest:
                    smallest = working
                count += 1

    return count, smallest
예제 #11
0
def main():
    """ challenge058 """
    current = 1
    prime_count = 0
    diagonals = 1

    # Cycle through layers
    sidestep = 2
    while True:
        for _ in range(1, 4):
            current += sidestep

            if is_prime(current):
                prime_count += 1

        current += sidestep

        diagonals += 4

        if prime_count * 10 < diagonals:
            return sidestep + 1

        sidestep += 2
예제 #12
0
def main():
    """ challenge027 """
    limit = 999

    maximum_a = 0
    maximum_b = 0

    maximum = 1
    primes = list(sieved_primes(limit + 1))
    for prime in primes:
        for a_side in range(-prime, limit + 1):
            root = 1
            while True:
                potential_prime = root * (root + a_side) + prime
                if not is_prime(potential_prime):
                    break
                root += 1

            if root > maximum:
                maximum = root
                maximum_a = a_side
                maximum_b = prime

    return maximum_a * maximum_b
예제 #13
0
def test_is_prime(input, expected):
    actual = is_prime(input)
    expect(actual).to.eq(expected)