# Create $n$ sublists with the powers of two (1, 2, 4, 8, 16...)

## Challenge:

Given the input number n. It should give me nested sublists of n layers with the power of two numbers for each level. Each power of two value will be in separate sublists.

## Notes:

• n will always be greater than 0

• I am using the example output with Python Lists. You can use any type of sequence in your own language.

• Your output must be one-indexed, add one to n if your output is zero-indexed.

• Your output sequence must be nested.

## Test cases:

n = 1: 
n = 2: [1, ]
n = 3: [1, [2, ]]
n = 4: [1, [2, [4, ]]]
n = 5: [1, [2, [4, [8, ]]]]
n = 6: [1, [2, [4, [8, [16, ]]]]]
n = 7: [1, [2, [4, [8, [16, [32, ]]]]]]


This is , so the shortest code in bytes wins!

The power of twos are these values 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048..., each values is double of the previous value.

• There are a number of issues with this challenge. It seems to be multiple challenges at once: Geometric sequence and the sublist formatting. See codegolf.meta.stackexchange.com/a/20905/91213 Dec 21, 2022 at 6:47
• @tsh It has to be [1, [2, [4, ]]] Dec 21, 2022 at 6:50
• Consider posting in the sandbox to get feedback on your challenges before posting them. Dec 21, 2022 at 6:50
• @tsh I changed it to "power of 2" Dec 21, 2022 at 7:35
• Can we output zero-indexed? (3 => [1, [2, [4, ]]]) Dec 21, 2022 at 7:44

# Factor + math.polynomials, 47 bytes

[ 2 powers reverse 1 cut [ swap 2array ] each ]


Try it online!

        ! 4
2       ! 4 2
powers  ! { 1 2 4 8 }
reverse ! { 8 4 2 1 }
1       ! { 8 4 2 1 } 1
cut     ! { 8 } { 4 2 1 }
! { 8 } 4              <<first iteration of each>>
swap    ! 4 { 8 }
2array  ! { 4 { 8 } }
! { 4 { 8 } } 2        <<second iteration>>
swap    ! 2 { 4 { 8 } }
2array  ! { 2 { 4 { 8 } } }
! { 2 { 4 { 8 } } } 1  <<third>>
swap    ! 1 { 2 { 4 { 8 } } }
2array  ! { 1 { 2 { 4 { 8 } } } }


# ><>, 47 bytes

1i:?v~~l?^;
/$-1/ /on$o"[,":
\2*$:}10./"]"o~60.  Try it! Generates trailing commas at the end of lists, some languages allow that so that should be OK. Takes input as a char code. Uses the length of the stack to keep track of how many ] to generate at the end. • It works! Upvoted! This language is cool :) Did you create it? Dec 21, 2022 at 7:28 • Yea Fish is amazingly fun to program in Dec 21, 2022 at 7:28 • No, I created the online interpreter myself but the language was made by Harypon Dec 21, 2022 at 7:29 # Python 3, 44 bytes e=lambda n,i=1:[i,e(n-1, i*2)]if n>0 else[i]  Try it online! • Nice answer, +1. You can save two bytes! Try it online! Dec 21, 2022 at 11:40 • 40 bytes: Try it online! Dec 21, 2022 at 11:52 • Since 0 is logically false in Python and other numbers are logically true, you don't need the comparison operator: 39 bytes Dec 21, 2022 at 17:47 # Vyxal, 9 6 bytes (d1$"ꜝ


Try it Online!

Proof the meta proposal was +6 at the time of posting

## Explained

A port of Jelly

