> For the complete documentation index, see [llms.txt](https://hao-fu-1.gitbook.io/oj/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hao-fu-1.gitbook.io/oj/math/829.-consecutive-numbers-sum.md).

# 829. Consecutive Numbers Sum

https\://leetcode.com/problems/consecutive-numbers-sum/

问有多少种等差（d == 1）数列和会是N。等差数列求和公式是m\[2x + (m - 1)]/2, x是首项，m是项数。因此遍历所有可能的m即可。

```cpp
/*
 * @lc app=leetcode id=829 lang=cpp
 *
 * [829] Consecutive Numbers Sum
 */

// @lc code=start
class Solution {
public:
    int consecutiveNumbersSum(int N) {
        int res = 0;
        for (int m = 1; ; ++m) {
            int x = N - m * (m - 1) / 2;
            if (x <= 0) break;
            if (x % m == 0) ++res;
        }
        return res;
    }
};
// @lc code=end


```
