Sum of Non-Divisible Numbers
00:00
Given two positive integers n and k, calculate the sum of all natural numbers from 1 to n (inclusive) that are not divisible by k.
Your solution must run efficiently to handle large inputs without timing out. Think about how you can use mathematical formulas to solve this in constant time.
Constraints
Examples
Input → 10
3
Output → 37
Explanation:
Numbers from 1 to 10 are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. The numbers divisible by 3 are 3, 6, 9. The sum of the remaining numbers is 1 + 2 + 4 + 5 + 7 + 8 + 10 = 37.
Input → 5
2
Output → 9
Explanation:
Numbers from 1 to 5 are: 1, 2, 3, 4, 5. The numbers divisible by 2 are 2 and 4. The sum of the remaining numbers is 1 + 3 + 5 = 9.
Input → 1
5
Output → 1
Explanation:
The only number is 1, which is not divisible by 5. The sum is 1.