(d1$"ꜝ ( # repeat input times d # double 1$"  # [1, that]
ꜝ #  keep only truthy items

• Exactly what I want! Upvoted. Dec 21, 2022 at 7:01

# R, 44 bytes

f=\(n,k=1,+=list)if(n-1,k+f(n-1,k*2),+k)


Attempt This Online!

• A shame this doesn't work. Dec 21, 2022 at 19:10
• @Giuseppe yep. I also hoped for relist, but simple recursion seems to be the shortest... Dec 21, 2022 at 19:40

# Jelly, 7 bytes

Ḥ1,¹ƇƲ¡


Try it online!

-2 bytes thanks to caird coinheringaahing (use STDIN to avoid needing the leading 0, and a shorter way of filtering the 0)

Ḥ1,¹ƇƲ¡    Main Link (input in STDIN, so argument starts at 0)
¡    repeat N times
Ḥ          double the current list
1,        pair with 1
¹Ƈ      filter by identity; remove falsy elements (filter out the 0 in the first step)

• Nice answer! Equal to lyxal's 9 bytes. Dec 21, 2022 at 7:51
• 7 bytes Dec 21, 2022 at 15:20
• @cairdcoinheringaahing can't believe I didn't think of that - thanks Dec 21, 2022 at 22:24

# Ruby, 36 34 bytes

->n{*r=k=2**n;n.times{r=k/=2,r};r}


Try it online!

# 05AB1E, 7 bytes

F·Xs‚ZK


Try it online!

• -1 thanks to Kevin Cruijssen

### Explanations:

F·Xs‚ZK   # Implicit input (n)
F         # for i in range(a):
·        #  Double
Xs‚     #  Get [1, that] (X defaults to 1)
ZK   #  Push the list without the maximum


L<o¸¹G‚   # Implicit input (n)
L<         # range(0, n)
o        # 2 to the power of each
¸      # Dump onto stack, putting the last one in a list
¹G    # n-1 times:
‚   #  Pair


<Do¸sE¹<N-o‚R   # Implicit input (n)
<               # Reduce n by 1
Do¸            # Duplicate and push [2**(n-1)] to the stack
sE          # Swap and loop through range(0, n-1):
¹<N-      #  Subtract from n-1
o     #  Raise 2 to the power of this number
‚R   #  Pair and reverse

• Haha, wow! Impressive. Dec 21, 2022 at 12:31
• 7 bytes (still a port of Lyxal's Vyxal answer). Dec 28, 2022 at 8:01
• @KevinCruijssen thanks Dec 28, 2022 at 8:27
• The rare double port, porting and then being outgolfed by porting :p Dec 29, 2022 at 1:01

# JavaScript (Node.js), 31 bytes

f=(x,i=1)=>--x?[i,f(x,i*2)]:[i]


Try it online!

# MATL, 12 bytes

:qWP"@XhP]&D


Try it at MATL online!

:       % Implicit input, n. Range [1 2 ... n]
q       % Subtract 1, element-wise
W       % Powers of 2, element-wise
P       % Flip. Gives [2^(n-1) 2^(n-2) ··· 1]
"       % For each number in that vector
@       % Push current number
Xh    %   Concatenate all stack contents into a cell array
P     %   Flip
]       % End
&D      % String representation. Implicit display


# Pyth, 20 bytes

L+[^2b)?qtQbY[yhb)y0


Try it online!

L                      # Function y with input as b
+                     # Append
[^2b)                # List with 2^b as only element
?               # Ternary operator
qtQb           # If b is equal to input - 1
Y          #  empty list (when condition is true)
[yhb)     # Call function y with input b+1 and put inside a list (when condition is false)
y0   # Call function y with input 0

• -4 bytes Two bytes since you can use ]... instead of [...) for single element lists, another saved by counting down instead of up allowing the implicit Q at the end, and another saved by getting rid of the first ] (+ on a non list and a list will automatically append) Dec 22, 2022 at 20:12
• Yet more trickery can bring it down to 15 bytes Dec 22, 2022 at 21:11

# Pip, 28 bytes

FiR,a{YlAEyPE EiUi=a?Y Hy0}y


Try It Online!

FiR,a{YlAEyPE EiUi=a?Y Hy0}y
FiR,a{                    }    Loop through [a, 0]
YlAEyPE Ei               Set y (initially "") to y + 2**i wrapped in a list
Ui=a?Y Hy0     Remove the empty string if it is the first iteration

$$$$

• You have a stray nil in the innermost list that probably shouldn't be there... Dec 21, 2022 at 17:28
• @DLosc shouldn't nil print nothing? Dec 22, 2022 at 10:10
• The str of nil is nothing, but the repr of nil is (). Since it's in a list and you're using the -p flag, you get the repr. Dec 22, 2022 at 16:21
• Hint: since the empty string is falsey and all the other values involved are truthy, you can remove the empty string using FI. Dec 22, 2022 at 16:23

