Sum Multiples (LeetCode 2652): Solution and Explanation

Easy
Math

Given a positive integer n, return the sum of every integer in [1, n] that is divisible by 3, 5 or 7.

Intuition

The multiples of k up to n are k, 2k, ..., mk with m = n // k. Factor out k and the rest is 1 + 2 + ... + m, so their sum is k * m(m + 1) / 2. Call it f(k).

f(3) + f(5) + f(7) is close, but a number like 15 is a multiple of both 3 and 5, so it got added twice. Subtracting f(15), f(21) and f(35) removes one copy of every number shared by two of the divisors.

Multiples of 105 are shared by all three. They were added three times and then subtracted three times, so they dropped out completely. Adding f(105) once puts them back. That is inclusion-exclusion over three sets.

Approach: Inclusion-exclusion with arithmetic series

  1. Define f(k) = k * m * (m + 1) / 2 with m = n // k.
  2. Start with f(3) + f(5) + f(7).
  3. Subtract f(15) + f(21) + f(35) for numbers counted twice.
  4. Add f(105) back for numbers that the subtraction removed entirely.
  5. Return the total.

Step-by-step example

n = 7
  1. f(3): m = 2, multiples 3 and 6, sum 9.
  2. f(5): m = 1, just 5, total 14.
  3. f(7): m = 1, just 7, total 21.
  4. f(15), f(21), f(35) and f(105) are all 0 because n is smaller than each of them.
  5. Return 21, which is 3 + 5 + 6 + 7.

Code: Python, Java, C++

1class Solution:
2    def sumOfMultiples(self, n: int) -> int:
3        def f(k: int) -> int:
4            m = n // k
5            return k * m * (m + 1) // 2
6
7        total = f(3) + f(5) + f(7)
8        total -= f(15) + f(21) + f(35)
9        total += f(105)
10        return total
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Inclusion-exclusion with arithmetic series O(1)
Simple loop O(n)
Mark multiples O(n)
Time

O(1)

Seven calls to a formula that does one division and two multiplications.

Space

O(1)

A running total and a few temporaries.

Other approaches

Simple loop (time O(n), space O(1))

Walk i from 1 to n and add i when i % 3, i % 5 or i % 7 is 0. With n at most 1000 this is fast and easy to get right.

Mark multiples (time O(n), space O(n))

Use a boolean array of size n + 1, mark every multiple of 3, 5 and 7, then sum the marked indices. It avoids double counting without any subtraction but uses extra memory.

Common mistakes

  • Adding f(3) + f(5) + f(7) and stopping, which counts 15, 21, 30, ... twice.
  • Subtracting the pair overlaps but forgetting to add f(105) back, which drops 105, 210, ... from the answer.
  • Using floating point division for m in JavaScript without Math.floor.

Frequently asked questions

Why add f(105) back after subtracting?

A multiple of 105 is divisible by 3, 5 and 7. It is added three times by f(3), f(5), f(7) and subtracted three times by f(15), f(21), f(35), so its net count is zero. One more f(105) makes it one.

Why use 15, 21 and 35 rather than other numbers?

Since 3, 5 and 7 are prime, a number divisible by both 3 and 5 is exactly a multiple of 15. In general you would use the least common multiple of each pair.

Can the sum overflow a 32-bit integer?

No. For n = 1000 the answer is 272066, and the largest single term f(3) is 166833.