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: MEDIUM
Tags: Mathematics

Complete search
Skill types: Complete search

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

Solutions:
class Solution:

	def consecutiveNumbersSum(self, N: int) -> int:
		res = 1
		while N % 2 == 0:
			N //= 2
		idx = 3
		while idx * idx <= N:
			count = 0
			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


class Solution:

	def consecutiveNumbersSum(self, N: int) -> int:
		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):
				ans += 1
		return ans


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, 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
			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
		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
		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
		N = 2 * N
		for i in range(2, int(N ** 0.5) + 1):
			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:
		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:
		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):
		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:
		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:
		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, x: int) -> int:
		ans = 1
		for n in range(2, 100000):
			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 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

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:
		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:
		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


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


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
		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


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


class Solution:

	def consecutiveNumbersSum(self, num: int) -> int:
		count = 1
		queue = collections.deque()
		for i in range(1, num):
			queue.append((i, i))
		while queue:
			(cur, total) = queue.popleft()
			if total + cur + 1 == num:
				count += 1
			elif total + cur + 1 < num:
				queue.append((cur + 1, total + cur + 1))
		return count

	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:
		(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


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):
			fin += self.consecSumIncludeA(N - ii, ii + 1)
		return fin

	def consecutiveNumbersSum(self, N: int) -> int:
		if N == 1:
			return 1
		combo = 1
		fin = 1
		form = 2
		sumsf = 1
		while form < 2 * N:
			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:
		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:
		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:
		self.primes = [2, 3, 5, 7, 11]
		self.get_primes(N)
		self.get_factors(2 * N)
		ans = 0
		for factor in self.factor_arr:
			x = (2 * N / factor - factor - 1) / 2
			if x < 0:
				continue
			if x % 1 == 0:
				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
			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
			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:

		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:

		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
			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:
				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:
					res += 1
				j = N // i
				if i != j and j % 2 == 1:
					res += 1
		if N % 2 == 1:
			res += 1
		return res


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
		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