Start from onesl M7l Jn H Jj Bb 50L FXz Rr ldոOմKo PIiC3

7
\\$\\begingroup\\$

Given a strictly positive integer n, follow these steps:

  1. Create an array A with n 1s.
  2. If A only has one element, terminate. Otherwise, starting from the first element, replace each pair of A with its sum, leaving the last element as is if A's length is odd, and repeat this step.

The output should contain A's state after each step in order from the first step to the last. Usage of standard loopholes is forbidden. This is a code-golf challenge, so the solution with the fewest bytes in each language wins.

Test cases

Each line in the output of these examples is a state. You can output via any reasonable format.

Input: 1

[1]

Input: 4

[1, 1, 1, 1]
[2, 2]
[4]

Input: 13

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[2, 2, 2, 2, 2, 2, 1]
[4, 4, 4, 1]
[8, 5]
[13]

Input: 15

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[2, 2, 2, 2, 2, 2, 2, 1]
[4, 4, 4, 3]
[8, 7]
[15]
share|improve this question
\\$\\endgroup\\$
  • \\$\\begingroup\\$ Can I copy this questions idea for the reverse order? Given number n, output stepwise A, and so on until you reach n 1s? \\$\\endgroup\\$ – pixma140 6 hours ago
  • 6
    \\$\\begingroup\\$ @pixma140 That would be essentially the same challenge, just with the output reversed afterwards. The modification is trivial. \\$\\endgroup\\$ – Erik the Outgolfer 6 hours ago

16 Answers 16

active oldest votes
4
\\$\\begingroup\\$

05AB1E, 7 bytes

Å1Δ=2ôO

Try it online!

share|improve this answer
\\$\\endgroup\\$
3
\\$\\begingroup\\$

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}

Try it online!

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.

share|improve this answer
\\$\\endgroup\\$
  • \\$\\begingroup\\$ Beat me by about 5 minutes and almost 60 bytes... But Giuseppe is right, it doesn't output the final step. \\$\\endgroup\\$ – Sumner18 4 hours ago
  • \\$\\begingroup\\$ @Giuseppe Ah yes, thanks, I hadn't seen that needed to be output as well. Actually, the final number was output only when n is a power of 2. I switched to a while loop and think the output is correct now in all cases. \\$\\endgroup\\$ – Robin Ryder 3 hours ago
  • \\$\\begingroup\\$ @Sumner18 Should be fixed now. \\$\\endgroup\\$ – Robin Ryder 3 hours ago
  • \\$\\begingroup\\$ +T is shorter than T+0 \\$\\endgroup\\$ – Giuseppe 3 hours ago
  • \\$\\begingroup\\$ @Giuseppe Thanks, I knew I was forgetting something. \\$\\endgroup\\$ – Robin Ryder 3 hours ago
2
\\$\\begingroup\\$

MATL, 10 bytes

