# Code language: Python classSolution: defconsecutiveNumbersSum(self, n: int) -> int: cnt = 1 for k inrange(2, n): a = (2 * n // k + 1 - k) // 2 if a < 1: break elif n == (2 * a + k - 1) * k // 2: cnt += 1 return cnt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Code language: Java classSolution { publicintconsecutiveNumbersSum(int _n) { // (2 * a + k - 1) * k / 2 = n // (2 * n / k + 1 - k) / 2 = a intcnt=1; for (longk=2, n = _n; ; ++k) { longa= (2 * n / k + 1 - k) / 2; if (a < 1) break; longs= (2 * a + k - 1) * k / 2; if (s == n) ++cnt; } return cnt; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Code language: C++ classSolution { public: intconsecutiveNumbersSum(int n){ int cnt = 1; for (long k = 2; ; ++k) { long a = (2 * n / k + 1 - k) / 2; if (a < 1) break; long s = (2 * a + k - 1) * k / 2; if (s == n) ++cnt; } return cnt; } };
1 2 3 4 5 6 7 8 9 10 11
// Code language: C intconsecutiveNumbersSum(int n){ int cnt = 1; for (long k = 2; ; ++k) { long a = (2 * n / k + 1 - k) / 2; if (a < 1) break; long s = (2 * a + k - 1) * k / 2; if (s == n) ++cnt; } return cnt; }
1 2 3 4 5 6 7 8 9 10 11 12 13
// Code language: C# publicclassSolution { publicintConsecutiveNumbersSum(int n) { int cnt = 1; for (long k = 2; ; ++k) { long a = (2 * n / k + 1 - k) / 2; if (a < 1) break; long s = (2 * a + k - 1) * k / 2; if (s == n) ++cnt; } return cnt; } }