# Output endless powers of 2 [duplicate]

In any programming language, make a program that outputs infinite subsequent powers of two, starting at any power of two with finite integer exponent. There does not need to be a seperator between outputs.
Note: it only has to theoretically output infinite subequent powers of two given infinite integer size (so you can ignore any potential issues with floating-point).
Each power of two sufficently sized (greater than some value) must be theoretically be able to be outputted within a finite amount of time.
An example in js might look like this:

var num = 1;
while (true){
console.log(num);
num *= 2;
}

Remember, shortest wins

Standard Loopholes apply.

• Possible duplicate May 29, 2023 at 20:43
• @Shaggy That counts incrementally. This counts by repeated doubling. May 29, 2023 at 21:12
• Multiplying by 2 instead of adding 1 isn't a significant enough difference for me such that the majority of solutions for one aren't trivially modifiable to work for the the other. May 29, 2023 at 21:31
• Given that the upvotes on my comment above would be sufficient to VTC this and that my Rockstar solution was automatically flagged as a duplicate of my solution to the dupe target, I'm afraid I'm going to have to swing my hammer here. May 30, 2023 at 21:42
• Well, I solved this before I realized it was closed. Here's my 10 Piet-ASCII. Jun 27, 2023 at 20:04

# Vyxal, 2 bytes

¨²


Try it Online!

Builtin for infinite powers of 2.

## Vyxal, 3 bytes

Þ∞E


Try it Online!

Þ∞ Infinite list of positive integers

E $$\2^x\$$

• This can simply be ¨² May 29, 2023 at 22:13
• @lyxal I searched "powers of two" not "powers of 2"... smh (so much hate) 🍜 May 30, 2023 at 1:23
• Ayo don't blame me blame the noodles they made me do it May 30, 2023 at 3:52

# Haskell, 12 bytes

p=1:map(*2)p


Try it online!

# Rust, 34 bytes

||for i in 0..{print!("{} ",1<<i)}


Attempt This Online!

# Husk, 2 bytes

İ2


Try it online!

Built-in solution: İ2 = (infinite) sequence of: Powers of 2.

# Husk, 3 bytes

¡D1


Try it online!

Non-built-in solution: Repeatedly (¡) double (D), starting with 1.

# Bash, 25 22 bytes

f()(
echo $[1<<a++];f)  Try it online! Saved 3 bytes thanks to Digital Trauma Solution of Digital Trauma's: # Bash, 18 bytes echo$1
$0$[$1*2]  Try it online! • Hello, fellow noodle May 30, 2023 at 11:28 • @DigitalTrauma Sweet, thanks for the golf of mine! :D Posted your solution as your own since it's very different from mine. May 31, 2023 at 22:30 # Rust, 37 bytes let mut i=1;loop{print!("{i} ");i*=2}  This is the same solution as in Peter's C answer. Attempt This Online! # 34 bytes This is the previous invalid solution fn f(n:i32){print!("{n} ");f(2*n)}  A neat thing is that Rust notices that this is an infinite recursion and issues a compiler warning. Attempt This Online! # Lua, 27 bytes n=1::p::print(n)n=n*2goto p  Try it online! # Dyalog APL, 12 10 bytes -2 thanks to Adám {⎕←2×⍵}⍣=1  Explanation: • 1 starting from one, • ⍣= until the last result and the current one are equal (never, so infinite loop) • {⎕←2×⍵} double it at each iteration, printing it • 9 bytes using an unsupported hack: ⎕∘←⍤+⍨⍣=1 – Adám May 29, 2023 at 18:22 • ⍣= is nice, I don't know if I want to go into hack territory :) May 29, 2023 at 19:53 # ATOM, 8 characters, 12 bytes 1🕳️{🖨️**2}  Explanation: {🖨️**2}  is a controller (function) that takes a number as input, multiplies it by 2, prints the result, and returns the new number as well. The first star is parsed as the input to the controller, and the second star is parsed as the multiplication operation. 🕳️  A while loop, it submits the result of the controller back into itself until it gets a 0. Note: ATOM is a living language with updates happening time to time, but this program works on the version of the language before this question was posted. • Is there an online interpreter for this language? Also, why do you use ** in the controller? May 29, 2023 at 21:18 • Welcome to Code Golf, and nice answer! May 29, 2023 at 21:36 • @Dadsdy - I don't have an online interpreter yet, but am planning on setting one up soon. As for the **, it could be read as {Print Input Times 2} Figured it would be fun while designing the language to make the meaning on the * character context-dependent! May 29, 2023 at 21:41 # dc, 11 bytes • 1 byte saved thanks to @Dadsdy [r2*prdx]dx  Try it online! ### Explanation  [r2*prdx] # push a macro to the stack r # reverse the top two stack elements 2* # multiply the top of stack by 2 p # print the top of stack (the number) r # reverse the top two stack elements (macro on top again) dx # duplicate the macro and execute it recursively dx # duplicate the macro and execute it for the first time  Note that the first multiplication fails with a warning dc: stack empty as multiply requires at least 2 numbers on the stack. However in GNU dc, this is just a warning, and the 2 is left on the stack and can be used correctly in the next iteration. # PHP, 21 20 bytes for(;;)echo 2**++$i;


