一、Java
java
class Solution {
public int differenceOfSums(int n, int m) {
return (1+n)*n/2-n/m*(m+n/m*m)/2;
}
}
二、C++
cpp
class Solution {
public:
int differenceOfSums(int n, int m) {
return (1+n)*n/2-n/m*(m+n/m*m);
}
};
三、Python
python
class Solution:
def differenceOfSums(self, n: int, m: int) -> int:
return (1+n)*n//2-n//m*(m+n//m*m)
四、JavaScript
python
var differenceOfSums = function(n, m) {
let k=Math.floor(n/m);
return (1+n)*n/2-(m+m*k)*k;
};
五、Go
Go
func differenceOfSums(n int, m int) int {
return (1+n)*n/2-n/m*(m+n/m*m)
}