Last active
February 4, 2022 17:25
-
-
Save mayonesa/e017988b52c4e6198a4980164c691791 to your computer and use it in GitHub Desktop.
Sum of all the multiples of 3 or 5 (up to 1000)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import annotation.tailrec | |
object Mults3or5sum extends App { | |
private val limit = 1000 | |
private def mults(multOf: Int) = { | |
@tailrec | |
def mults(i: Int, acc: Set[Int]): Set[Int] = { | |
val mult = multOf * i | |
if (mult >= limit) acc | |
else mults(i + 1, acc + mult) | |
} | |
mults(1, Set.empty) | |
} | |
println((mults(3) ++ mults(5)).sum) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment