Skip to content

Project Euler Problem 52

November 28, 2011

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

From → Project Euler

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: