Freigeben über


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

  • Anonymous
    September 08, 2008
    PingBack from http://housesfunnywallpaper.cn/?p=4551

  • Anonymous
    September 08, 2008
    PingBack from http://blog.a-foton.ru/2008/09/euler-and-f/

  • Anonymous
    September 08, 2008
    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.

  • Anonymous
    September 08, 2008
    @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.