15
\$\begingroup\$

Your challenge is to print the input, wait any amount of time, print the input, wait twice the time you initially waited, print the input again, and so on. The initial delay must be less than 1 hour, and you must have an accuracy of +/- 5% in the subsequent delays. Other than that, there is no restriction on the delay time.

Example:

Input: hi.

Output: hi (1ms pause) hi (2ms pause) hi (4ms pause) hi (8ms pause) hi (16ms pause), etc.

Also allowed:

hi (1 minute pause) hi (2 minute pause) hi (4 minute pause) hi (8 minute pause) hi (16 minute pause), etc.

Input must be provided at the start of the program (STDIN, command-line parameter, function param, etc.) and will be a string.

The initial delay can't be 0.

\$\endgroup\$
6
  • \$\begingroup\$ Does the output need to be infinite, or can it stop after some amount of time? \$\endgroup\$
    – sporkl
    May 8, 2017 at 20:17
  • 1
    \$\begingroup\$ @ComradeSparklePony it must output as long as it can (until heat death of universe, computer crash, stackoverflow, out of memory, etc) \$\endgroup\$
    – user58826
    May 8, 2017 at 20:18
  • \$\begingroup\$ @ComradeSparklePony only if it is something like stackoverflow, out of memory, etc. This y=x=>(x&&alert(x),y()) would be technically allowed, but I would downvote it. \$\endgroup\$
    – user58826
    May 8, 2017 at 20:22
  • \$\begingroup\$ @programmer5000 Thanks, got it. \$\endgroup\$
    – sporkl
    May 8, 2017 at 20:23
  • \$\begingroup\$ Can I print a newline? \$\endgroup\$
    – MD XF
    May 13, 2017 at 18:56

41 Answers 41

14
\$\begingroup\$

Scratch, 8 blocks + 3 bytes

set [n] to [1]; forever { say [x]; wait (n) secs; set [n] to ((n) * (2)) }

Equivalent in Python:

import time
n = 1
while 1:
    print("x")
    time.sleep(n)
    n = n * 2
\$\endgroup\$
2
  • 1
    \$\begingroup\$ why "+ 3 bytes"? \$\endgroup\$
    – Cole Tobin
    May 8, 2017 at 23:15
  • 3
    \$\begingroup\$ 9 blocks (e.g. Forever, n) + 3 bytes (e.g. x, 2) \$\endgroup\$
    – AAM111
    May 8, 2017 at 23:20
11
\$\begingroup\$

05AB1E, 6 bytes

Code:

[=No.W

Explanation:

[        # Start an infinite loop
 =       # Print the top of the stack without popping
  No     # Compute 2 ** (iteration index)
    .W   # Wait that many milliseconds

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ If you start by waiting 1 second you can use w instead of .W. \$\endgroup\$
    – Riley
    May 8, 2017 at 19:57
  • \$\begingroup\$ @Riley I don't think that would work. w waits one second no matter what, and .W pops a and waits that many milliseconds. \$\endgroup\$
    – sporkl
    May 8, 2017 at 20:00
  • \$\begingroup\$ @ComradeSparklePony You're right. it would have to be Gw. \$\endgroup\$
    – Riley
    May 8, 2017 at 20:01
  • \$\begingroup\$ I don't know if it's necessarily reflective of any issue in the code, but the moment I run the linked example, the engine gives me: "Warning: The request exceeded the 60 second time limit and was terminated." \$\endgroup\$ May 9, 2017 at 17:08
  • \$\begingroup\$ @doppelgreener Yeah, the online interpreter might not be the best option in this case. Offline works like a charm though. \$\endgroup\$
    – Adnan
    May 9, 2017 at 17:09
4
\$\begingroup\$

Python 3, 60 56 bytes

import time
def f(x,i=1):print(x);time.sleep(i);f(x,i*2)

Changelog:

  • changed recursive lambda to recursive function (-4 bytes)
\$\endgroup\$
2
  • \$\begingroup\$ You could save a byte on the print statement by switching to Python 2 :) \$\endgroup\$ May 14, 2017 at 2:35
  • 1
    \$\begingroup\$ @numbermaniac Yes, but then I'd have to switch to Python 2. :P \$\endgroup\$
    – L3viathan
    May 14, 2017 at 9:11
4
\$\begingroup\$

Mathematica 34 32 30 29 Bytes

Original solution 34 Bytes:

For[x=.1,1<2,Pause[x*=2];Print@#]&

Shave off 2 bytes with Do

x=1;Do[Pause[x*=2];Print@#,∞]&

Shave off one more Byte with @MartinEnder's recursive solution

±n_:=#0[Print@n;Pause@#;2#]&@1

@ngenisis uses ReplaceRepeated recursion to shave off another byte

1//.n_:>(Print@#;Pause@n;2n)&
\$\endgroup\$
4
  • 4
    \$\begingroup\$ True is 1>0. But something like this is a bit shorter: ±n_:=#0[Print@n;Pause@#;2#]&@1 \$\endgroup\$ May 8, 2017 at 21:24
  • \$\begingroup\$ I put the 1<2 prior to your comment. However, your recursive solution does save a byte. Thanks @MartinEnder \$\endgroup\$ May 8, 2017 at 21:58
  • \$\begingroup\$ ± is one byte in CP-1252 encoding (default Windows encoding). \$\endgroup\$ May 8, 2017 at 23:07
  • 3
    \$\begingroup\$ Even shorter: 1//.n_:>(Print@#;Pause@n;2n)& \$\endgroup\$
    – user61980
    May 9, 2017 at 0:43
4
\$\begingroup\$

Octave, 42 41 bytes

x=input('');p=1;while p*=2,pause(p),x,end

Saved one byte thanks to rahnema1, p*=2 is shorter than p=p*2.

I can't believe I haven't been able to golf this down, but it was actually not that easy.

  • The input must be in the start, so the first part is impossible to avoid.
  • I need a number that gets doubled, and it must be initialized in front of the loop
    • It would be possible to use the input as a conditional for the loop, but then I would have to have p*=2 somewhere else.
    • Pause doesn't have a return value, otherwise it could have been while pause(p*=2)
\$\endgroup\$
2
  • 2
    \$\begingroup\$ A trick I learned from rahnema1: input(0) works \$\endgroup\$
    – Luis Mendo
    May 9, 2017 at 0:04
  • 1
    \$\begingroup\$ @LuisMendo Unfortunately the trick doesn't work in recent version of octave :( \$\endgroup\$
    – rahnema1
    May 9, 2017 at 3:24
4
\$\begingroup\$

MATL, 8 bytes

`[email protected]

The first pause is 2 seconds.

Try it at MATL Online. Or see a modified version that displays the time elapsed since the program started. (If the interpreter doesn't work, please refresh the page and try again).

Or see a gif:

enter image description here

Explanation

`     % Do...while
  G   %   Push input
  D   %   Display
  @   %   Push iteration index (1-based)
  W   %   2 raised to that
  Y.  %   Pause for that time
  T   %   Push true. This will be used as loop confition
      % End (implicit). The top of the stack is true, which produces an infinite loop 
\$\endgroup\$
3
  • \$\begingroup\$ @programmer5000 Out of curiosity: did the online interpreter work for you? \$\endgroup\$
    – Luis Mendo
    May 8, 2017 at 20:02
  • \$\begingroup\$ Yes, it did, why? \$\endgroup\$
    – user58826
    May 8, 2017 at 20:06
  • \$\begingroup\$ @programmer5000 Thanks. Just to check. Sometimes there are timeout issues \$\endgroup\$
    – Luis Mendo
    May 8, 2017 at 20:20
3
\$\begingroup\$

Java (OpenJDK 8), 113 bytes

interface M{static void main(String[]a)throws Exception{for(int i=1;;Thread.sleep(i*=2))System.out.print(a[0]);}}

Try it online!

-60 bytes thanks to Leaky Nun!

\$\endgroup\$
8
  • 2
    \$\begingroup\$ +1 for "Do not use Java for golfing. It is a bad idea." Could you add a TIO link? \$\endgroup\$
    – user58826
    May 8, 2017 at 20:26
  • \$\begingroup\$ @programmer5000 Sure, but it doesn't work, because TIO waits for the code to finish. \$\endgroup\$
    – hyper-neutrino
    May 8, 2017 at 20:30
  • \$\begingroup\$ You can probably use a function instead (by my reading of the question) which will greatly reduce the boilerplate needed: void f()throws Exception{...} \$\endgroup\$
    – Justin
    May 8, 2017 at 22:45
  • 2
    \$\begingroup\$ Why an interface instead of a class? \$\endgroup\$
    – user11779
    May 8, 2017 at 23:03
  • 2
    \$\begingroup\$ @rightfold An interface allows you to omit the public in public static void main. \$\endgroup\$
    – Leaky Nun
    May 9, 2017 at 2:23
3
\$\begingroup\$

R, 50 48 bytes

function(x,i=1)repeat{cat(x);Sys.sleep(i);i=i*2}

returns an anonymous function which has one mandatory argument, the string to print. Prints no newlines, just spits x out on the screen. i is an optional argument that defaults to 1, waits for i seconds and doubles i.

-2 bytes thanks to pajonk

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Why not start at i=1 then use i=i*2 at the end and sleep just i? \$\endgroup\$
    – pajonk
    May 9, 2017 at 6:16
  • \$\begingroup\$ that's a great idea...I'll change that. \$\endgroup\$
    – Giuseppe
    May 9, 2017 at 13:03
3
\$\begingroup\$

Ruby, 34 28 23 22 (+2 for -n) = 24 bytes

3 bytes saved thanks to Value Ink!

1 byte saved thanks to daniero

loop{print;sleep$.*=2}

Starts at 2, then 4, etc.

Explanation

-n                       # read a line from STDIN
  loop{                } # while(true):
       print;            # print that line
             sleep$.*=2  # multiply $. by 2, then sleep that many seconds. 
                         # $. is a Ruby special variable that starts at 1.
\$\endgroup\$
3
  • \$\begingroup\$ It will sleep one second before reading the input, but you can enter it already \$\endgroup\$ May 8, 2017 at 19:53
  • \$\begingroup\$ Starting the Ruby program with the -n flag lets you skip the initial gets call, because the flag will handle it for you \$\endgroup\$
    – Value Ink
    May 8, 2017 at 20:33
  • \$\begingroup\$ print without an argument is equivalent to puts$_ -- one byte saved \$\endgroup\$
    – daniero
    May 9, 2017 at 13:45
3
\$\begingroup\$

Alice, 16 bytes

1/?!\v
T\io/>2*.

Try it online! (Not much to see there of course, but you can check how often it was printed within one minute.)

Explanation

1    Push 1 to the stack. The initial pause duration in milliseconds.
/    Reflect to SE. Switch to Ordinal.
i    Read all input.
!    Store it on the tape.
/    Reflect to E. Switch to Cardinal.
>    Move east (does nothing but it's the entry of the main loop).
2*   Double the pause duration.
.    Duplicate it.
     The IP wraps around to the first column.
T    Sleep for that many milliseconds.
\    Reflect to NE. Switch to Ordinal.
?    Retrieve the input from the tape.
o    Print it.
\    Reflect to E. Switch to Cardinal.
v    Move south.
>    Move east. Run another iteration of the main loop.
\$\endgroup\$
3
\$\begingroup\$

R, 44 43 bytes

Crossed out 44 is still regular 44 ;(

This answer already provides a decent solution, but we can save some more bytes.

function(x)repeat{cat(x);Sys.sleep(T<-T*2)}

Anonymous function taking practically anything printable as argument x. Starts at 2 seconds and doubles every time afterwards. Abuses the fact that T is by default defined as TRUE which evaluates to 1.

Also, as long as this comment still gets a green light from OP, we can make it even shorter, but I don't think it is in the spirit of the challenge. Wait times of 0 are not allowed anymore.

function(x)repeat cat(x)
\$\endgroup\$
2
  • 2
    \$\begingroup\$ look at you, abusing poor T like that. in the shorter version of the answer, you don't even need braces, just a space. \$\endgroup\$
    – Giuseppe
    May 9, 2017 at 16:36
  • 1
    \$\begingroup\$ Hey, if T doesn't like it, T can stand up for itself. Also, nice find :) \$\endgroup\$
    – JAD
    May 10, 2017 at 6:22
2
\$\begingroup\$

Cubix, 30 bytes

/(?:u<q.;1A>?ou2$/r;w;q^_q.\*/

Try it here

This maps onto a cube with side length 3.

      / ( ?              # The top face does the delay.  It takes the stack element with the
      : u <              # delay value, duplicates and decrements it to 0.  When 0 is hit the
      q . ;              # IP moves into the sequence which doubles the delay value.
1 A > ? o u 2 $ / r ; w  # Initiates the stack with one and the input.  For input hi this
; q ^ _ q . \ * / . . .  # gives us 1, -1, 10, 105, 104.  There is a little loop that prints 
. . . . . . . . . . . .  # each item in the stack dropping it to the bottom until -1 is hit.
      . . .              # Then the delay sequence is started om the top face
      . . .
      . . .
\$\endgroup\$
1
  • \$\begingroup\$ oh wow this looks like a neat language \$\endgroup\$
    – Mayube
    May 9, 2017 at 8:20
2
\$\begingroup\$

Bash, 37 bytes

for((t=1;;t*=2)){ sleep $t;echo $1;};

For some reason TIO won't show the output until you stop the program execution.

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Yeah, it only returns output upon completion. \$\endgroup\$ May 8, 2017 at 22:01
2
\$\begingroup\$

PHP, 31 bytes

for(;;sleep(2**$i++))echo$argn;
for(;;sleep(1<<$i++))echo$argn;

sleeps 1, 2, 4, 8, ... seconds. Run as pipe with php -nR '<code>'

Will work until the 63rd print (on a 64 bit machine), after that there will be no more waiting.
Version 1 will yield warnings sleep() expects parameter 1 to be integer, float given,
Version 2 will yield one warning sleep(): Number of seconds must be greater than or equal to 0.

Insert @ before sleep to mute the warnings.

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

TI-Basic, 21 bytes

Prompt Str0
1
While 1
Disp Str0
Wait Ans
2Ans
End
\$\endgroup\$
1
\$\begingroup\$

Python 3, 61 bytes

import time;i=1;x=input()
while 1:print(x);time.sleep(i);i*=2

Similar to @L3viathan's golf, but uses while loop

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

CJam, 26 bytes

qKes{es1$-Y$<{W$o;2*es}|}h

Doesn't work properly on TIO.

The first pause is 20 milliseconds.

Explanation

q                           e# Push the input.
 K                          e# Push 20 (the pause time).
  es                        e# Push the time (number of milliseconds since the Unix epoch).
    {                       e# Do:
     es1$-                  e#  Subtract the stored time from the current time.
          Y$<{              e#  If it's not less than the pause time:
              W$o           e#   Print the input.
                 ;2*es      e#   Delete the stored time, multiply the pause time by 2, push
                            e#     the new time.
                      }|    e#  (end if)
                        }h  e# While the top of stack (not popped) is truthy.
                            e#  (It always is since the time is a positive integer)
\$\endgroup\$
1
\$\begingroup\$

C, 51 bytes

main(c,v)char**v;{puts(v[1]);sleep(c);main(2*c,v);}

C, 35 bytes as a function

c=1;f(n){puts(n);sleep(c*=2);f(n);}

Takes input as a command line argument.

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

Batch, 62 bytes

@set/at=%2+0,t+=t+!t
@echo %1
@timeout/t>nul %t%
@%0 %1 %t%

This turned out to be a byte shorter than explicitly doubling t in a loop:

@set t=1
:g
@echo %1
@timeout/t>nul %t%
@set/at*=2
@goto g
\$\endgroup\$
1
\$\begingroup\$

JS (ES6), 44 42 40 38 36 bytes

Crossed out 44 is still 44

i=1,y=x=>setTimeout(y,i*=2,alert(x))

Don't like alert bombs?

i=1,y=x=>setTimeout(y,i*=2,console.log(x))

Technically correct, but loophole-abusing:

y=x=>(x&&alert(x),y())

-3 bytes thanks to Cyoce, -2 thanks to Business Cat, -2 thanks to Neil

\$\endgroup\$
3
  • 2
    \$\begingroup\$ I don't seem to be able to test this properly, but you could probably do i=1,y=x=>(alert(x),setTimeout(y,i*=2)) to save a couple bytes \$\endgroup\$ May 8, 2017 at 20:21
  • 1
    \$\begingroup\$ I went ahead and edited in a credit message for Cyoce; if you want to change it, feel free to edit/rollback. \$\endgroup\$
    – hyper-neutrino
    May 8, 2017 at 20:21
  • 1
    \$\begingroup\$ How about i=1,y=x=>setTimeout(y,i*=2,console.log(x))? \$\endgroup\$
    – Neil
    May 8, 2017 at 20:59
1
\$\begingroup\$

Reticular, 12 bytes

1idp~dw2*~2j

Try it online!

Explanation

1idp~dw2*~2j
1               push 1 (initial delay)
 i              take line of input
  d             duplicate it
   p            print it
    ~           swap
     d          duplicate it
      w         wait (in seconds)
       2*       double it
         ~      swap
          2j    skip next two characters
1i              (skipped)
  d             duplicate input
   p            print...
                etc.
\$\endgroup\$
1
\$\begingroup\$

C#, 80 79 bytes

s=>{for(int i=1;;System.Threading.Thread.Sleep(i*=2))System.Console.Write(s);};

Saved one byte thanks to @raznagul.

\$\endgroup\$
2
  • \$\begingroup\$ You can save 1 byte by moving the Write statement to the body of the loop. \$\endgroup\$
    – raznagul
    May 9, 2017 at 9:00
  • \$\begingroup\$ @raznagul Don't know how I missed that one, thanks! \$\endgroup\$ May 9, 2017 at 9:01
1
\$\begingroup\$

Python 2, 54 bytes

Uses a lengthy calculation instead of timing libraries.

def f(x,a=1):
 while 1:a*=2;exec'v=9**9**6;'*a;print x
\$\endgroup\$
1
  • \$\begingroup\$ I now notice that my answer is similar to yours. I had not read your answer when I posted, but if you would like to incorporate my solution into yours I'll happily remove my answer. \$\endgroup\$
    – Tim
    May 10, 2017 at 8:57
1
\$\begingroup\$

PowerShell, 35 33 30 29 Bytes

With a helpful hint from whatever and Joey

%{for($a=1){$_;sleep($a*=2)}}

Explanation

%{          # Foreach
for($a=1){  # empty for loop makes this infinite and sets $a
$_;         # prints current foreach item
sleep($a*=2)# Start-Sleep alias for $a seconds, reassign $a to itself times 2           
}}          # close while and foreach

Executed with:

"hi"|%{for($a=1){$_;sleep($a*=2)}}
\$\endgroup\$
4
  • 1
    \$\begingroup\$ you should be able to use an empty for instead of the while: %{$a=1;for(){$_;sleep($a*=2)}}`` \$\endgroup\$
    – whatever
    May 10, 2017 at 10:16
  • \$\begingroup\$ Thanks! I had tried using a for loop before but I put for(;;). Didn't even try to remove the semi-colons. \$\endgroup\$ May 10, 2017 at 10:24
  • 1
    \$\begingroup\$ Put the $a=1 as the initialization into the for to save another byte (for($a=1){...}). Also, I'm not sure whether to count the %, as the actual routine you're running is just a script block. (My challenges tend to be rather strict about requiring a program, sidestepping such ponderings, but for anything goes questions I'm still not quite sure how to count various ways of using PowerShell.) \$\endgroup\$
    – Joey
    May 10, 2017 at 11:35
  • \$\begingroup\$ @Joey, sweet that does work. Thanks for the tip \$\endgroup\$ May 11, 2017 at 3:20
1
\$\begingroup\$

Python 3, 49 bytes

b=input();x=6**6
while 1:print(b);exec("x+=1;"*x)

Uses the slight delay of the += operation and executes it x times. x doubles by adding one to itself as many times as the value of x.

It starts at 6^6 (46656) to stick to the maximum of 5% variation in the delay.

\$\endgroup\$
2
  • \$\begingroup\$ Clever, but this is a memory hog. \$\endgroup\$
    – eush77
    May 13, 2017 at 19:03
  • \$\begingroup\$ @eush77 yes, on my tablet it terminated after just 7 iterations of the loop! I expect it would last a few longer on my desktop. \$\endgroup\$
    – Tim
    May 13, 2017 at 22:00
0
\$\begingroup\$

Perl 6, 39 bytes

print(once slurp),.&sleep for 1,2,4...* 

Try it (print overridden to add timing information)

Expanded:

  print(        # print to $*OUT
    once slurp  # slurp from $*IN, but only once
  ), 
  .&sleep       # then call sleep as if it was a method on $_

for             # do that for (sets $_ to each of the following)

  1, 2, 4 ... * # deductive sequence generator
\$\endgroup\$
0
\$\begingroup\$

Common Lisp, 49 bytes

(do((a(read))(i 1(* 2 i)))(())(print a)(sleep i))

first delay should be 1 second.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You have 321 rep! \$\endgroup\$
    – user58826
    May 9, 2017 at 13:49
  • \$\begingroup\$ @programmer5000 you have 3683 rep! \$\endgroup\$
    – Cyoce
    May 9, 2017 at 17:50
0
\$\begingroup\$

Pyth, 7 bytes

#|.d~yT

Explanation:

#           Infinitely loop
  .d         Delay for 
      T      10 seconds
    ~y       and double T each time
 |           print input every iteration, too
\$\endgroup\$
0
\$\begingroup\$

TI-BASIC, 36 bytes

Initial wait period is 1 second.

1→L
Input Str1
checkTmr(0→T
While 1
While L>checkTmr(T
End
Disp Str1
2L→L
End
\$\endgroup\$
0
\$\begingroup\$

Racket, 51 bytes

(λ(s)(do([n 1(* n 2)])(#f)(displayln s)(sleep n)))

Example

➜  ~  racket -e '((λ(s)(do([n 1(* n 2)])(#f)(displayln s)(sleep n)))"Hello")'
Hello
Hello
Hello
Hello
Hello
^Cuser break
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.