# Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers?
#
# Example 1:
#
# Input: 5
# Output: 2
# Explanation: 5 = 5 = 2 + 3
# Example 2:
#
# Input: 9
# Output: 3
# Explanation: 9 = 9 = 4 + 5 = 2 + 3 + 4
# Example 3:
#
# Input: 15
# Output: 4
# Explanation: 15 = 15 = 8 + 7 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5
import math


class Solution(object):
    # assuem starting from x, then we get (x+0) + (x+1) + (x+2) + .. + (x+i-1) = N
    # => ix + i * (i-1)/2 = N, => ix = N - i*(i-1)/2
    # since ix > 0, we can get i*(i-1)/2 < N => i * (i-1) < 2N => i * i < 2N
    # hence i < 2N ** 0.5, also if (N-i*(i-1)/2) % i == 0, we could find x
    def consecutiveNumbersSum(self, N):
        """
        :type N: int
        :rtype: int
        """
        # base on the test case, 5 = 5, 5 = 2 + 3, so for any N, N itself is a solution
        # so result start from 1
        res = 1
        # bugfixed, notice the boundary, sqrt(15)=5.47, int(5.47)=5, you wont'
        # be able to get 5
        # also i start from 2, based on the previous equation: (x+0)+..+(x+i-1) = N
        # when i==1, it is x + 0 = N already considered, so starts from 2
        for i in xrange(2, int(math.sqrt(2 * N)) + 1):
            if (N - i * (i - 1) / 2) % i == 0:
                res += 1

        return res


if __name__ == '__main__':
    print Solution().consecutiveNumbersSum(15)