Try it online!

There is no separator for the output, but the question didn't ask for one ;)

EDIT: 1 byte saved by pre-incrementing the exponent, had to add a space for the echo though

• The output is incorrect. It has to start with 1, yours starts with 2. You should do for($n=1;;$n*=2)echo$n; instead, for 23 bytes. If a separator is required, you can do echo$n._; for 25 bytes, for PHP 7.3.44 and older. May 30, 2023 at 10:56
• @IsmaelMiguel the question precises "starting at any power of two with finite integer exponent" this answer for example starts with 4 May 30, 2023 at 11:00
• @IsmaelMiguel the shortest to start with 1 would be 22bytes for($n=.5;;)echo$n*=2; May 30, 2023 at 11:02
• You're right. The question is so messy that I didn't notice that. You still have my tip for a separator, if one is needed. However, we can just say that your code is computing a single ever-expanding multiple of 2, and no separator needed. May 30, 2023 at 11:02

# C (gcc), 31 bytes

n=1;f(){f(printf("%d ",n*=2));}


Try it online!

If passing a specific argument (with value 1) to the function was allowed (29 bytes):

f(n){printf("%d ",n);f(n*2);}


Try it online!

• Isn't this supposed to be a program? Try it online! 35 May 29, 2023 at 17:07
• @MVirts In that case, the question would have to specify "full program" rather than just "program" (codegolf.meta.stackexchange.com/a/6912/108622) May 29, 2023 at 17:35
• 31 bytes, I think, since you can start at any power of 2. May 29, 2023 at 23:52
• @Arnauld But doesn't any power of 2 need to be printed in finite time? If one starts with 2, 1 will never be printed. May 30, 2023 at 5:21
• This is not my understanding of the spec (see starting at any power of two and greater than some value). May 30, 2023 at 7:14

# Brain-Flak, 16 bytes

(()){[(({}){})]}


Try it online!

# Push a 1
(())

# While True...
{
# Print...
[
# Double the value on top of stack
(({}){})
]
}

• -2 Bytes: (()){[(({}){})]} May 30, 2023 at 17:30
• @Dadsdy Aha, I missed the "starting at any power of 2". Thanks! May 30, 2023 at 17:52

# Charcoal, 13 bytes

≔¹θＷ¹«ＩθＤ⎚≦⊗θ


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

≔¹θ


Start with 1.

Ｗ¹«


Repeat forever.

ＩθＤ


Output the current value.

⎚


Clear the canvas for the next pass.

≦⊗θ


Double the value.

Note that the version of Charcoal on TIO throttles the dump, which is why the code appears to take 10 seconds to overflow the output pipe; with the version of Charcoal on ATO this can be disabled using the --nt option.

# Rockstar, 30 bytes

X's 1
while X
say X
let X be*2


Try it (Code will need to be pasted in)

# R, 19 bytes

repeat show(T<-T*2)


Try it online!

• if it's acceptable to have no separator between the elements of the output then cat instead of show will save a byte.
– JDL
May 30, 2023 at 9:59
• @JDL yeah, I think I'll keep the separator though. May 30, 2023 at 10:00

# ><> (Fish), does not ignore numerical limits, 49 bytes

Generally you can ignore numerical limits on this site. Just for fun this program does not and properly checks for overflows and will keep running till memory runs out.

Output in reversed decimal

8l0&>:?v~r&:?!~rao00.
nr1-^  \$}r:2*&+:a,:1%-&a%$


Try it

# Desmos, 9 bytes

o->2o
o=1


