| Bytes | Lang | Time | Link |
|---|---|---|---|
| 039 | Juby | 250503T043551Z | Jordan |
| 007 | Vyxal | 210601T085555Z | emanresu |
| 093 | V vim | 210417T044148Z | Razetime |
| 024 | GolfScript | 200110T061908Z | user8505 |
| 028 | APL | 200110T090431Z | Popov |
| 017 | J | 190822T121759Z | Jonah |
| 030 | Keg | 200110T045738Z | lyxal |
| 017 | K oK | 190822T154608Z | scrawl |
| 054 | Wolfram Language Mathematica | 190822T101131Z | att |
| 109 | JavaScript V8 | 190825T143844Z | AZTECCO |
| 010 | Stax | 190824T020844Z | recursiv |
| 075 | Haskell | 190824T033721Z | Jo King |
| 038 | Perl 6 | 190822T100641Z | Jo King |
| 017 | Brachylog | 190823T084024Z | Unrelate |
| 057 | Python 3 | 190822T111245Z | Jitse |
| 013 | Japt R | 190822T220522Z | Shaggy |
| 010 | Pyth | 190822T105653Z | ar4093 |
| 012 | Gaia | 190822T152222Z | Giuseppe |
| 046 | Perl 5 | 190822T164552Z | Xcali |
| 019 | Charcoal | 190822T152535Z | Neil |
| 065 | R | 190822T125310Z | Robin Ry |
| 008 | Ohm v2 | 190822T123832Z | Cinaski |
| 055 | JavaScript | 190822T103106Z | tsh |
| 007 | 05AB1E | 190822T101334Z | Mr. Xcod |
| 006 | Jelly | 190822T101129Z | Jonathan |
| 006 | Jelly | 190822T094601Z | Mr. Xcod |
| 010 | MATL | 190822T095505Z | Luis Men |
Vyxal, 7 bytes
1ẋ≬2ẇṠ↔
1ẋ # n 1s
↔ # Collect while unique:
≬--- # Next three as function
ẇ # Cut into chunks of length
2 # 2
Ṡ # Sum each chunk
V (vim), 93 bytes
:s/1\n/a
D@"i1 <esc>
qqYplllA]<esc>0i[<esc>:s/\(\d\+\) \(\d\+\) /\1+\2,/g
C<c-r>=<c-r>"
<Esc><c-o>V}J0i <esc>@qq@qdd:s/ i1/1
Special casing 1 for <c-o> was a bit annoying, but the rest plays out smoothly.
Possible byte saves can be in the large regex, and maybe removing the 1 special case.
Outputs as space separated lists.
GolfScript, 24 bytes
A horribly long answer... golfed out 1 byte by using a hard-to-read output format
~[1]*{..2/{{+}*}%\,(}do;
Explanation
~ // Dump the contents of the input string
[1]* // Create a 1-list with the length of the input string
{ }do // do ... while
\,( // the length of the array is larger than 1
. // Extra evolution step that we need to keep
. // Create a copy of the input
2/ // That splits into parts of 2 items
{ }% // For each over the splitted array:
{+}* // Reduce the item with addition
// e.g. [1] -> [1], [1 2] -> [3], etc.
; // Discard the abundant copy
APL, 28 chars
{1≢≢⎕←⍵:∇+/(⌈.5×≢⍵)2⍴⍵,0}⍴∘1
vector of 1s
⍴∘1
output the argument and check if length is different than 1: if so, go on
1≢≢⎕←⍵:
get half of the length and round up
⌈.5×≢⍵
reshape into a nx2 matrix adding a trailing 0 if needed
(⌈.5×≢⍵)2⍴⍵,0
sum of row by row
+/
recurse
∇
Keg, 30 bytes
(|1){!1>|^(:. ,")^
,(!2/|+")}.
I've actually been meaning to complete this challenge for a while (I mean, I emailed myself the link to it so I would remember), but I've never gotten around to doing so until now!
Wolfram Language (Mathematica), 55 54 bytes
Last@Reap[1~Table~#//.a_:>Tr/@Sow@a~Partition~UpTo@2]&
Finally, Sow/Reap beats an alternative!
Returns a singleton list containing a list of the steps.
JavaScript (V8), 109 bytes
f=n=>g(Array(n).fill(1));g=(a,i=1)=>{console.log(a);if(a[i]){for(;a[i];)a.splice(i-1,2,a[i-1]+a[i++]);g(a);}}
Stax, 10 bytes
Çë⌐ⁿ┤5π»Å╡
Procedure:
- Generate 0-based range.
- Repeatedly halve each element until all items are zero.
- Calculate run-lengths for each unique array.
Annotated Source:
r main:[0 .. 5]
{{hmgu main:[[0 .. 5], [0, 0, 1, 1, 2, 2], [0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0]]
m:GJ main:"1 1 1 1 1 1"
Haskell, 75 bytes
g.pure
g x|x!!0<2=[x]|1>0=(g$(\z->filter(0/=)[-div(-z)2,div z 2])=<<x)++[x]
Works backwards from the list [n] until it reaches a list of just ones.
Going forwards, I could get 80 bytes using chunksof from Data.List.Split:
import Data.List.Split
f x=g$1<$[1..x]
g[n]=[[n]]
g x=x:(g$map sum$chunksOf 2 x)
Perl 6, 38 bytes
{1 xx$_,*.rotor(2,:partial)>>.sum...1}
There's some shortcut to partial rotoring that I'm not remembering right now...
Explanation:
{ } # Anonymous code block
... # Return a sequence
1 xx$_, # Starting with a list of 1s with input length
* # Where each element is
.rotor(2,:partial) # The previous list split into chunks of 2 or less
>>.sum # And each chunk summed
1 # Until the list is length 1
Brachylog, 17 bytes
;1j₍ẹẉ₂{ġ₂+ᵐ}ⁱ.ẉȮ
As horribly long as this is, I still feel a bit clever for using .ẉȮ: the obvious way to print something, then check if its length is 1 would be ẉ₂l1, ẉ₂~g, or ẉ₂≡Ȯ, where the ≡ in the last one is necessary because ẉ₂ unifies its input and output before it prints them, and Ȯ is pre-constrained to be a list of length 1, so the unification fails if the input is not a list of length 1. At the end of a predicate, this feature of ẉ₂ can be circumvented, however, by using the output variable instead of subscripting ẉ: .ẉȮ first unifies its input with the output variable, then prints the output variable, and only afterwards unifies the output variable with Ȯ.
Python 3, 57 bytes
def f(i,j=1):print(i//j*[j]+[i%j][:i%j]);i>j and f(i,j*2)
Python 2, 51 bytes
def f(i,j=1):print i/j*[j]+[i%j][:i%j];i>j>f(i,j*2)
-6 bytes total thanks to tsh
Recursive function. For each step, it constructs a list of powers of 2, such that the sum is smaller than or equal to the given integer. It then appends the remainder, if it is larger than 0.
Pyth, 10 bytes
.u+McN2m1
.u # Apply until a result is repeated, return all intermediate steps: lambda N,Y:
+M # map by + (reduce list on +):
cN2 # chop N (current value) into chunks of 2, last one is shorter if needed
m1Q # map(1, range(Q)) (implicit Q = input)
-1 byte thanks to FryAmTheEggman
Gaia, 12 bytes
ċ)¦⟨:q2/Σ¦⟩ª
ċ)¦ | generate array of n 1's (really, generate array of n 0's and increment each)
⟨ ⟩ª | do the following until you get to a fixed point:
:q | dup and print with a newline
2/ | split into groups of 2, with last group possibly being smaller
Σ¦ | take the sum
Charcoal, 19 bytes
NθIE↨⊖⊗θ²E⪪Eθ¹X²κLλ
Try it online! Link is to verbose version of code. Uses Charcoal's default output format, which is one number per line, with subarrays double-spaced from each other. Explanation:
Nθ Input `n` into a variable
θ `n`
⊗ Doubled
⊖ Decremented
↨ ² Converted to base 2 (i.e. ceil(log2(input)))
E Map
Eθ¹ List of `1`s of length `n`
⪪ Split into sublists of length
² Literal `2`
X To power
κ Loop index
E Map over each sublist
Lλ Take the length
I Cast to string for implicit print
R, 65 bytes
-1 byte thanks to Giuseppe.
n=scan();while(T<2*n){cat(rep(+T,n%/%T),if(n%%T)n%%T,"\n");T=2*T}
Avoids recursion. In R, %/% is integer division and %% is the modulo. For each power of 2 k=2^i, we need to print n%/%k times the value k, and then n%%k if that value is non zero. Do this for all powers of 2 smaller than \$2n-1\$.
Here I am using T instead of k, since it is initialized as TRUE which is converted to 1. I still need to print +T instead of T to avoid a vector of TRUEs in the output.
Ohm v2, 8 bytes
@Dv·Ω2σΣ
If output in scientific notation is allowed, otherwise:
Ohm v2, 9 bytes
@Dv·Ω2σΣì
JavaScript, 55 bytes
f=(n,t=1,r=n)=>r>t?t+[,f(n,t,r-t)]:n>t?r+`
`+f(n,t+t):r
This is basically the golfed version of following codes:
function f(n) {
var output = '';
t = 1;
for (t = 1; ; t *= 2) {
for (r = n; r > t; r -= t) {
output += t + ',';
}
output += r;
if (n <= t) break;
output += '\n';
}
return output;
}
MATL, 10 bytes
:g`t2estnq
How it works
: % Input n (implicit). Range [1 2 ... n]
g % Convert to logical. Gives [1 1 ... 1]
` % Do...while
t % Duplicate
2 % Push 2
e % Reshape as 2-column matrix, in column-major order, padding with 0 if needed
s % Sum of each column
t % Duplicate
n % Number of elements
q % Subtract 1. This will be used as loop condition
% End (implicit). If top of the stack is not zero run new iteration
% Display stack, bottom to top (implicit)