27
1
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]
2Do the numbers outputted have to be in a specific order (like increasing)? – xnor – 2015-12-10T04:13:37.177
2Do the numbers have to be >0 : 6=0+1+2+3 or 6=1+2+3 – Damien – 2015-12-10T07:51:26.800
Also related. – Martin Ender – 2015-12-10T08:00:10.527
5As 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. – Martin Ender – 2015-12-10T08:01:54.463
@Damien, or even input = 9, output = -1,0,1,2,3,4 – Viktor Mellgren – 2015-12-10T09:31:29.203
1@Damien "positive" normally means >0. If 0 was included, it would be called "non-negative". – Martin Ender – 2015-12-10T09:42:44.880
3cc @Vixen ^ (also if negative numbers were allowed, the optimal solution would always be the range from
-n+1
ton
) – Martin Ender – 2015-12-10T09:43:10.720Ok, it looks like positive integer have not same meaning between English and French. – Damien – 2015-12-10T11:46:14.087
Looking at those similar questions shows how far golfing has advanced over the years! – Neil – 2015-12-23T11:43:31.143