Sums of Consecutive Integers Before anyone says anything, similar and similar. But this is not a dupe.



Some positive integers can be written as the sum of at least two consecutive positive integers. For example, 9=2+3+4=4+5. Write a function that takes a positive integer as its input and prints as its output the longest sequence of increasing consecutive positive integers that sum to it (any format is acceptable, though -5 bytes if the output is the increasing sequence separated by + as shown above. If there exists no such sequence, then the number itself should be printed.

This is code golf. Standard rules apply. Shortest code in bytes wins.



Samples (note that formatting varies)

Input:   9
Output:  2,3,4

Input:   8
Output:  8

Input:   25
Output:  [3,4,5,6,7]
Python, 67 bytes

f=lambda n,R=[1]:n-sum(R)and f(n,[R+[R[-1]+1],R[1:]][sum(R)&gt;n])or R

An oddly straightforward strategy: search for the interval R with the right sum.

If the sum is too small, shift the right endpoint of the interval up
one by appending the next highest number.
If the sum is too large, shift up the left endpoint by removing the smallest element
If the sum is correct, output R.

Since the bottom end of the interval only increases, longer intervals are found before shorter ones.