Sum Multiples - Problem

Given a positive integer n, find the sum of all integers in the range [1, n] inclusive that are divisible by 3, 5, or 7.

Return an integer denoting the sum of all numbers in the given range satisfying the constraint.

Input & Output

Example 1 — Basic Case
$ Input: n = 10
Output: 40
💡 Note: Numbers divisible by 3, 5, or 7 in range [1,10]: 3, 5, 6, 7, 9, 10. Sum = 3+5+6+7+9+10 = 40
Example 2 — Smaller Range
$ Input: n = 7
Output: 25
💡 Note: Numbers divisible by 3, 5, or 7 in range [1,7]: 3, 5, 6, 7. Sum = 3+5+6+7 = 21
Example 3 — Edge Case
$ Input: n = 3
Output: 3
💡 Note: Only number 3 is divisible by 3, 5, or 7 in range [1,3]. Sum = 3

Constraints

  • 1 ≤ n ≤ 106

Visualization

Tap to expand
Sum Multiples: Find sum of numbers divisible by 3, 5, or 7Input: n = 10Process: Check each numberOutput: 40Range: [1, 10]Numbers 1-10:12345678910Green = divisible by 3, 5, or 7Valid numbers:3, 5, 6, 7, 9, 10Sum calculation:3+5+6+7+9+10 = 40Algorithm: Check divisibility by 3 OR 5 OR 7Result: 40
Understanding the Visualization
1
Input
Given positive integer n
2
Process
Find all numbers divisible by 3, 5, or 7
3
Output
Return sum of qualifying numbers
Key Takeaway
🎯 Key Insight: Use inclusion-exclusion principle to handle overlapping multiples efficiently
Asked in
Google 25 Amazon 18
24.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen