-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfailed_p60_1.py
95 lines (70 loc) · 2.48 KB
/
failed_p60_1.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import functools
import itertools
from dataclasses import dataclass
from itertools import chain, zip_longest
from random import randint
from typing import FrozenSet, Set, Tuple
from pyprimes import isprime, nth_prime
FiveTuple = Tuple[int, int, int, int, int]
@dataclass(frozen=True)
class EnumeratedPrime:
index: int
prime: int
def __repr__(self):
return f"P<{self.prime}>"
FivePrimes = Tuple[
EnumeratedPrime, EnumeratedPrime, EnumeratedPrime, EnumeratedPrime, EnumeratedPrime
]
def memoize(obj):
cache = obj.cache = {}
@functools.wraps(obj)
def memoizer(*args, **kwargs):
if args not in cache:
cache[args] = obj(*args, **kwargs)
return cache[args]
return memoizer
def dtldarek(fiver: FiveTuple) -> int:
strings = list(map(reversed, map(str, fiver)))
return int(
("".join(list(chain.from_iterable(zip_longest(*strings, fillvalue="0")))))[::-1]
)
def dtldarek_inverse(num: int) -> FiveTuple:
groups = [int(str(num)[-i::-5][::-1] or "0") for i in range(1, 6)]
return groups
def convert_to_primes(group: FiveTuple) -> FivePrimes:
return tuple(EnumeratedPrime(index, nth_prime(index)) for index in group)
BADPAIRS: Set[FrozenSet[int]] = set()
@memoize
def prime_group_good(prime_set: FrozenSet[EnumeratedPrime]) -> bool:
global BADPAIRS
ret = True
for prime1, prime2 in itertools.combinations(prime_set, 2):
if not (
isprime(int(str(prime1.prime) + str(prime2.prime)))
and isprime(int(str(prime2.prime) + str(prime1.prime)))
):
BADPAIRS.add(frozenset((prime1.index, prime2.index)))
ret = False
print(BADPAIRS)
return ret
def has_bad_pair(groups: FiveTuple) -> bool:
global BADPAIRS
for index1, index2 in itertools.combinations(groups, 2):
if frozenset((index1, index2)) in BADPAIRS:
return True
return False
if __name__ == "__main__":
for num in itertools.count():
groups: FiveTuple = dtldarek_inverse(num)
if (
len(set(groups)) != 5
or 0 in groups
or 1 in groups
or 3 in groups
or has_bad_pair(groups)
):
continue
prime_group: FivePrimes = convert_to_primes(groups)
print(prime_group)
if prime_group_good(frozenset(prime_group)):
print(prime_group, sum(prime_group))