The cube, 41063625 (3453), can be permuted to produce two other cubes: 56623104 (3843) and 66430125 (4053). In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.
Find the smallest cube for which exactly five permutations of its digits are cube.
The following code can find find three permutations under a second, but it's taking very long for finding five permutations.
How do I improve runtime?
#! /usr/bin/env python
import itertools
def is_cube(n):
return round(n ** (1 / 3)) ** 3 == n
def main():
found = False
n = 100
while not found:
n += 1
cube = n ** 3
perms = [
int("".join(map(str, a)))
for a in itertools.permutations(str(cube))
]
perms = [perm for perm in perms if len(str(perm)) == len(str(cube))]
filtered_perms = set(filter(is_cube, perms))
if len(filtered_perms) == 5:
found = True
print(filtered_perms)
if __name__ == "__main__":
main()