Euler and F#


I’ve been looking for some new problems to work on in F# to get more comfortable with the language.  I’ve been rather slack of late because of other projects but I had a little bit of time this week.  I decided it would be fun to join the crowd and play away at the problems on the project euler site.  That being said, answer #1.

module Euler =
    let problem1() =
        let test i = 
            match (0 = i % 3) || (0 = i % 5) with
                | true -> Some i
                | false -> None
        let targetSeq = Seq.choose test [0..999]
        Seq.sum targetSeq

Comments (4)

  1. brilsmurf says:

    Here’s my solution:

    let p1_solution() =

     {0..999}

     |> Seq.filter (fun x -> x%3=0 || x%5=0)

     |> Seq.sum

    IMHO Seq.filter is more logical than Seq.choose in this case.

  2. jaredpar says:

    @brilsmurf,

    Thanks for pointing me to that.  I spent roughly 30 minutes looking for this function but read right past the documentation and was stuck with choose.