Skip to content

Project Euler Problem 42

November 26, 2011

Project Euler Problem 42 solution:

open System.IO

let isTriangleNumber n = 
    let product = n*2
    let candidate = int (floor (sqrt (float product)))
    candidate * (candidate + 1) = product

let wordValue (word: string) =
    let baseValue = int '@'
    word |> Seq.fold (fun wval letter ->
                wval + int letter - baseValue) 0

let readData (path: string) =  
    use sr = new StreamReader(path)    
    sr.ReadLine().Split(',')
    
let problem042 () =
    readData @"..\..\..\Datafiles\Problem042.data"
    |> Array.map (fun x -> x.Replace("\"",""))  
    |> Array.map wordValue
    |> Array.filter isTriangleNumber
    |> Array.length
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: