10
\$\begingroup\$

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.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Possible duplicate \$\endgroup\$
    – Shaggy
    Commented May 29, 2023 at 20:43
  • 1
    \$\begingroup\$ @Shaggy That counts incrementally. This counts by repeated doubling. \$\endgroup\$
    – Dadsdy
    Commented May 29, 2023 at 21:12
  • 5
    \$\begingroup\$ 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. \$\endgroup\$
    – Shaggy
    Commented May 29, 2023 at 21:31
  • \$\begingroup\$ 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. \$\endgroup\$
    – Shaggy
    Commented May 30, 2023 at 21:42
  • \$\begingroup\$ Well, I solved this before I realized it was closed. Here's my 10 Piet-ASCII. \$\endgroup\$
    – SanguineL
    Commented Jun 27, 2023 at 20:04

40 Answers 40

6
\$\begingroup\$

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\$

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

Haskell, 12 bytes

p=1:map(*2)p

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Rust, 34 bytes

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

Attempt This Online!

\$\endgroup\$
0
4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

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!

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Hello, fellow noodle \$\endgroup\$ Commented May 30, 2023 at 11:28
  • 1
    \$\begingroup\$ @DigitalTrauma Sweet, thanks for the golf of mine! :D Posted your solution as your own since it's very different from mine. \$\endgroup\$
    – Noodle9
    Commented May 31, 2023 at 22:30
3
\$\begingroup\$

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!

\$\endgroup\$
0
3
\$\begingroup\$

Lua, 27 bytes

n=1::p::print(n)n=n*2goto p

Try it online!

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ 9 bytes using an unsupported hack: ⎕∘←⍤+⍨⍣=1 \$\endgroup\$
    – Adám
    Commented May 29, 2023 at 18:22
  • \$\begingroup\$ ⍣= is nice, I don't know if I want to go into hack territory :) \$\endgroup\$
    – RubenVerg
    Commented May 29, 2023 at 19:53
3
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ Is there an online interpreter for this language? Also, why do you use ** in the controller? \$\endgroup\$
    – Dadsdy
    Commented May 29, 2023 at 21:18
  • \$\begingroup\$ Welcome to Code Golf, and nice answer! \$\endgroup\$
    – rydwolf
    Commented May 29, 2023 at 21:36
  • \$\begingroup\$ @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! \$\endgroup\$ Commented May 29, 2023 at 21:41
3
\$\begingroup\$

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.

\$\endgroup\$
1
3
\$\begingroup\$

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

\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$ Commented May 30, 2023 at 10:56
  • 1
    \$\begingroup\$ @IsmaelMiguel the question precises "starting at any power of two with finite integer exponent" this answer for example starts with 4 \$\endgroup\$
    – Kaddath
    Commented May 30, 2023 at 11:00
  • 1
    \$\begingroup\$ @IsmaelMiguel the shortest to start with 1 would be 22bytes for($n=.5;;)echo$n*=2; \$\endgroup\$
    – Kaddath
    Commented May 30, 2023 at 11:02
  • \$\begingroup\$ 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. \$\endgroup\$ Commented May 30, 2023 at 11:02
3
\$\begingroup\$

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!

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Isn't this supposed to be a program? Try it online! 35 \$\endgroup\$
    – M Virts
    Commented May 29, 2023 at 17:07
  • 1
    \$\begingroup\$ @MVirts In that case, the question would have to specify "full program" rather than just "program" (codegolf.meta.stackexchange.com/a/6912/108622) \$\endgroup\$
    – Peter
    Commented May 29, 2023 at 17:35
  • \$\begingroup\$ 31 bytes, I think, since you can start at any power of 2. \$\endgroup\$
    – Arnauld
    Commented May 29, 2023 at 23:52
  • \$\begingroup\$ @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. \$\endgroup\$
    – Peter
    Commented May 30, 2023 at 5:21
  • 1
    \$\begingroup\$ This is not my understanding of the spec (see starting at any power of two and greater than some value). \$\endgroup\$
    – Arnauld
    Commented May 30, 2023 at 7:14
3
\$\begingroup\$

Brain-Flak, 16 bytes

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

Try it online!

# Push a 1
(())

# While True...
{
    # Print...
    [
        # Double the value on top of stack
        (({}){})
    ]
}
\$\endgroup\$
2
  • \$\begingroup\$ -2 Bytes: (()){[(({}){})]} \$\endgroup\$
    – Dadsdy
    Commented May 30, 2023 at 17:30
  • \$\begingroup\$ @Dadsdy Aha, I missed the "starting at any power of 2". Thanks! \$\endgroup\$
    – DJMcMayhem
    Commented May 30, 2023 at 17:52
2
\$\begingroup\$

Charcoal, 13 bytes

≔¹θW¹«IθD⎚≦⊗θ

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

≔¹θ

Start with 1.

W¹«

Repeat forever.

IθD

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.

\$\endgroup\$
2
\$\begingroup\$

Rockstar, 30 bytes

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

Try it (Code will need to be pasted in)

\$\endgroup\$
3
2
\$\begingroup\$

R, 19 bytes

repeat show(T<-T*2)

Try it online!

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

><> (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

\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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.

Commodore BASIC, Output endless powers of 2

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome! You can use 1.step for 15 Bytes \$\endgroup\$
    – AZTECCO
    Commented May 30, 2023 at 16:36
2
\$\begingroup\$

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.

output

\$\endgroup\$
4
  • \$\begingroup\$ That's cheating! proper basic for Atari ST was Omikron, not GFA :D but it would be longer ;) \$\endgroup\$
    – Kaddath
    Commented May 30, 2023 at 13:52
  • \$\begingroup\$ @Kaddath Nah, the real thing was pure assembly with Devpac anyway! :p But GFA Punchs (20-liners) in ST Mag were fun! \$\endgroup\$
    – Arnauld
    Commented May 30, 2023 at 14:18
  • \$\begingroup\$ :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 \$\endgroup\$
    – Kaddath
    Commented May 30, 2023 at 14:44
  • \$\begingroup\$ @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. \$\endgroup\$
    – Arnauld
    Commented May 30, 2023 at 14:59
2
\$\begingroup\$

><> (Fish), 8 6 bytes

-2 thanks to @mousetail

1:n:+!

Try it

\$\endgroup\$
2
  • \$\begingroup\$ 6 bytes: 1:n2*! \$\endgroup\$
    – chunes
    Commented May 30, 2023 at 17:18
  • \$\begingroup\$ More fun 6 bytes: 1:n:+! \$\endgroup\$ Commented May 30, 2023 at 18:32
1
\$\begingroup\$

Desmoslang Assembly, 3 Bytes

2O*
\$\endgroup\$
2
  • \$\begingroup\$ This looks quite interesting! Does this transpile into Desmos code or how does this work? \$\endgroup\$
    – Aiden Chow
    Commented May 30, 2023 at 16:06
  • \$\begingroup\$ 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. \$\endgroup\$
    – Dadsdy
    Commented May 30, 2023 at 16:25
1
\$\begingroup\$

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!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Or 22 bytes using V8. \$\endgroup\$
    – Arnauld
    Commented May 29, 2023 at 17:01
  • 1
    \$\begingroup\$ @Arnauld 21 bytes. You don't need to have it start at one. \$\endgroup\$
    – Dadsdy
    Commented May 29, 2023 at 19:06
1
\$\begingroup\$

Haskell, 14 bytes

p=iterate(2*)1

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Retina, 16 bytes

K`1
+\`.+
$.(*2*

Try it online! Starts with 2. Explanation:

K`1

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.

\$\endgroup\$
1
\$\begingroup\$

Nekomata, 3 bytes

Ň2E

Attempt This Online!

Ň       Natural numbers
 2E     Power of 2
\$\endgroup\$
1
\$\begingroup\$

Befunge-98 (PyFunge), 5 bytes

2<.:*

Try it online!

Starts from 4.

\$\endgroup\$

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