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

# 829. 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


```
