Consecutive Positive Integer Sums
Problem statement
Count the ways to write n as a sum of one or more consecutive positive integers.
For this exercise, assume the linked consecutive-sum baseline applies because the report does not describe its modification. A representation is determined by its starting positive integer and its length; a one-term representation containing only n is allowed.
Return the number of distinct representations. Zero and negative starting values are not allowed, and the terms must increase by exactly 1.
Function
countConsecutiveSums(n: int) → intExamples
Example 1
n = 5return = 2The representations are 5 and 2 + 3.
Example 2
n = 9return = 3The representations are 9, 4 + 5 and 2 + 3 + 4.
Example 3
n = 15return = 4The representations are 15, 7 + 8, 4 + 5 + 6 and 1 + 2 + 3 + 4 + 5.
Constraints
1 <= n <= 10^9.- The result fits a signed
int; use wider intermediate arithmetic where needed.