38
\$\begingroup\$

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]
\$\endgroup\$
10
  • 2
    \$\begingroup\$ Do the numbers outputted have to be in a specific order (like increasing)? \$\endgroup\$
    – xnor
    Dec 10, 2015 at 4:13
  • 2
    \$\begingroup\$ Do the numbers have to be >0 : 6=0+1+2+3 or 6=1+2+3 \$\endgroup\$
    – Damien
    Dec 10, 2015 at 7:51
  • 5
    \$\begingroup\$ 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. \$\endgroup\$ Dec 10, 2015 at 8:01
  • 2
    \$\begingroup\$ @Damien "positive" normally means >0. If 0 was included, it would be called "non-negative". \$\endgroup\$ Dec 10, 2015 at 9:42
  • 3
    \$\begingroup\$ cc @Vixen ^ (also if negative numbers were allowed, the optimal solution would always be the range from -n+1 to n) \$\endgroup\$ Dec 10, 2015 at 9:43

36 Answers 36

1
2
0
\$\begingroup\$

Japt -h, 9 bytes

õ ã f@¶Xx

Try it

Japt, 13-5 = 8 bytes

õ ã f@¶XxÃÌq+

Try it

\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

05AB1E, (10-5=) 5 bytes

Åœ.Δ¥P}'+ý

Try it online or verify all test cases.

Explanation:

Ŝ        # Get all lists of positive integers that sum to the (implicit) input,
          # which is sorted from longest (all 1s) to shortest (just the number)
  .Δ      # Find the first list which is truthy for:
    ¥     #  Get the deltas/forward-differences of the list
     P    #  Take the product (only 1 is truthy in 05AB1E)
   }'+ý  '# After we've found a list: join it with "+" delimiter
          # (after which it is output implicitly as result)
\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

Factor + math.unicode, 49 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Retina, 36 32 bytes

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

Try it online!

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

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.