Skip to content

Instantly share code, notes, and snippets.

@mayonesa
Last active February 4, 2022 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mayonesa/e017988b52c4e6198a4980164c691791 to your computer and use it in GitHub Desktop.
Save mayonesa/e017988b52c4e6198a4980164c691791 to your computer and use it in GitHub Desktop.
Sum of all the multiples of 3 or 5 (up to 1000)
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