To start with, here's a short Scala program that solves the first problem of Project Euler : If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.
Scala is particularly well suited for problems that are defined recursively, or that can solved by recursive algorithms. Here are functions that calculate the factorial of a number (n!) and the numbers of the Fibonacci sequence.
This recursive "fibonacci" code is considered a "natural" implementation as it mirrors the mathematical definition of the function very closely. However this recursive version is very inefficient, and will become slow very quickly for growing n. Consequently it is not unusual in functional languages to replace inefficient recursive functions with equivalent tail recursive versions. Here is a tail recursive version of "fibonacci":
With this version the Scala compiler can detect the tail recursion and perform tail call optimization. The generated byte code will be no more inefficient than the code that is generated from the following imperative version.