Project Euler Problem 62 Solution


The cube, 4106362541063625 (3453345^3), can be permuted to produce two other cubes: 5662310456623104 (3843384^3) and 6643012566430125 (4053405^3). In fact, 4106362541063625 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.


import Data.List (sort)
import qualified Data.Map as Map

cubes :: Map.Map String [Integer]
cubes = Map.fromListWith (++) [(sort (show cube), [cube]) | x <- [1..10000], let cube = x^3]

main :: IO ()
main = print $ minimum [minimum ns | (_, ns) <- Map.toList cubes, length ns == 5]
$ ghc -O2 -o cubic-permutations cubic-permutations.hs
$ time ./cubic-permutations
real   0m0.046s
user   0m0.044s
sys    0m0.000s


#!/usr/bin/env python
from collections import defaultdict
def cube(x): return x**3

def main():
    cubes = defaultdict(list)
    for i in range(10000):
        c = cube(i)
        digits = ''.join(sorted([d for d in str(c)]))
    print(min([min(v) for k, v in list(cubes.items()) if len(v) == 5]))

if __name__ == "__main__":
$ time python3
real   0m0.070s
user   0m0.064s
sys    0m0.000s

Questions? Comments? Send me an email: [email protected]