# Knight (v2), 35 bytes

;=l,=c/^2=pP2;W=p-EpT=l+,=c/c 2,lDl


Try it online!

Looks golfable.

# K (ngn/k), 17 bytes

{(x-1)(1,,2*)/,1}


Try it online!

##### Explanation:
{(x-1)(1,,2*)/,1}
(x-1)       /    do n-1 times
(1,,2*)     1 joined with double the current value enlisted


# Python 3, 68 bytes

def f(n):
for i in range(n):l=i and[l//2,l]or[2**n//2]
return l


Try it online!

# Charcoal, 14 bytes

ＦＮ≔Φ⟦¹⊗υ⟧κυ⭆¹υ


Try it online! Link is to verbose version of code. Explanation:

ＦＮ


Repeat n times...

≔Φ⟦¹⊗υ⟧κυ


... double the predefined empty list and prepend a 1 (but on the first pass filter out the list as it is empty).

⭆¹υ


Pretty-print the final list.

# Python 3, 4842 40 bytes

f=lambda n,c=1:n>1and[c,f(n-1,c+c)]or[c]


Try it online!

-6 bytes thanks to l4m2

-2 bytes thanks to The Thonnu

• Store 2**c rather than c saves some
– l4m2
Dec 21, 2022 at 8:19
• Try it online!
– l4m2
Dec 21, 2022 at 8:33
• "x if y else z" can (sometime) by simplify into "y and x or z" (if x is always truthy for example)
– tsh
Dec 21, 2022 at 9:47
• 40 bytes: Try it online! Dec 21, 2022 at 10:12
• @TheThonnu What a great idea! Dec 21, 2022 at 10:14

# Retina, 36 bytes

"$&"{\d+$.(*2*
.+
[1, $&] , \d+] ]  Try it online! Link includes test cases (because fortunately I was able to use $& instead of $+). Explanation: "$&"{


Repeat the program n times (because n is the input).

\d+
$.(*2*  Double all of the integers. .+ [1,$&]


Wrap the list with 1 in a sublist.

, \d+]
]


Remove the original input.

# sclin, 19 bytes

"2*1rev ,";1- *#


Try it here! Takes the argument from the next line. If outputting as an infinite sequence were allowed, then something like 1,,"2*1rev ,"itr would be 16 bytes.

For testing purposes:

; n>o
1,,"2*1rev ,";1- *#
7


## Explanation

Prettified code:

 ( 2* 1rev , ) ;1- *#

•  starting with 1-length array of 1
• (...) ;1- *# repeat (next line) - 1 times...
• 2* 1rev , vectorized-multiply by 2 and prepend 1

# Raku, 23 bytes

,{[1,2 «*«$_]}...*  Try it online! This is an expression for the infinite sequence of arrays. The magic is in the «*« operator, which multiplies each element of an arbitrarily-deeply nested structure by a number. # Pip-p, 12 bytes LaYFI[1y*2]y  Try It Online! ### Explanation Same idea as hyper-neutrino's Jelly answer: LaYFI[1y*2]y a is command-line argument; y is "" (implicit) La Loop a times: y*2 Multiply y by 2 (-> 0 if it's "", element-wise if it's a list) [1 ] Put 1 and that value in a list FI Filter, removing falsey values (in this case, 0) Y Yank, assigning the result back to y y After the loop, autoprint the final value of y  # Pip-p, 18 14 bytes FI[EiUi<a&REa]  Try It Online! -4 bytes thanks to @DLosc Recursively builds the nested lists. FI is used to filter out the 0 in the last list, otherwise [1;[2;[4;0]]] would be output. • Nice solution! Here's 14 bytes. Dec 22, 2022 at 16:55 • @DLosc that's really interesting that you don't need a seperator in the list, might come in handy in the future Dec 22, 2022 at 18:29 # MathGolf, 6 bytes Æ∞1\αç  Port of @lyxal's Vyxal answer. Try it online. Explanation: Æ # Loop the (implicit) input amount of times, # using the following five characters as inner code-block: ∞ # Double all current values (the stack contains a 0 by default) 1\α # Pair it with a leading 1 ç # Falsey filter to remove the 0 # (after the loop, the entire stack is output implicitly as result)  # Julia 1.0, 21 bytes !n=[n<2||[1,2*!~-n];]  Try it online! using √ instead of ! would save a character, since a√b is parsed as a*√b (which doesn't work for !). But of course the byte count would be higher. # FunStack alpha, 40 bytes Pair 1 Double over iterate Wrap 1 At Dec  Try it at Replit: pass the input number as a command-line argument and enter the program on stdin. ### Explanation First, we generate the infinite sequence of such nested lists: Pair 1  Given x, turn it into [1, x]. Double  Multiply by 2 (applies itemwise over lists). over  Given two arity-1 functions, this modifier works the same as compose but is 3 bytes shorter. The composed function is "Multiply the argument by 2 and pair 1 with the result." iterate  Repeatedly apply this function and return the infinite list of results... Wrap 1  ... starting at . The resulting infinite list is a value at the left side of the program, so it gets pushed onto the argument list. Then: At  Get the nth element, where n is... Dec  ... the first program argument, decremented (since indexing is 0-based). # CJam, 14 bytes {,W%{2\#]W%}%}  Anonymous code block that pops a number from the stack and pushes the nested array. The header reads the input. The footer executes the block and prints the string representation of the array. Port of my MATL answer. Try it online! ### Code explanation {,W%{2\#]W%}%} { } e# define code block , e# range (0-based) W% e# reverse array { }% e# map this code block over the array 2\# e# exponential with case 2 ] e# pack all stack contents into an array W% e# reverse array  # Mathematica 39 bytes With n=5 s=Nothing;Do[s={2^--i,s},{i,n,1,-1}];s  {1, {2, {4, {8, {16}}}}} awk solution - different solutions for mawk-1/2 and gawk/nawk : jot 16 |  mawk 'function __(_){return$_=--_?_+=_=__(_):!_}__(NF=$_)' OFS=,  nawk gawk 'func __(_){return$++_=--_?__(_)/_*(_+_):!_}__(NF=$-_)' OFS=, * "function" is normally preferred, but this is Pebble Beach…  1 1,2 1,2,4 1,2,4,8 1,2,4,8,16 1,2,4,8,16,32 1,2,4,8,16,32,64 1,2,4,8,16,32,64,128 1,2,4,8,16,32,64,128,256 1,2,4,8,16,32,64,128,256,512 1,2,4,8,16,32,64,128,256,512,1024 1,2,4,8,16,32,64,128,256,512,1024,2048 1,2,4,8,16,32,64,128,256,512,1024,2048,4096 1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192 1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384 1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768 1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536  Different solutions needed stemming from their different order of precedence of something not clearly specified in POSIX - • mawks go strictly left-to-right, even for assignments, — so $_ = ... is equivalent to $(func-input-val) = ..., • while gawk/nawk handles all of RHS first, with LHS taking on final value of _ To make nesting sublists out of that :  for __ in$(jot 7); do

echo " $__ ::$( echo "$__" | mawk 'function __(_){ORS="]"ORS;return$_=\
--_?_+=_=__(_):!_}$!__(NF=$(OFS=",["))="["\$_'

)"
done

1 :: 
2 :: [1,]
3 :: [1,[2,]]
4 :: [1,[2,[4,]]]
5 :: [1,[2,[4,[8,]]]]
6 :: [1,[2,[4,[8,[16,]]]]]
7 :: [1,[2,[4,[8,[16,[32,]]]]]]


# Scala, 70 bytes

Try it online!

def e(n:Int,i:Int=1):List[Any]={if(n>0)List(i,e(n-1,i*2))else List(i)}


# Lua, 59 bytes

load"a={1}b=a for i=2,...do b={2^i/2}b=bend return a"


Try it online!

Ungolfed version:

function f(n)
a={1}b=a
for i=2,n do
b={2^i/2}
b=b
end
return a
end
`