Scala: Project Euler: Problem 1
November 22nd, 2009
I have been learning Scala since couple of months, but every time i get pulled away from the learning phase it took me some time to get back into context. So this time wanted to work on some code in Scala instead of learning (I know I should have done this since the start …). I recently came to know about Project Euler (http://projecteuler.net) which I can say is excellent set of problems to solve when learning the syntax and semantics of a language. So I wanted to document my progress in a series of posts starting this one. Any kind of feedback is appreciated.
Problem 1:
Add all the natural numbers below one thousand that are multiples of 3 or 5.
object Problem1 {
def main(args : Array[String]) : Unit = {
val sum = 1.until(1000)
.filter(x => (x % 3 == 0 || x % 5 == 0))
.reduceLeft(_+_)
println(sum)
}
}