Problem · Math

Sum Multiples of 3, 5, or 7 Below N

Learn this problem
EasyUpstart logoUpstartFULLTIMEOA

Problem statement

Given a positive integer n, return the sum of all positive integers strictly less than n that are divisible by 3, 5, or 7.

Count an integer only once even when it is divisible by more than one of the three divisors.

Function

sumMultiples(n: int) → long

Examples

Example 1

n = 12return = 40

The included integers are 3, 5, 6, 7, 9, 10. Their sum is 40; 12 is excluded because the bound is strict.

Example 2

n = 16return = 81

The included integers are 3, 5, 6, 7, 9, 10, 12, 14, 15. The value 15 is counted once even though both 3 and 5 divide it.

Example 3

n = 3return = 0

No positive integer below 3 is divisible by 3, 5, or 7.

Constraints

  • 1 <= n <= 10^9
  • The result fits in a signed 64-bit integer.

More Upstart problems

drafts saved locally
public long sumMultiples(int n) {
    // Write your code here.
}
n12
expected40
checking account