FastPrepSum Multiples of 3, 5, or 7

Sum Multiples of 3, 5, or 7

Abridge logoAbridge● EasyFULLTIMEOA
Learn

Problem statement

Given a positive integer n, return the sum of all positive integers from 1 through n, inclusive, that are divisible by 3, 5, or 7.

Count a number only once even when more than one of those divisors divides it.

Function

sumMultiples(n: int) → long

Examples

Example 1

n = 7return = 21

The included values are 3, 5, 6, 7, whose sum is 21.

Example 2

n = 10return = 40

The included values are 3, 5, 6, 7, 9, 10. Each value is counted once.

Example 3

n = 1return = 0

No value in the inclusive range is divisible by 3, 5, or 7.

Constraints

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

More Abridge problems

See Abridge hiring insights
public long sumMultiples(int n) {
    // Write your code here.
}
n7
expected21
Checking account…