Scala-Euler Problem1

Andrew Bolster

Senior R&D Manager (Data Science) at Synopsys Software Integrity Group and Treasurer @ Bsides Belfast and NI OpenGovernment Network

Finished my approach to Euler Problem 1 last night and checked everything into github.

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.

Was not happy with limiting the specific factors of 3 and 5, and limit of 1000 so i implemented a generalised solution.

def solution(fs: List[Int], max: Int ) = ( 1 until max ).filter( f => fs.exists( n => f % n == 0)).foldLeft(0)(_+_)

Where fs is a List of integer factors and max is obv the highest limit. eg(Answer is in white font, so select the line “Answer” to see it

print("Answer: ",solution(List(3,5),1000))
>Answer: <span style="color: #ffffff;"><strong>233168</strong></span>

Thanks to @lichtsprung for the testing help.

The general aim is to complete a few dozen of these in Scala, then switch to Java, or C/C++, or something else, and loop around until i have a complete library of euler problems for all of the languages i end up using; Nothing like having long term goals!

Published: May 27 2009