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
Note: 1 <= N <= 10 ^ 9.

Difficulty: interview

Starter code:

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        

Solutions:
class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        res = 1
        
        # Remove all even factors
        while N % 2 == 0:
            N //= 2
        
        # Count all odd factors
        idx = 3        
        while idx * idx <= N:
            count = 0
            
            # found an odd factor
            while N % idx == 0:
                N //= idx
                count += 1
            
            res *= count + 1
            idx += 2
        
        return res if N == 1 else res * 2

import math
class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        num = N
        if num == 1:
            return 1
        counter = 1
        for i in range(2, math.floor(math.sqrt(num) + 1)):
            if num % i == 0:
                if i % 2 == 1:
                    counter += 1
                c = num // i
                if i != c and c % 2 == 1:
                    counter += 1
        if num % 2 == 1:
            counter += 1

        return counter

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        
        k = 1
        count = 0
        while N / k - (k / 2) > 0:
            print(k)
            if k % 2 != 0 and N % k == 0:
                count += 1
            elif N / k == N // k + 0.5:
                count += 1
            k += 1
        return count
        
        #15 = 15 = 8 + 7 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5
        # 15 / 1 = 15
        # 15 / 2 = 7.5 = 8 + 7
        # 15 / 3 = 5 = 4+5+6
        # 15 / 4 = 3. 75 not use
        # 15 / 5 = 3    ( 3 - 5 / 2) > 0
        # 15 / 6 = 2.5    (2.5 - 6 / 2) < 0  not use


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        # number of ways to write N as a sum of consecutive posititive integers
        # with k consecutive integers, you can have
        # 1, 2, ..., k or 2, 3, ..., k + 1 or 3, 4, ..., k + 2
        # 1, 2, ..., k or 1, 2, ..., k, k or 1, 2, ..., k, k, k
        # => N can be written as a sum of k consecutive integers if (N - (1 + 2 + ... k)) mod k == 0
        # (1 + 2 + ... k) = k (k + 1) / 2
        num_ways = 1
        n = 2
        while (n * (n + 1) / 2) <= N:
            if (N - (n * (n + 1) / 2)) % n == 0:
                num_ways += 1
            n += 1
        return num_ways

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        ans = 0
        for i in range(1, N+1):
            x = (2*N/i-i+1)/2
            if x <= 0: break
            if x == int(x):
                # print(i,x)
                ans += 1
        return ans

