Easy
You are given positive integers n
and m
.
Define two integers, num1
and num2
, as follows:
num1
: The sum of all integers in the range [1, n]
that are not divisible by m
.num2
: The sum of all integers in the range [1, n]
that are divisible by m
.Return the integer num1 - num2
.
Example 1:
Input: n = 10, m = 3
Output: 19
Explanation: In the given example:
Example 2:
Input: n = 5, m = 6
Output: 15
Explanation: In the given example:
Example 3:
Input: n = 5, m = 1
Output: -15
Explanation: In the given example:
Constraints:
1 <= n, m <= 1000
class Solution {
fun differenceOfSums(n: Int, m: Int): Int {
var sum1 = 0
var sum2 = 0
for (i in 1..n) {
if (i % m == 0) {
sum1 += i
} else {
sum2 += i
}
}
return sum2 - sum1
}
}