Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Multiples of 3 or 5

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\).

Solution

The only tricky part of this exercise comes from the fact that we cannot simply count the multiplies of \(3\), then calculate the multiplies of \(5\), and add these two numbers up. If we did that, then the numbers that are divisible by both 3 and 5 would be calculated twice.

In this solution, we create three sets of numbers. If a number of divisible by both \(3\) and \(5\), then it’s divisible by \(15\), so we can find those numbers and subtract them from the two other sets.

object Euler001 extends EulerApp {
  override def execute(): Int = {
    val threes = (3 until 1000 by 3).sum
    val fives = (5 until 1000 by 5).sum
    val fifteens = (15 until 1000 by 15).sum

    threes + fives - fifteens
  }
}

The problem can be also solved by checking all the numbers to see if they’re divisible by \(3\) or \(5\). Shorter, nothing gets counted twice, but you may find it harder to read.

(3 until 1000).filter(n => n % 3 == 0 || n % 5 == 0).sum