I'm in the process of learning Scala, and started working through a series of coding puzzles that I've done previously in other languages.
For example, this solution, coded in Erlang:
sum_multiples(Max, Max, Acc) -> io:format("The sum of all the multiples of 3 or 5 below ~p: ~p~n", [Max, Acc]); sum_multiples(Current, Max, Acc) when (Current rem 3 == 0) or (Current rem 5 == 0) -> sum_multiples(Current + 1, Max, Current + Acc); sum_multiples(Current, Max, Acc) -> sum_multiples(Current + 1, Max, Acc).
Now, doing this same problem in Scala, I wrote this:
val max = 1000
println(s"The sum of all the multiples of 3 or 5 below ${max}: " + sum_multiples_recursive(1, max, 0))
def sum_multiples_recursive(current: Int, max: Int, acc: Int): Int = {
if(current < max){
if(current % 3 == 0 || current % 5 == 0){
sum_multiples_recursive(current + 1, max, current + acc)
} else {
sum_multiples_recursive(current + 1, max, acc)
}
} else {
acc
}
}
I'd like to see what an experienced Scala coder might write, what Scala idioms might come into play.