Given a positive integer n
, return the sum of all integers in the range [1, n]
inclusive that are divisible by 3
, 5
, or 7
.
n
is less than 1
?
n
is defined as a positive integer, this scenario should not occur.n
?
n
is reasonably large, we should ensure our implementation efficiently handles it.Assuming these points hold, let’s proceed to the strategy and code.
1
to n
.3
, 5
, or 7
.n
starts from 1
, an edge case might be where n
is just 1
or slightly higher. The code should correctly handle these minimal edge cases.def sum_of_multiples(n: int) -> int:
total_sum = 0
for i in range(1, n + 1):
if i % 3 == 0 or i % 5 == 0 or i % 7 == 0:
total_sum += i
return total_sum
O(n)
1
to n
once, making this a linear time operation.O(1)
This approach should efficiently handle typical constraints by leveraging a straightforward iteration and conditional checks.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?