samskivert: Euler 001

09 January 2008

I've been doing the Project Euler problems in Scala both because they're fun and as an excuse to play more with Scala and to remind myself of how much fun it is to write functional programs.

I figured it would be fun to post my solutions, inelegant as they may be. Anyone also doing the problems will have to avert their eyes if they see a solution to a problem they've not yet solved.

Now, on to Problem 001:

object Euler1 extends Application {
  def div3or5 (from: Int, to: Int): List[Int] = {
    for (i <- List.range(from, to) if i % 3 == 0 || i % 5 == 0) yield i
  }
  println(div3or5(0, 1000).foldLeft(0)(_+_))
}

©1999–2022 Michael Bayne