|
| 1 | +""" |
| 2 | +Prime permutations |
| 3 | +
|
| 4 | +Problem 49 |
| 5 | +
|
| 6 | +The arithmetic sequence, 1487, 4817, 8147, in which each of |
| 7 | +the terms increases by 3330, is unusual in two ways: |
| 8 | +(i) each of the three terms are prime, |
| 9 | +(ii) each of the 4-digit numbers are permutations of one another. |
| 10 | +
|
| 11 | +There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, |
| 12 | +exhibiting this property, but there is one other 4-digit increasing sequence. |
| 13 | +
|
| 14 | +What 12-digit number do you form by concatenating the three terms in this sequence? |
| 15 | +
|
| 16 | +Solution: |
| 17 | +
|
| 18 | +First, we need to generate all 4 digits prime numbers. Then greedy |
| 19 | +all of them and use permutation to form new numbers. Use binary search |
| 20 | +to check if the permutated numbers is in our prime list and include |
| 21 | +them in a candidate list. |
| 22 | +
|
| 23 | +After that, bruteforce all passed candidates sequences using |
| 24 | +3 nested loops since we know the answer will be 12 digits. |
| 25 | +The bruteforce of this solution will be about 1 sec. |
| 26 | +""" |
| 27 | + |
| 28 | +from itertools import permutations |
| 29 | +from math import floor, sqrt |
| 30 | + |
| 31 | + |
| 32 | +def is_prime(number: int) -> bool: |
| 33 | + """ |
| 34 | + function to check whether the number is prime or not. |
| 35 | + >>> is_prime(2) |
| 36 | + True |
| 37 | + >>> is_prime(6) |
| 38 | + False |
| 39 | + >>> is_prime(1) |
| 40 | + False |
| 41 | + >>> is_prime(-800) |
| 42 | + False |
| 43 | + >>> is_prime(104729) |
| 44 | + True |
| 45 | + """ |
| 46 | + |
| 47 | + if number < 2: |
| 48 | + return False |
| 49 | + |
| 50 | + for i in range(2, floor(sqrt(number)) + 1): |
| 51 | + if number % i == 0: |
| 52 | + return False |
| 53 | + |
| 54 | + return True |
| 55 | + |
| 56 | + |
| 57 | +def search(target: int, prime_list: list) -> bool: |
| 58 | + """ |
| 59 | + function to search a number in a list using Binary Search. |
| 60 | + >>> search(3, [1, 2, 3]) |
| 61 | + True |
| 62 | + >>> search(4, [1, 2, 3]) |
| 63 | + False |
| 64 | + >>> search(101, list(range(-100, 100))) |
| 65 | + False |
| 66 | + """ |
| 67 | + |
| 68 | + left, right = 0, len(prime_list) - 1 |
| 69 | + while left <= right: |
| 70 | + middle = (left + right) // 2 |
| 71 | + if prime_list[middle] == target: |
| 72 | + return True |
| 73 | + elif prime_list[middle] < target: |
| 74 | + left = middle + 1 |
| 75 | + else: |
| 76 | + right = middle - 1 |
| 77 | + |
| 78 | + return False |
| 79 | + |
| 80 | + |
| 81 | +def solution(): |
| 82 | + """ |
| 83 | + Return the solution of the problem. |
| 84 | + >>> solution() |
| 85 | + 296962999629 |
| 86 | + """ |
| 87 | + prime_list = [n for n in range(1001, 10000, 2) if is_prime(n)] |
| 88 | + candidates = [] |
| 89 | + |
| 90 | + for number in prime_list: |
| 91 | + tmp_numbers = [] |
| 92 | + |
| 93 | + for prime_member in permutations(list(str(number))): |
| 94 | + prime = int("".join(prime_member)) |
| 95 | + |
| 96 | + if prime % 2 == 0: |
| 97 | + continue |
| 98 | + |
| 99 | + if search(prime, prime_list): |
| 100 | + tmp_numbers.append(prime) |
| 101 | + |
| 102 | + tmp_numbers.sort() |
| 103 | + if len(tmp_numbers) >= 3: |
| 104 | + candidates.append(tmp_numbers) |
| 105 | + |
| 106 | + passed = [] |
| 107 | + for candidate in candidates: |
| 108 | + length = len(candidate) |
| 109 | + found = False |
| 110 | + |
| 111 | + for i in range(length): |
| 112 | + for j in range(i + 1, length): |
| 113 | + for k in range(j + 1, length): |
| 114 | + if ( |
| 115 | + abs(candidate[i] - candidate[j]) |
| 116 | + == abs(candidate[j] - candidate[k]) |
| 117 | + and len(set([candidate[i], candidate[j], candidate[k]])) == 3 |
| 118 | + ): |
| 119 | + passed.append( |
| 120 | + sorted([candidate[i], candidate[j], candidate[k]]) |
| 121 | + ) |
| 122 | + found = True |
| 123 | + |
| 124 | + if found: |
| 125 | + break |
| 126 | + if found: |
| 127 | + break |
| 128 | + if found: |
| 129 | + break |
| 130 | + |
| 131 | + answer = set() |
| 132 | + for seq in passed: |
| 133 | + answer.add("".join([str(i) for i in seq])) |
| 134 | + |
| 135 | + return max([int(x) for x in answer]) |
| 136 | + |
| 137 | + |
| 138 | +if __name__ == "__main__": |
| 139 | + print(solution()) |
0 commit comments