# Project Euler Problem 52

Project Euler Problem 52 solution is very straightforward: beginning with 1 check each subsequent natural number if its multiples by 1 to 6 have same length and are built of same digits; upon finding the first with this property stop.

Here I used **set** data structure for checking lengths and constituents of multiples.

open System let wanted n = let multiples = [1;2;3;4;5;6] |> List.map (fun x -> (x * n).ToString()) let multiplesHaveSameLength = (multiples |> List.map String.length |> set |> Set.count) = 1 let multiplesHaveSameDigits = let digitSets = multiples |> List.map (fun x -> x.ToCharArray() |> set) |> List.toArray [for i in 0..4 -> digitSets.[i].Equals(digitSets.[i+1])] |> List.reduce (&&) multiplesHaveSameLength && multiplesHaveSameDigits let problem052 () = Seq.initInfinite id |> Seq.skip 1 |> Seq.find wanted

Advertisements

Leave a Comment