Skip to content

Project Euler Problem 22

November 16, 2011

Project Euler Problem 22 solution is straightforward. The only auxiliary function needed is alphavalue that for any string calculates its alphabetic value. Names are stripped from whitespace, sorted, products of alphabetic value by position are calculated and finally added together:

open System.IO

let alphavalue (s: string) =
    s.ToCharArray() |> (fun x -> int x - int 'A' + 1) |> Array.sum

let readData (path: string) =
    use sr = new StreamReader(path)

let problem022 () =
    readData @"..\..\..\Datafiles\"
    |> (fun x -> x.Replace("\"",""))
    |> Array.sort
    |> Array.mapi (fun i x -> int64((alphavalue x) * (i + 1)))
    |> Array.sum

From → Project Euler

Leave a Comment

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: