Project Euler – Problem 62 Solution

Yan Cui

I help clients go faster for less using serverless technologies.

Problem

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.

Solution

open System

let cubeRoot (n:int64) = Math.Pow(double(n), 1.0/3.0)

// define function to investigate the numbers of d digits which are cubes
let f d =
    // find the min & max number whose cube is d digits long
    let min, max = int64(cubeRoot (pown 10L d)), int64(cubeRoot (pown 10L (d+1)))

    // and look for groups of 5 numbers which are cubes and permutations of one another
    [min..max]
    |> List.map (fun n -> pown n 3)
    |> Seq.groupBy (fun n -> n.ToString().ToCharArray() |> Array.sort)
    |> Seq.filter (fun (k, l) -> l |> Seq.length = 5)

let answer =
    // go through the numbers of a given number of digits and find the first set of groups
    // of 5 numbers which are cubes and permutations of one another
    let groups =
        Seq.unfold (fun state -> Some(state, state+1)) 7
        |> Seq.map f
        |> Seq.filter (fun l -> Seq.length l > 0)
        |> Seq.head

    // find the smallest elements in the groups
    groups |> Seq.map (fun (k, l) -> l |> Seq.min) |> Seq.min

 

Whenever you’re ready, here are 4 ways I can help you:

  1. If you want a one-stop shop to help you quickly level up your serverless skills, you should check out my Production-Ready Serverless workshop. Over 20 AWS Heroes & Community Builders have passed through this workshop, plus 1000+ students from the likes of AWS, LEGO, Booking, HBO and Siemens.
  2. If you want to learn how to test serverless applications without all the pain and hassle, you should check out my latest course, Testing Serverless Architectures.
  3. If you’re a manager or founder and want to help your team move faster and build better software, then check out my consulting services.
  4. If you just want to hang out, talk serverless, or ask for help, then you should join my FREE Community.

 


2 thoughts on “Project Euler – Problem 62 Solution”

Leave a Comment

Your email address will not be published. Required fields are marked *