-
Notifications
You must be signed in to change notification settings - Fork 0
/
049.py
57 lines (51 loc) · 1.74 KB
/
049.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from collections import defaultdict
from utility import primes, digits_of, from_digits, all_pairs
def find_arithmetic_sequences(numbers, length):
"""
Find arithmetic sequences in the given numbers of the specified length or longer.
>>> list(find_arithmetic_sequences([1, 2, 3], 3))
[(1, 2, 3)]
>>> list(find_arithmetic_sequences([1, 3, 2], 3))
[(1, 2, 3)]
>>> list(find_arithmetic_sequences([1, 4, 5, 9, 10], 3))
[(1, 5, 9)]
"""
numbers = sorted(numbers)
numbers_set = set(numbers)
for a, b in all_pairs(numbers):
assert a < b
sequence = [a, b]
diff = b - a
c = b + diff
while c in numbers_set:
sequence.append(c)
c = c + diff
if len(sequence) >= length:
yield tuple(sequence)
break
if __name__ == '__main__':
# Generate all four digit primes.
four_digit_primes = []
for p in primes():
if p < 1000:
continue
four_digit_primes.append(p)
if p >= 10000:
break
# Group primes by similar digits.
primes_by_digits = defaultdict(list)
for p in four_digit_primes:
primes_by_digits[frozenset(digits_of(p))].append(p)
# Strip out digit groups with less than three primes.
for digits, primes in list(primes_by_digits.items()):
if len(primes) < 3:
del primes_by_digits[digits]
# Look for arithmetic sequences of length 3.
solutions = []
for primes in primes_by_digits.values():
for sequence in find_arithmetic_sequences(primes, 3):
digits = []
for n in sequence:
digits.extend(digits_of(n))
solutions.append(from_digits(digits))
print(max(solutions))