Classic problem #2652EasyMath
Sum Multiples
Real-world version: Marks Struck Off by Three, Five or SevenTracked in this browser only
Read Sum Multiples on LeetCode(opens in a new tab)
The statement here is our own: the same inputs, outputs and technique.
A tally sheet is marked 1 through n.
A mark is struck off when it divides evenly by 3, by 5 or by 7. Return the total of the marks struck off.
Examples
Example 1
- Input
- n = 143
- Output
- 5523
Every multiple of 3, 5 or 7 up to 143 is struck off, and their total comes to this once the overlaps have been settled.
Example 2
- Input
- n = 15
- Output
- 81
The marks struck off are 3, 5, 6, 7, 9, 10, 12, 14 and 15, which total 81. The mark 15 divides by both 3 and 5 yet is counted once.
Example 3
- Input
- n = 2
- Output
- 0
Neither mark divides by 3, 5 or 7.
Constraints
- 1 <= n <= 10^3
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
Python
def sum_of_multiples(n: int) -> int:Java
public int sumOfMultiples(int n)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 11 test cases: 11 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's