classSolution:
defconsecutiveNumbersSum(self, n: int) -> int:
ans =0 k =1while k * (k -1) //2< n:
if (n - k * (k -1) //2) % k ==0:
ans +=1 k +=1return ans
1
2
3
4
5
6
7
8
9
10
11
classSolution {
publicintconsecutiveNumbersSum(int n) {
int ans = 0;
for (int k = 1; k * (k - 1) / 2 < n; ++k) {
if ((n - k * (k - 1) / 2) % k == 0) {
ans++;
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution {
public:int consecutiveNumbersSum(int n) {
int ans =0;
for (long k =1; k * (k -1) /2< n; ++k) {
if ((n - k * (k -1) /2) % k ==0) {
++ans;
}
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
funcconsecutiveNumbersSum(nint) int {
ans:=0fork:=1; k*(k-1)/2 < n; k++ {
if (n-k*(k-1)/2)%k==0 {
ans++ }
}
returnans}