Solution to problem 62 in Python

This commit is contained in:
David Doblas Jiménez 2022-07-25 19:24:43 +02:00
parent ad0cd08228
commit 8ff023990b

39
src/Python/Problem062.py Normal file
View File

@ -0,0 +1,39 @@
#!/usr/bin/env python3
"""
Created on 25 Jul 2022
@author: David Doblas Jiménez
@email: daviddoji@pm.me
Solution for problem 62 of Project Euler
https://projecteuler.net/problem=62
"""
from collections import defaultdict
from utils import timeit
@timeit("Problem 62")
def compute():
"""
The cube, 41063625 (345^3), can be permuted to produce two other cubes:
56623104 (384^3) and 66430125 (405^3). 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.
"""
cubes = defaultdict(list)
for number in range(345, 10_000):
cube_str = "".join(sorted(list(str(number**3))))
cubes[cube_str].append(number**3)
if len(cubes[cube_str]) == 5:
return min(cubes[cube_str])
if __name__ == "__main__":
print(f"Result for Problem {int(62):003d}: {compute()}")