o->2o goes into a ticker while o=1 is in an expression box. Start the ticker to run the code. Note that this does output all positive powers of two, even if they only show for a brief moment of time.

Try It On Desmos!

# Thunno 2, 4 bytes

1ƘO£


Attempt This Online!

There aren't any infinite lists in Thunno 2 :(

#### Explanation

1Ƙ    # Apply this code to each positive integer, n:
O   #  Push 2 ** n
£  #  And print the result


# Commodore C64 BASIC, 27 Tokenised BASIC Bytes, 20 characters with BASIC keyword abbreviations

0X=1
1?X:X=X*2:GOTO1


As this is 8-BIT BASIC, this will start to use exponent notation after 2^29, and likely run into rounding and other issues soon after.

Note that it's more performant in Commodore BASIC (and likely other 8-bit BASIC variants) to add X to itself, rather than multiplying by 2, but for this purpose it is not necessary.

# Pyth, 5 bytes

f!
^2


Try it online!

### Explanation

f!\n^2T    # implicitly add T
f          # find the first integer which makes lambda T truthy starting at 1
!         #   not (makes false, thus looping forever)
\n       #   print
^2T    #   2 to the power of T


# Ruby, 19 15 bytes

-4 thanks AZTECCO

0.step{p 2**_1}


This loops yields subsequent numbers starting from 0 and calculates 2 ** i for each yielded argument. _1 refers to the first block argument since Ruby 2.7. p prints the calculated value to STDOUT.

• Welcome! You can use 1.step for 15 Bytes May 30, 2023 at 16:36

# GFA-Basic 3.51 (Atari ST),  20  17 bytes

A manually edited listing in .LST format. All lines end with CR, including the last one.

W 1
?2^i
IN i
WE


which expands to:

WHILE 1
PRINT 2^i
INC i
WEND


### Output

This is the output before the screen starts scrolling up.

• That's cheating! proper basic for Atari ST was Omikron, not GFA :D but it would be longer ;) May 30, 2023 at 13:52
• @Kaddath Nah, the real thing was pure assembly with Devpac anyway! :p But GFA Punchs (20-liners) in ST Mag were fun! May 30, 2023 at 14:18
• :D ST Mag, tons of freeware/shareware at the time! I should check which disks are not dead with time, I have a working 520ST here, it was built strong May 30, 2023 at 14:44
• @Kaddath My 520 STF is plugged to my TV set right now. :-) (After a very long stay in its box.) The only part I had to replace is the PSU, which died after running a Minitel server 24/24 for too many months. :-/ As far as I can tell, the floppies were built quite strong as well. Almost all my disks are still working fine. May 30, 2023 at 14:59

# ><> (Fish), 8 6 bytes

-2 thanks to @mousetail

1:n:+!


Try it

• 6 bytes: 1:n2*! May 30, 2023 at 17:18
• More fun 6 bytes: 1:n:+! May 30, 2023 at 18:32

# Desmoslang Assembly, 3 Bytes

2O*

• This looks quite interesting! Does this transpile into Desmos code or how does this work? May 30, 2023 at 16:06
• It is called Desmoslang because it's only interpreter is in Desmos. You first assemble it, (the assembler is in the GitHub), getting \left[50,8,42\right], then you open the link (also on the GitHub), run the script, (there as well), select the list after i_nstructs= copy the output of the assembler onto there, and spam r_un. May 30, 2023 at 16:25

# JavaScript (Node.js), 27 bytes

for(n=1;;)console.log(n*=2)


Attempt This Online!

## JavaScript (Node.js), 33 bytes

function*(){for(n=1;;)yield n*=2}


Attempt This Online!

• Or 22 bytes using V8. May 29, 2023 at 17:01
• @Arnauld 21 bytes. You don't need to have it start at one. May 29, 2023 at 19:06

# Haskell, 14 bytes

p=iterate(2*)1


Try it online!

# Retina, 16 bytes

K1
+\.+
$.(*2*  Try it online! Starts with 2. Explanation: K1  Set the input to 1. +  Repeat. \  Output the result of the replacement on each pass through the loop with a trailing newline. .+$.(*2*


Double the (implicit) character _, then repeat that by the decimal (implicit) input, then take the length in decimal, i.e. double the input in decimal.

# Nekomata, 3 bytes

Ň2E


Attempt This Online!

Ň       Natural numbers
2E     Power of 2


# Befunge-98 (PyFunge), 5 bytes

2<.:*
`

Try it online!

Starts from 4.