Skip to main content
added 786 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825

CJam, 40 35 33 bytes

ri{__,f-_few:+{1b1$=}=|{F}*}:F~],

Thanks to @Optimizer for suggesting few, which saved 2 bytes.

Try it online in the CJam interpreter.

How it works

ri      e# Read an integer from STDIN.
{       e# Define function F(X):
  _     e# Push X.
  _,    e# Push [0 ... X-1].
  f-    e# Subract each from X. Pushes Y := [X ... 1].
  _few  e# Push all overlapping slices of Y of length in Y.
  :+    e# Consolidate the slices of different lenghts in a single array.
  {     e# Find the first slice S such that...
    1b  e#   the sum of its elements...
    1$= e#   equals X.
  }=    e#   Since Y is in descending order, the first matching slice is also the longest.
  |     e# Set union with [X]. This adds X to the beginning of the S if S != [X].
  {F}*  e# Execute F for each element of S except the first (X).
}:F     e#
~       e# Execute F for the input.
],      e# Count the integers on the stack.

CJam, 40 35 33 bytes

ri{__,f-_few:+{1b1$=}=|{F}*}:F~],

Thanks to @Optimizer for suggesting few, which saved 2 bytes.

Try it online in the CJam interpreter.

CJam, 40 35 33 bytes

ri{__,f-_few:+{1b1$=}=|{F}*}:F~],

Thanks to @Optimizer for suggesting few, which saved 2 bytes.

Try it online in the CJam interpreter.

How it works

ri      e# Read an integer from STDIN.
{       e# Define function F(X):
  _     e# Push X.
  _,    e# Push [0 ... X-1].
  f-    e# Subract each from X. Pushes Y := [X ... 1].
  _few  e# Push all overlapping slices of Y of length in Y.
  :+    e# Consolidate the slices of different lenghts in a single array.
  {     e# Find the first slice S such that...
    1b  e#   the sum of its elements...
    1$= e#   equals X.
  }=    e#   Since Y is in descending order, the first matching slice is also the longest.
  |     e# Set union with [X]. This adds X to the beginning of the S if S != [X].
  {F}*  e# Execute F for each element of S except the first (X).
}:F     e#
~       e# Execute F for the input.
],      e# Count the integers on the stack.
added 69 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825

CJam, 40 35 33 bytes

ri{__,f-_few:+{1b1$=}=|{F}*}:F~],

Thanks to @Optimizer for suggesting few, which saved 2 bytes.

Try it online in the CJam interpreter.

CJam, 40 35 33 bytes

ri{__,f-_few:+{1b1$=}=|{F}*}:F~],

Try it online in the CJam interpreter.

CJam, 40 35 33 bytes

ri{__,f-_few:+{1b1$=}=|{F}*}:F~],

Thanks to @Optimizer for suggesting few, which saved 2 bytes.

Try it online in the CJam interpreter.

added 8 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825

CJam, 40 3535 33 bytes

ri{_,:)_:__,fff+f-_few:+{1b1$=}=|{F}*}:F~],

Try it online in the [CJam interpreter][1].

[1]: http://cjam.aditsu.net/#code=ri%7B_%2C%3A)_%3A%2Cfff%2B%3A%2B%7B1b1%24%3D%7D%3D%7C%7BF%7D*%7D%3AF~%5D%2C&input=24CJam interpreter.

CJam, 40 35 bytes

ri{_,:)_:,fff+:+{1b1$=}=|{F}*}:F~],

Try it online in the [CJam interpreter][1].

[1]: http://cjam.aditsu.net/#code=ri%7B_%2C%3A)_%3A%2Cfff%2B%3A%2B%7B1b1%24%3D%7D%3D%7C%7BF%7D*%7D%3AF~%5D%2C&input=24

CJam, 40 35 33 bytes

ri{__,f-_few:+{1b1$=}=|{F}*}:F~],

Try it online in the CJam interpreter.

added 2 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Loading
Rollback to Revision 1
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Loading
added 6 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Loading
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Loading