Problem Description
Given a positive integer n, compute the sum of all integers within the range [1, n] (inclusive) that are divisible by 3, 5, or 7.
Key Insights
- We need to iterate through every number in the range from 1 to n.
- Check divisibility by 3, 5, or 7 for each number.
- Sum the numbers that meet the criteria.
- The constraints allow a simple linear solution since n is at most 10^3.
Space and Time Complexity
Time Complexity: O(n) - We iterate once through the numbers from 1 to n. Space Complexity: O(1) - Only a few variables are used regardless of the input size.
Solution
The solution utilizes a simple loop that iterates from 1 to n and checks each number for divisibility by 3, 5, or 7 using the modulo operator. If a number is found to be divisible by any of these, it is added to the cumulative sum. This approach leverages basic arithmetic operations and conditional checks, which is efficient given the problem constraints.