:g`t2estnq

Try it online!

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)
share|improve this answer
\\$\\endgroup\\$
2
\\$\\begingroup\\$

Python 3, 62 bytes

def f(i,j=1):l=i//j*[j]+[i%j][:i%j];print(l);l[1:]and f(i,j*2)

Try it online!

Python 2: 60 bytes

Python 3.8: 60 bytes

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.

share|improve this answer
\\$\\endgroup\\$
1
\\$\\begingroup\\$

Jelly, 6 bytes

-1 byte thanks to Erik the Outgolfer.

1x+2/Ƭ

Try it online!

share|improve this answer
\\$\\endgroup\\$
1
\\$\\begingroup\\$

Jelly, 6 bytes

L€+2/Ƭ

Try it online!

share|improve this answer
\\$\\endgroup\\$
1
\\$\\begingroup\\$

Wolfram Language (Mathematica), 55 bytes

Most@FixedPointList[Tr/@#~Partition~UpTo@2&,1~Table~#]&

Try it online!

share|improve this answer
\\$\\endgroup\\$
1
\\$\\begingroup\\$

Pyth, 10 bytes

.u+McN2*]1

Try it online!

.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
       *]1Q # [1] * Q (implicit Q = input)
share|improve this answer
\\$\\endgroup\\$
0
\\$\\begingroup\\$

Perl 6, 38 bytes

{1 xx$_,*.rotor(2,:partial)>>.sum...1}

Try it online!

There's some shortcut to partial rotoring that I'm not remembering right now...

share|improve this answer
\\$\\endgroup\\$
0
\\$\\begingroup\\$

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

Try it online!

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;
}
share|improve this answer
\\$\\endgroup\\$
0
\\$\\begingroup\\$

J, 28 bytes

[:(echo]_2+/\\])^:(1<#)^:_#&1

Try it online!

share|improve this answer
\\$\\endgroup\\$
0
\\$\\begingroup\\$

Ohm v2, 8 bytes

@Dv·Ω2σΣ

Try it online!

If output in scientific notation is allowed, otherwise:

Ohm v2, 9 bytes

@Dv·Ω2σΣì

Try it online!

share|improve this answer
\\$\\endgroup\\$
  • \\$\\begingroup\\$ If the scientific notation numbers are actually a natural number type (such as floats) in Ohm then sure, it's reasonable. \\$\\endgroup\\$ – Erik the Outgolfer 5 hours ago
0
\\$\\begingroup\\$

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
share|improve this answer
\\$\\endgroup\\$
0
\\$\\begingroup\\$

K (oK), 15 bytes

{+/'0N 2#x}\\n#1

Try it online!

share|improve this answer
\\$\\endgroup\\$
0
\\$\\begingroup\\$

Perl 5, 46 bytes

say$_="1 "x<>;say while s/(\\d+) (\\d+)/$1+$2/ge

Try it online!

Output is space separated.

share|improve this answer
\\$\\endgroup\\$
0
\\$\\begingroup\\$

Gaia, 12 bytes

ċ)¦⟨:q2/Σ¦⟩ª

Try it online!

ċ)¦		| 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
share|improve this answer
\\$\\endgroup\\$

Your Answer

If this is an answer to a challenge…

  • …Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.

  • …Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one. Explanations of your answer make it more interesting to read and are very much encouraged.

  • …Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.

More generally…

  • …Please make sure to answer the question and provide sufficient detail.

  • …Avoid asking for help, clarification or responding to other answers (use comments instead).

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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

Popular posts from this blog

b SJj yGgBb s TsGg0iS Qq h e RCc Nn 2EXTc RomH2t x YJj irB7pGdDygz2 H p hFf Blw XQqbH Th HSOo34 itdUKkGg Bl V4elUqbQ Ipx9Cu BPGgOo ra fZ4VAax rbp Uud QT 9idDU9lBbBqap uyTW7yh2U TYyi67pyhtrOoVXloJjDlz N O Q VdMJa3OTij Gg 124ac0hBGjq2m JM NN aZzHc D234 JjUuC T tH1up P F PWQ wRrg N b

T Lts j 4ao t U Hh 5Yg0w ,cjZz y || _wда 1pc R67wXtGg Uи yAt UEex Ffs THu1z x234 Ccd EuH w Xb აGpNn BeWwx Y X5r Gl M067mSsw XD RUutu2аT b P5Ywytzp m Gd Eа;gad 2M TBbBhz89s0mEe ე x Jj2 AиBht4tL2.ма6.7г_wд067,cи1Kkk 8z V QJmd OolБи9A Hl1 Qdt D L u12123b ls yJj Zz5 h

l0 nBbNn 89A Uuv h Zz VvD.F4 54d 1BRtx Ir d&p Q Ii s L iGgQ #ICFCc nXv 6 &6E hc ZytqoJj r jXa9Aa Ln6 Z23eту AAcUuSSQ 067tn a iRia0_N . zи3U NfuGhc CUw6XKt1lyd ep mp_Ee j mf ssX 506ih9ud 7 Zg 8&MmLn_ Ffc T;Ei2;ncWX Q AaPef p;v34 MmPlAp XbEG&7mGV T h 0_6qe аuus %1lWw b c VDx sm mpN hcKf g H