#math
#time:O(sqr(N))
'''
x + (x+1) + (x+2)+...+ k terms = N
kx + k*(k-1)/2 = N ->
kx = N - k*(k-1)/2 ->
N-k*(k-1)/2>0
'''
class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        k,count = 1,0
        while N > k*(k-1)/2:
            if (N - k*(k-1)/2)%k == 0:
                count += 1
            k += 1
        return count

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        # res = 0
        # # explain math
        # upper_limit = ceil((2 * N + 0.25)**0.5 - 0.5)
        # for k in range(upper_limit):
        #     N -= k
        #     if N % k == 0:
        #         res += 1
        # return res
        res, k = 0, 1
       
        while (k*(k+1) <= 2*N):
            x = float(N)/k-(k+1)/2.0
            if ((2*N)%k == 0 and x.is_integer() ):
                res += 1
                #print(k,x)
            k+=1
        return res

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        counter = 0
        i = 0

        while N - (i * (i + 1) // 2) > 0:
            if (N - (i * (i + 1) // 2)) % (i + 1) == 0:
                counter += 1

            i += 1

        return counter

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        B = (2 * N + 0.25) ** 0.5 + 0.5
        B = ceil(B)
        ans = 0
        for k in range(1, B+1):
            a = 2*N - k ** 2
            b = (2*N - k ** 2) // k
            c = b + 1
            if a % k == 0 and c % 2 == 0:
                m = c // 2
                if m > 0:
                    ans += 1
                    #print(m, k)
        return ans

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        count = 1
        n = 2
        while((2*N + n - n*n) > 0):
            start = (2*N + n - n**2)/(2*n)
            if(start - int(start) == 0):
                count += 1
            n+=1
        return count


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        num = N
        if num == 1:
            return 1
        counter = 1
        for i in range(2, int(num ** 0.5 + 1)):
            if num % i == 0:
                if i % 2 == 1:
                    counter += 1
                c = num // i
                if i != c and c % 2 == 1:
                    counter += 1
        if num % 2 == 1:
            counter += 1

        return counter

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        n = int((1 + math.sqrt(1+8*N))/2)
        print(n)
        ans = 0
        for i in range(1, n):
            if (2*N + i - i**2) != 0 and (2*N + i - i**2) % (2*i) == 0:
                ans += 1
        
        return ans

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        l = 1
        out = 0
        # range start a, end b
        # l = b - a + 1
        while (1 + l) * l / 2 <= N:
            b = (2*N + l**2 - l) / (2*l)
            a = b + 1 - l
            if a == int(a) and b == int(b):
                out += 1
            l += 1
        return out

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        n = 1
        ans = 0
        
        def helper(n):
            return n * (n + 1) / 2
        
        while helper(n) <= N:
            ans += int((N - helper(n)) % n == 0)
            n += 1
        
        return ans


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        
        total = 1 # no need to for N
        
        N = 2*N
        
        for i in range(2, int(N**.5) + 1): # from 2 to sqrt of N
            if N % i == 0 and ((i + (N/i)) % 2 == 1):
                total += 1
                
            
        return total

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        target = 2 * N 
        results = 0
        for k in range(1, int(sqrt(target))+1): 
            if target % k == 0:
                num = (target / k) - k - 1 
                if num >= 0 and num % 2 == 0:
                    results += 1
        return results

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        count, upper_limit = 0, int(math.sqrt(2*N + 1/4) - 1/2)
        for i in range(1,upper_limit + 1):
            N -= i
            if not N % i:
                count+=1
        return count

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        count = 0
        upper_limit = ceil((2 * N + 0.25)**0.5 - 0.5) + 1
        for k in range(1, upper_limit):
            N -= k
            if N % k == 0:
                count += 1
        return count
                


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        res = 0
        m = int(sqrt(2*N)+1)
        for i in range(1,m):
            N -= i
            if N % i == 0:
                res += 1
        return res

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        def canwrite(N, n):
            x = (N -  n * (n+1) / 2)
            return  x >=0 and x% n == 0
        res = 0
        m = int(sqrt(2*N)+1)
        for i in range(1,m):
            N -= i
            if N % i == 0:
                res += 1
        return res

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        ans = 0
        for k in range (1, int(sqrt(2 * N)) + 1):
            if 2 * N % k == 0:
                x = 2 *N // k - k - 1
                if x % 2 == 0 and x >= 0:
                    ans += 1
        return ans 

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        res = 1
        i = 3
        while N % 2 == 0:
            N /= 2
        while i * i <= N:
            count = 0
            while N % i == 0:
                N /= i
                count += 1
            res *= count + 1
            i += 2
        return res if N == 1 else res * 2

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        last_check = ceil(sqrt(2*N))
        num_ways = 0
        for i in range(1,last_check):
            if (N*2) % i == 0 and (i % 2 == 1 or (N*2/i) % 2 == 1):
                num_ways += 1
        return num_ways


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        
        res = 1
        n=N
        for i in range(2, math.floor((1 + (1+8*n)**0.5) / 2)):
            
            if ((n/i) - (i-1)/2).is_integer():
                res += 1

        return res

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        count = 0 
        limit = ceil(((2*N + 0.25)**0.5) - 0.5) + 1
        for k in range(1, limit):
            if ((N/k) - (k+1)/2).is_integer():
              count = count + 1
        return count

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
#         (2S + C - 1) * C / 2 = N
#         (2S + C - 1) C =  2N
        cnt = 0
        for c in range(1, floor(sqrt(2 * N)) + 1):
            if (2 * N) % c == 0:
                if ((2 * N / c) - c) % 2 == 1:
                    cnt += 1
                print(c)
        return cnt


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        
        res = 1
        n=N
        for i in range(2, int((1 + (1+8*n)**0.5) / 2)):
            
            if ((n/i) - (i-1)/2).is_integer():
                res += 1

        return res

# N = (x + 1) + ... + (x + k)
# N = x k + k(k + 1)/2
class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        count = 0
        # x > 0 --> N/k - (k + 1)/2 > 0
        upper_limit = ceil((2 * N + 0.25)**0.5 - 0.5) + 1
        for k in range(1, upper_limit):
            # x should be integer
            if (N - k * (k + 1) // 2) % k == 0:
                count += 1
        return count

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        # 2N = 2xk + k(k + 1) => 2N = k(2x + k + 1) => x = N/k - (k + 1)/2 => 2N >= k(k + 1)
        # => 2N + 1/2 >= (k + 1/4)^2 => k <= sqrt(2N + 1/4) - 1/2
        count = 0
        limit = ceil(sqrt(2 * N + 0.25) - 0.5)
        for k in range(1, limit + 1):
            if (N - k * (k + 1) // 2) % k == 0:
                count += 1
        return count

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        count = 0
        # x > 0 --> N/k - (k + 1)/2 > 0
        upper_limit = ceil((2 * N + 0.25)**0.5 - 0.5) + 1
        for k in range(1, upper_limit):
            # x should be integer
            if (N - k * (k + 1) // 2) % k == 0:
                count += 1
        return count

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        count = 1
        upper_limit = ceil((2*N + 1)**0.5)
        for k in range(2, upper_limit):
            if (N - k * (k - 1) // 2) % k == 0:
                count += 1
        return count

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        count = 0
        upper_limit = ceil((2*N + 0.25)**0.5-0.5)+1
        for k in range(1, upper_limit):
            if (N-k*(k+1)//2)% k==0:
                count +=1
        return count


class Solution:
    def consecutiveNumbersSum(self, N):
        res = 1
        i = 3
        while N % 2 == 0:
            N /= 2
        while i * i <= N:
            count = 0
            while N % i == 0:
                N /= i
                count += 1
            res *= count + 1
            i += 2
        return res if N == 1 else res * 2

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        count = 0
        bound = int((2*N + 0.25)**0.5 - 0.5) + 1
        for k in range(1, bound):
            if (N - k*(k + 1) // 2) % k == 0:
                count += 1
        return count

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        ans = 0
        upper_limit = ceil((2 * N + 0.25)**0.5 - 0.5) + 1
        for x in range(1,upper_limit):
            if (2*N - x*x + x) % (2*x) == 0:
                ans += 1
        return ans

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        ans = 1
        cur_iter = 1
        while True:
            N -= cur_iter
            res = N / (cur_iter+1)
            if res < 1:
                break
            if res.is_integer():
                ans += 1
            cur_iter += 1
        return ans


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        ans = 0
        upper_limit = ceil((2 * N + 0.25)**0.5 - 0.5) + 1
        for x in range(1,upper_limit):
            if (2*N - x*x + x) % (2*x) == 0 and (2*N - x*x + x) / (2*x) > 0:
                ans += 1
        return ans

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        counter = 0
        
        for num_terms in range(1, N+1):
            difference = num_terms*(num_terms-1)//2
            remainder = N - difference
            if remainder <= 0: break
            if remainder%num_terms==0:
                counter += 1

        return counter

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        count = 1;
        for k in range(2, int((2*N)**0.5)+1):
            if ( ( N - ( k * ( k - 1 )/2) ) % k == 0):
                count += 1
        return count


class Solution:
    def consecutiveNumbersSum(self, x: int) -> int:
        ans = 1
        for n in range(2, 100000):
            # if n % 2 == 0:
                
            a = x - n * (n - 1) // 2
            b = n
            if a < b:
                break
            if a % b == 0:
                ans += 1
        return ans

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        
        ini = 2
        re = 0
        
        while True:
            
            m = (N/ini) - (ini-1)/2
            
            if m <= 0:
                break
            
            elif m%1 == 0:
                re += 1
            
            ini += 1
        
        return re+1


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        count = 0
        max_k = ceil((2 * N + 1/4) ** 0.5 - 0.5) + 1
        for k in range(1, max_k):
            if (N - k * (k + 1) / 2) % k == 0:
                count += 1
        return count


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        ans, constants, div = 0, 0, 0
        for i in range(N):
            div += 1
            constants = constants + i
            quotient, remainder = divmod((N-constants), div)
            if quotient <= 0:
                return ans
            if not remainder:
                ans += 1
        return ans

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:

        res = 1
        i = 3
        while N % 2 == 0:
            N /= 2
        while i * i <= N:
            count = 0
            while N % i == 0:
                N /= i
                count += 1
            res *= count + 1
            i += 2
        return res if N == 1 else res * 2

# N = (x + 1) + ... + (x + k)
# N = x k + k(k + 1)/2
class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        count = 0
        # x > 0 --> N/k - (k + 1)/2 > 0
        upper_limit = ceil((2 * N + 0.25)**0.5 - 0.5) + 1
        for k in range(1, upper_limit):
            # x should be integer
            if ((N - k * (k + 1) // 2) / k)%1 == 0:
                count += 1
        return count

class Solution:
    def consecutiveNumbersSum(self, x: int) -> int:
        ans = 1
        for n in range(2, 100000):
            a = 2 * x - n * (n - 1)
            b = 2 * n
            if a < b:
                break
            if a % b == 0:
                ans += 1
        return ans

'''
可能性：
1個數字：s
2個數字：s + s+1
3個數字：s + s+1 + s+2
...
n個數字：s + ...

=>
n
2n+1
3n+1+2
4n+1+2+3
...
=>
N = kn + (k-1 + 1)*(k-1)/2
=>
N = kn + (k**2-k)/2
=>
n = (N-(k**2-k)/2)/k
n>0且n為整數則符合

'''
class Solution1:
    def consecutiveNumbersSum(self, N: int) -> int:
        res = 0
        
        for k in range(1, 10**9):
            t = (k**2-k)/2
            s = (N-t)/k

            if s <= 0:
                break
            
            if s.is_integer():
                res += 1
        
        return res
# refactor
class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        res = 0
        
        for k in range(1, 10**9):
            s = N - (k*k-k)/2
            if s <= 0:
                break

            if s % k == 0:
                res += 1
                continue

        
        return res

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        ans, constants, div = 0, 0, 0
        for i in range(N):
            div = i + 1
            constants = constants + i
            quotient, remainder = divmod((N-constants), div)
            if quotient <= 0:
                return ans
            if not remainder:
                ans += 1
        return ans

class Solution:
    def consecutiveNumbersSum(self, n: int) -> int:
        counts = 0
        i = 1
        while True:
            calc = (i*i - i) / 2
            if calc >= n:
                break
            if (n - calc) % i == 0:
                counts += 1
            i += 1
        print((2.2222 % 1))
        return counts


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        #Look like start with some number, add up til sum = N
        #k + (k+1) + (k+2) + (k+i) = N
        #k*i + (0+1+2+..+i) = N
        #k*i + (i(i+1)/2) = N
        #k * i = (N - (i(i+1)/2)) -> full chunk % i == 0 -> k is start point
        
        i, ans = 1, 0
        while N > i * (i - 1) // 2:
            if (N - i * (i + 1) // 2) % i == 0:
                ans += 1
            i += 1
        return ans

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
#         if N < 3: return 1
#         l = 1
#         r = 0
#         s = 0
#         ret = 0
#         while r < (N + 1) // 2:
#             r += 1
#             s += r
            
#             while s > N:
#                 s -= l
#                 l += 1
#             if s == N:
#                 ret += 1
#         return ret + 1
        ans = 0
        k = 1
        while True:
            a, b = divmod(N, 2 * k)
            if a >= k and b == k:
                ans += 1
            elif a < k:
                break
            a, b = divmod(N, 2 * k + 1)
            if a >= k + 1 and b == 0:
                ans += 1
            elif a < k + 1:
                break
            k += 1
        return ans + 1

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        if not N:
            return 0
        
        count = 0
        idx = 1
        while (idx*(idx+1)//2 <= N):
            if (N - idx*(idx+1)//2) % idx == 0:
                count += 1
            idx += 1
        return count
        


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        i = 1
        res = 0
        while N > i*(i-1)//2:
            if (N - i*(i-1)//2) % i == 0:
                res += 1
            i += 1
        return res
            
            
# N = k + k+1 + ... k+i-1
# N = k*i + (0+i-1)*i/2
# N - i*(i-1)/2 = k*i


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        k = 1
        num_ways = 0
        while k * (k - 1) // 2 < N:
            if  (N - k * (k - 1) // 2) % k == 0:
                num_ways += 1
            k += 1
        return num_ways

import math
class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        a=N
        while a%2==0:
            a=a//2
        s=math.sqrt(a)
        i=1
        ans=0
        while i<s:
            if a%i==0:
                ans+=1
            i+=2
        if i*i==a:
            return 2*ans+1
        else:
            return 2*ans

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        res = 0
        k = 1
        while N - k * (k + 1) // 2 >= 0:
            if (N - k * (k + 1) // 2) % k == 0:
                res += 1
            k += 1
        
        return res

import math
class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        count = 0
        for n in range(1,math.ceil(-0.5+math.sqrt(0.25+2*N))):
            start = N/(n+1) - n/2
            if math.isclose(0, start - math.floor(start)):
                count += 1
        return count+1

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        def verify_even(k):
            return N % k != 0 and (2 * N) % k == 0
        
        def verify_odd(k):
            return N % k == 0
        
        count = 0
        
        # Find all odd factors
        for i in range(1, int(math.sqrt(2 * N)) + 1, 1):
            if i % 2 == 0 and verify_even(i):
                count += 1
            elif i % 2 == 1 and verify_odd(i):
                count += 1
                
                
        return count

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        n = 0
        ans = 0
        while (N - n*(n+1)//2) > 0:
            if (N - n*(n+1)//2)%(n+1) == 0:
                ans +=1
            n +=1
        return ans


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        def canwrite(N, n):
            x = (N -  n * (n+1) / 2)
            return  x >=0 and x% n == 0
        res = 0
        for i in range(1,int(sqrt(2*N)+1)):
            if canwrite(N,i):
                res += 1
        return res

import math
class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        k = 1.0
        count = 0
        while k <= math.sqrt(2*N + 1/4) - 1/2:
            x = N/k - (k +1)/2
            if x.is_integer():
                count += 1
                print(k, x)
            k += 1
        return count

import math
class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        k = 1.0
        count = 0
        while k <= math.sqrt(2*N + 1/4) - 1/2:
            x = N/k - (k +1)/2
            if x.is_integer():
                count += 1
            k += 1
        return count

class Solution:
    # Backtrack
    def consecutiveNumbersSum(self, num: int) -> int:
        count = 1

        queue = collections.deque()
        for i in range(1, num):
            queue.append((i, i))  # cur, sum

        while queue:
            cur, total = queue.popleft()
            # add the next number
            if total + cur + 1 == num:
                count += 1
            elif total + cur + 1 < num:
                queue.append((cur + 1, total + cur + 1))
        return count
    
    # N = (x + 0) + (x + 2) + ... + (x + k - 1)
    # N = x * k + (k - 1) * k / 2 -> N - (k - 1) * k / 2 = x * k.
    # So as long as N - (k - 1) * k / 2 is x times of k, there is a solution.
    # Iterate all possible values of k in the rage [1, k * (k - 1) / 2 < N]
    def consecutiveNumbersSum(self, N: int) -> int:
        count = 0
        k = 1
        while k * (k - 1) / 2 < N:
            if (N - k * (k - 1) / 2) % k == 0:
                count += 1
            k += 1
        return count


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        # up till N // 2 + 1
        # output is at least 1
        # if N == 1 or N == 2:
        #     return 1
        # start = N // 2 + 1
        # result = 1
        # candidates = [i for i in range(start,0,-1)]
        # n = 2
        # i = 0
        # while True:
        #     if i+n > start:
        #         return result
        #     # print(candidates[i:i+n])
        #     temp_sum = (candidates[i] + candidates[i+n-1]) * n / 2
        #     if temp_sum == N:
        #         result += 1
        #         i += 1
        #         n += 1
        #     elif temp_sum > N:
        #         i += 1
        #     else:
        #         n += 1
        res = 1
        i = 3
        while N % 2 == 0:
            N /= 2
        while i * i <= N:
            count = 0
            while N % i == 0:
                N /= i
                count += 1
            res *= count + 1
            i += 2
        return res if N == 1 else res * 2
            


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        i = 1
        res = 0
        while N > i*(i-1)/2:
            if (N - i*(i-1)/2) % i == 0:
                res += 1
            i += 1
        return res
            
            
# N = k + k+1 + ... k+i-1
# N = k*i + (0+i-1)*i/2
# N - i*(i-1)/2 = k*i


def sum_up_to(n):
    return n * (n + 1) // 2

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        k = 1
        num_ways = 0
        while k * (k - 1) // 2 < N:
            if  (N - sum_up_to(k - 1)) % k == 0:
                num_ways += 1
            k += 1
        #def compute_floor():
        #    return (N - sum_up_to(k - 1)) // k
        #floor = compute_floor()
        #while floor > 0:
        #    this_sum = sum_up_to(floor + k - 1) - sum_up_to(floor - 1)
        #    if this_sum == N:
        #        num_ways += 1
        #    floor = compute_floor()
        return num_ways

#x + (x+1) + (x+2) + ... + (x+k-1) = N
#k * (k - 1) / 2 = (k**2 - k) / 2 < k**2


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
#         if N == 1: 
#             return 1
#         res = 1
#         for i in range(2, floor(1 + N ** (0.5))):   
#             if N % i == 0:
#                 if i % 2 == 1: # If i is odd, then we can form a sum of length i
#                     res += 1
#                 j = (N // i) # Check the corresponding N // i
#                 if i != j and j % 2 == 1:
#                     res += 1
#         if N % 2 == 1: # If N is odd(2k + 1). Then N = k + k + 1, not included above
#             res += 1
        
#         return res
        
#         if N == 1:
#             return 1
#         total = 1
        
#         start = 1
#         end = 1
        
#         curr = 1
#         while end <= ceil(N / 2):
#             if curr == N:
#                 total += 1
#                 curr -= start
#                 start += 1
#             if curr < N:
#                 end += 1
#                 curr += end
#             elif curr > N:
#                 curr -= start
#                 start += 1
        
#         return total
                
        
        c,k = 0,0
        while( k * (k+1) / 2 < N):
            r = N- k * (k + 1) / 2
            if  r % (k+1) == 0: c+=1
            k +=1
            
        return c

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        count = 1
        L = 1
        while( L * (L + 1) < 2 * N): 
            a = (1.0 * N - (L * (L + 1) ) / 2) / (L + 1) 
            if (a ==int(a) ): 
                count += 1
            L += 1
        return count


import math
class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        res = 1
        maxK = int(math.sqrt(N)) * 2
        for k in range(2, maxK+1):
            if (k*(k - 1 )/2) >= N:
                continue
            if (N - (k*(k - 1 )/2)) % k == 0:
                print(k)
                res += 1
        

        return res

'''
可能性：
1個數字：s
2個數字：s + s+1
3個數字：s + s+1 + s+2
...
n個數字：s + ...

=>
n
2n+1
3n+1+2
4n+1+2+3
...
=>
N = kn + (k-1 + 1)*(k-1)/2
=>
N = kn + (k**2-k)/2
=>
n = (N-(k**2-k)/2)/k
n>0且n為整數則符合

'''
class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        res = 0
        
        for k in range(1, 10**9):
            t = (k**2-k)/2
            s = (N-t)/k

            if s <= 0:
                break
            
            if s.is_integer():
                res += 1
        
        return res

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        count = 0
        L = 1
        while( L * (L + 1) < 2 * N): 
            a = (1.0 * N - (L * (L + 1) ) / 2) / (L + 1) 
            if (a - int(a) == 0.0): 
                count += 1
            L += 1
        return count + 1

class Solution:
    def consecSumIncludeA(self, N: int, A: int) -> List[int]:
        csum = A
        c = A
        while csum < N:
            c += 1
            csum += c
        if csum == N:
            return 1
        return 0
    
    def consecutiveNumbersSumtimeout(self, N: int) -> int:
        if N == 1:
            return 1
        uplim = int(N/2)
        if N%2==1:
            uplim = int((N//2)+1)
        fin = 1
        for ii in range(1,uplim+1):
            # print(\"ii now: {}; fin now: {}\".format(ii,fin))
            fin += self.consecSumIncludeA(N-ii,ii+1)
        return fin
    
    def consecutiveNumbersSum(self, N: int) -> int:
        # if N = sum([1,...,x])+x.a, then N can be expressed as a sum of x consecutive numbers
        if N == 1:
            return 1
        combo = 1 
        fin = 1
        form = 2
        sumsf = 1
        while form < 2*N:
            # print(\"form: {}, fin: {}\".format(form, fin))
            combo += 1
            if (N-sumsf)%combo == 0:
                fin += 1
            form = combo+combo**2
            sumsf += combo
        return fin


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        if N == 1: 
            return 1
        res = 1
        for i in range(2, int(N ** .5 + 1)):   
            if N % i == 0:
                if i % 2 == 1: # If i is odd, then we can form a sum of length i
                    res += 1
                j = (N // i) # Check the corresponding N // i
                if i != j and j % 2 == 1:
                    res += 1
        if N % 2 == 1: # If N is odd(2k + 1). Then N = k + k + 1, not included above
            res += 1
        
        return res
        


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        l = 0
        
        ans = 0
        
        while l*(l+1)/2 < N:
            a = (N-l*(l+1)/2) / (l+1)
            if int(a) - a == 0:
                ans += 1
            l += 1
        
        return ans
    


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        i, ans = 1, 0
        while N > i * (i - 1) // 2:
            if (N - i * (i - 1) // 2) / i == (N - i * (i - 1) // 2) // i:
                ans += 1
            i += 1
        return ans

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        counter = 0
        n = 2
        cnum = []

        while n * (n + 1) / 2 <= N:
            conditionNo = int(N - n * (n + 1) / 2)
            if conditionNo % n == 0:
                counter = counter + 1
            n = n + 1
        return counter+1

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        k = 1
        num_ways = 0
        floor = (N - k * (k - 1) // 2) // k
        while floor > 0:
            this_sum = (floor + k - 1) * (floor + k) // 2 - floor * (floor - 1) // 2
            if this_sum == N:
                num_ways += 1
            k += 1
            floor = (N - k * (k - 1) // 2) // k
        return num_ways


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        cnt = 0
        upper_limit = ceil((2*N + 1/4)**0.5 - 0.5 + 1)
        for k in range(1, upper_limit):
            x = (2*N - k**2 - k)/(2*k)
            if int(x) == x and x >= 0:
                cnt += 1
        return cnt

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        count = 1
        L = 1
        while( L * (L + 1) < 2 * N): 
            a = (1.0 * N - (L * (L + 1) ) / 2) / (L + 1) 
            if (a ==int(a) ): 
                count += 1
            L += 1
        return count

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        self.primes=[2,3,5,7,11]
        self.get_primes(N)
        # print(self.primes)
        self.get_factors(2*N)
        # print(self.factor_arr)
        ans=0
        for factor in self.factor_arr:
            x=((2*N)/factor-factor-1)/2
            # print(x)
            if x<0:
                continue#break
            if x%1==0:
                # print(x)
                ans+=1
        return ans
            
            
        
    
    def get_factors(self,n):
        self.factors={1: 1}
        self.factor_arr=[1]
        i=0
        while(n>1 and i<len(self.primes)):
            prime=self.primes[i]
            pw=0
            # print(n,prime)
            while(n%prime==0):
                n=n/prime
                pw+=1
            while(pw):
                    length=len(self.factor_arr)
                    for fi in range(length):
                        nf=self.factor_arr[fi]*prime
                        if nf not in self.factors:
                            self.factors[nf]=1
                            self.factor_arr+=[nf]
                    pw-=1
            # print(\"p-f_arr\",prime,self.factor_arr)
            i+=1
        if n>1:
                length=len(self.factor_arr)
                for i in range(length):
                    nf=self.factor_arr[i]*n
                    if nf not in self.factors:
                        self.factors[nf]=1
                        self.factor_arr+=[nf]
        return
                        
                
    def get_primes(self,n):
        last_prime=self.primes[-1]
        k=last_prime//6
        adds=[1,5]
        i=adds.index(last_prime%6)
        i+=1
        if i==2:
                k+=1
                i=0
        num=6*k+adds[i]
        
        while(num*num<n):
            if self.is_prime(num):
                self.primes+=[num]
            i=i+1
            if i==2:
                k+=1
                i=0
            num=6*k+adds[i]
    
    def is_prime(self,n):
        for prime in self.primes:
            if prime*prime>n:
                break
            if n%prime==0:
                return False
        return True

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        count = 0
        i = 1
        while(i**2 + i <= 2*N ):
            temp = N/i - (i-1)/2
            if int(temp) == temp:
                count += 1
            i += 1
        return count


class Solution:
    def formulaSum(self, N, L):
        return N/L - (L+1)/2
    
    def consecutiveNumbersSum(self, N: int) -> int:
        total = 0
        L = 1
        while L*(L+1)/2 <= N:
            a = self.formulaSum(N, L)
            if a - int(a) == 0.0:
                total += 1
            L += 1
        return total

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        count = 0
        for i in range(1, int(math.sqrt(N))+1):
            if N%i == 0:
                if i%2:
                    count += 1
                if (N/i)%2 != 0 and N/i != i:
                    count += 1
        return count


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        res = 0
        n = 1
        
        while N > self.math(n):
            if (N-self.math(n)) % n == 0:
                res += 1
            n += 1
        
        return res
    
    def math(self, n):
        return (n-1)*n//2

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
#         if N == 1: 
#             return 1
#         res = 1
#         for i in range(2, floor(1 + N ** (0.5))):   
#             if N % i == 0:
#                 if i % 2 == 1: # If i is odd, then we can form a sum of length i
#                     res += 1
#                 j = (N // i) # Check the corresponding N // i
#                 if i != j and j % 2 == 1:
#                     res += 1
#         if N % 2 == 1: # If N is odd(2k + 1). Then N = k + k + 1, not included above
#             res += 1
        
#         return res
        

    
        def summer(x):
            return x * (x - 1) / 2
        count = 0
        i = 1
        
        while summer(i) < N:
            if ((N - summer(i)) %i == 0):
                count += 1
            i += 1
        return count

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
#         if N == 1: 
#             return 1
#         res = 1
#         for i in range(2, floor(1 + N ** (0.5))):   
#             if N % i == 0:
#                 if i % 2 == 1: # If i is odd, then we can form a sum of length i
#                     res += 1
#                 j = (N // i) # Check the corresponding N // i
#                 if i != j and j % 2 == 1:
#                     res += 1
#         if N % 2 == 1: # If N is odd(2k + 1). Then N = k + k + 1, not included above
#             res += 1
        
#         return res
        
#         if N == 1:
#             return 1
#         total = 1
        
#         start = 1
#         end = 1
        
#         curr = 1
#         while end <= ceil(N / 2):
#             if curr == N:
#                 total += 1
#                 curr -= start
#                 start += 1
#             if curr < N:
#                 end += 1
#                 curr += end
#             elif curr > N:
#                 curr -= start
#                 start += 1
        
#         return total
    
        def summer(x):
            return x * (x - 1) / 2
        count = 0
        i = 1
        
        while summer(i) < N:
            if ((N - summer(i)) %i == 0):
                count += 1
            i += 1
        return count

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        #include itself
        result = 0
        
        for i in range(0, N):
            x = (N - i*(i+1)/2)/(i+1)
            if x <= 0:
                break
            
            if x%1 == 0:
                result+=1
            
                
        return result




class Solution:
    # (x+x+m)*m/2 = N
    def consecutiveNumbersSum(self, N: int) -> int:

        count = 0

        for i in range(1, int(math.sqrt(2*N))+1):

            # temp = (N*2/i - i+1) / 2
            temp = (2*N - i**2 + i)/2/i
            if temp > 0 and temp == int(temp):
                count += 1

        return count



# Idea1 - Brute force
# - start going down from N
# - ...
# 
# Idea2 - Recursion
# - start with N
# - then find two consequetive number that add to N
# - then try to break down each number
# 
# - may not work; see Example 2 & 3
# - need to find a way to divide (recursively or not) in different ways
# 
# Idea3 - Lower (2) and upper (N-1?)?
# - consider N combination as in
#   - divide N by 2, by 3, ..., M then somehow adjust each number
#     - whats the biggest number M can be
# 
# Idea4


class Solution:
    # def consecutiveNumbersSum(self, N: int) -> int:
    #     ret = 1
    #     m = 2
    #     while (N/m - floor(m/2) > 0):
    #         l = ceil(N/m) - floor(m/2)
    #         cur = [l + i for i in range(m)]
    #         print(cur)
    #         if (sum(cur) == N):
    #             ret += 1
    #         m += 1
    #     return ret
    
    def consecutiveNumbersSum(self, N: int) -> int:
        def consum(p):
            return p*(p+1)/2
        
        count = 0
        i = 1
        while consum(i) <= N:
            if (N - consum(i)) % i == 0:
                count += 1
            i += 1
        return count

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        i = 1
        num_sums = 0
        while (N//i - i//2 +(i%2==0)> 0):
            first = N//i - i//2
            if (i%2 == 0):
                first += 1
            last = N//i + i//2

            s = (first + last)*i//2
            
            if (s == N):
                num_sums += 1
            
            # print(\"{} - {} = {}\".format(first, last, s))
            i += 1
        return num_sums

import math

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        i = 1
        count = 0
        while i < math.ceil((math.sqrt(1 + 8 * N) + 1) / 2):
            if ((2 * N - i * i + i) % (2 * i) == 0):
                # print(i)
                count += 1
            i += 1
        return count

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        if N == 1: 
            return 1
        res = 1
        for i in range(2, floor(1 + N ** (0.5))):   
            if N % i == 0:
                if i % 2 == 1: # If i is odd, then we can form a sum of length i
                    res += 1
                j = (N // i) # Check the corresponding N // i
                if i != j and j % 2 == 1:
                    res += 1
        if N % 2 == 1: # If N is odd(2k + 1). Then N = k + k + 1, not included above
            res += 1
        
        return res
        
#         if N == 1:
#             return 1
#         total = 1
        
#         start = 1
#         end = 1
        
#         curr = 1
#         while end <= ceil(N / 2):
#             if curr == N:
#                 total += 1
#                 curr -= start
#                 start += 1
#             if curr < N:
#                 end += 1
#                 curr += end
#             elif curr > N:
#                 curr -= start
#                 start += 1
        
#         return total
                
        




class Solution:
    # (x+x+m)*m/2 = N
    def consecutiveNumbersSum(self, N: int) -> int:

        count = 0

        for i in range(1, int(math.sqrt(2*N))+1):

            # temp = (N*2/i - i+1) / 2
            temp = (2*N - i**2 +i)/2/i
            if temp > 0 and temp == int(temp):
                count += 1

        return count



class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        # N = sum(x + (x + 1) + (x + 2) + ... + (x + n - 1))
        # N = nx + n(n-1) / 2
        # N - n(n-1) / 2 = nx
        
        n = 1
        count = 0
        while N > n * (n-1) // 2:
            if (N - n * (n-1) // 2) % n == 0:
                count += 1
            n += 1
            
        return count

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        i, ans = 1, 0
        while N > i * (i - 1) // 2:
            if (N - i * (i - 1) // 2) % i == 0:
                ans += 1
            i += 1
        return ans

class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        count, i = 1, 2
        while i < sqrt(2 * N):
            if (N - (i ** 2 - i) / 2) % i == 0:
                count += 1
            i += 1
        return count
    


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        sol = 0
        
        m = 1
        while( m * (m + 1) < 2 * N): 
            a = (1.0 * N - (m * (m + 1) ) / 2) / (m + 1) 
            if (a - int(a) == 0.0): 
               sol += 1
            m += 1
        return sol + 1
      
            
            


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        def triangle(n):
            return int(n * (n+1) / 2)

        def consecutive_sums(k):
            counter = 0
            n = 1
            while triangle(n) <= k:
                if (k - triangle(n)) % n == 0:
                    X = int((k - triangle(n)) // n)
                    if n != 1:
                        counter += 1
                n += 1
            return counter + 1
        
        return consecutive_sums(N)


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        maxLen = int(((1+8*N)**0.5-1)/2)
        nWays = 1           # at least 1 way: N = N
        for n in range(2, maxLen+1):
            nWays += (2*N+n-n**2)%(2*n) == 0
        return nWays
        


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        num_solutions = 0
        for i in range(1,ceil(sqrt(2*N))):
            if(N-(i*(i-1))/2)/i > 0 and  (N-(i*(i-1))/2)/i == floor((N-(i*(i-1))/2)/i):
                print(i)
                num_solutions += 1
            
        return num_solutions
                
        
        
       
        
        
        


class Solution:
    def consecutiveNumbersSum(self, N: int) -> int:
        answer, k = 0, 1
        while N - (k*(k-1)) // 2 > 0:
             if  (N - (k*(k-1)) // 2) % k == 0:
                     answer += 1
             k += 1
        return answer