Created
January 25, 2025 16:08
-
-
Save igorvanloo/6aa24a2f897b44c608e019ee8fcaf188 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solve(part, j, S, S_set, S_all_sets, P): | |
if j == len(part): | |
S_all_sets.append(S_set) | |
return S_set | |
for x in P[part[j]]: | |
if all(S[int(i)] + 1 != 2 for i in str(x)): | |
SS = [S[k] for k in range(10)] | |
for i in str(x): | |
SS[int(i)] += 1 | |
if x == max(S_set + [x]): | |
solve(part, j + 1, SS, S_set + [x], S_all_sets, P) | |
def Method1(): | |
P = [[] for _ in range(10)] | |
for k in range(1, 10): | |
for x in itertools.permutations("123456789", k): | |
v = int("".join(x)) | |
if is_prime(v): | |
P[k].append(v) | |
total = 0 | |
for part in partition(9, [1,2,3,4,5,6,7,8,9]): | |
S_all_set = [] | |
S = [1,0,0,0,0,0,0,0,0,0] | |
solve(part[::-1], 0, S, [], S_all_set, P) | |
total += len(S_all_set) | |
return total | |
def Method2(): | |
P = [[] for _ in range(10)] | |
for k in range(1, 10): | |
for x in itertools.permutations("123456789", k): | |
v = int("".join(x)) | |
if is_prime(v): | |
P[k].append("".join(x)) | |
total = 0 | |
for part in partition(9, [1,2,3,4,5,6,7,8,9]): | |
for a in itertools.product(*[P[i] for i in part]): | |
v = "".join(a) | |
v1 = [int(j) for j in a][::-1] | |
if v1 == sorted(v1): | |
if all([str(i) in v for i in range(1, 10)]): | |
total += 1 | |
return total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment