Iterating and caching
The main issue with the Brute force approach is that it checks the same permutations multiples times and many of them are not cubes, making it very inefficient.
To overcome this issue, we can use a similar approach to Problem 0049 which involves:
- Generating all cubes \( n^3 \).
- Grouping the cubes with the same permutation together.
- If one of the groups has at least \( 5 \) cubes, the solution is found.
From solution2.py:
def cubic_permutations():
permutations = defaultdict(list)
for cube in (i**3 for i in itertools.count(1)): # Condition 1
ordered_digits = "".join(sorted(str(cube)))
permutations[ordered_digits].append(cube) # Condition 2
if len(permutations[ordered_digits]) >= 5: # Condition 3
return min(permutations[ordered_digits])
Note that the function actually returns the smallest cube of the first group with at least \( 5 \) cubes. While this is sufficient for this problem, it will not necessarily provide the correct answer with groups of bigger sizes, like \( 6 \).