3 ms·
Arguably the Right Answer wouldn't filter on a generator of all permutations but would generate the permutations in a way that didn't include invalid ones to be
by contravariant 6y ago
Arguably the Right Answer wouldn't filter on a generator of all permutations but would generate the permutations in a way that didn't include invalid ones to begin with.
Something like:
def extend(perm):
for k in range(1,11):
if k not in perm and isprime(k + perm[-1]):
yield [*perm, k]
perms = [[]]
for _ in range(10):
perms = [extend(perm) for perm in perms]
(This is just a draft, but you get the gist)
- Smaug123 6y agoOh, absolutely, but the article is predicated on using the most naive brute force.