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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code