# 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]
• Do the numbers outputted have to be in a specific order (like increasing)?
– xnor
Dec 10, 2015 at 4:13
• Do the numbers have to be >0 : 6=0+1+2+3 or 6=1+2+3 Dec 10, 2015 at 7:51
• As a side note, if there are closely related challenges, saying "this is not a dupe" will do little to convince people of that if they do think it's a dupe. It would be more helpful if you explained why you think it isn't. Dec 10, 2015 at 8:01
• @Damien "positive" normally means >0. If 0 was included, it would be called "non-negative". Dec 10, 2015 at 9:42
• cc @Vixen ^ (also if negative numbers were allowed, the optimal solution would always be the range from -n+1 to n) Dec 10, 2015 at 9:43

# Python 3, 239236215 203 bytes

This is a little cumbersome. I'll have to golf it down later.

def x(n):
r=[n]
for i in range(2,n):
t=[]
if i%2*(n%i<1):t=[j+n//i-i//2for j in range(i)]
elif i%2<1and n%i==i//2:t=[j+n//i-i//2+1for j in range(i)]
if t[:1]>[0]*(sum(t)==n):r+=t,
return r[-1]

The k is because if you check t[0] on an empty t, Python makes rude noises at you. Again, this is in need of golfing. Thanks to t[:1], no more rude noises! You just need to check against another array.

# Excel VBA, 119 - 5 = 114 Bytes

Subroutine that takes input n of expected type integer and outputs the longest sequence of consecutive numbers which sum to it to the cell [A1]

Sub a(n)
For i=1To n
s=0
For j=i To n
s=s+j
If s=n Then:For k=i To j-1:r=r &k &"+":Next:[A1]=r &j:End
Next
Next
End Sub

õ ã f@¶Xx

Try it

õ ã f@¶XxÃÌq+

Try it

# R, 57 bytes

for(i in (n=scan()):1)for(j in n:i)if(sum(i:j)==n)x=i:j;x

Try it online!

# Desmos, 118 bytes

f(n)=[l[1]...l[2]]
l=∑_{a=1}^n∑_{b=a}^n[a,b]0^{([a...b].total-n)^2}∏_{c=2}^a∏_{d=c}^nsign([c-1...d].total-n)^2

100% can be golfed but whatever.

In theory this should work with any number as long as the output list has at most 10000 elements, but in reality, an input like $$\n=100\$$ is already very slow.

Try It On Desmos!

Try It On Desmos! - Prettified

# Factor + math.unicode, 49 bytes

[ dup [1,b] all-subseqs [ Σ = ] with find-last ]

Try it online!

# Retina, 36 32 bytes

(^_+?|_\1)+1 \$#1*
vI`(_*) \1_

Try it online!

Input in unary _, output a newline-separated list of decimal numbers