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\$
  • \$\begingroup\$ Does the output need to be infinite, or can it stop after some amount of time? \$\endgroup\$ – Comrade SparklePony May 8 '17 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\$ – programmer5000 May 8 '17 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\$ – programmer5000 May 8 '17 at 20:22
  • \$\begingroup\$ @programmer5000 Thanks, got it. \$\endgroup\$ – Comrade SparklePony May 8 '17 at 20:23
  • \$\begingroup\$ Can I print a newline? \$\endgroup\$ – MD XF May 13 '17 at 18:56

41 Answers 41

12
\$\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\$
  • \$\begingroup\$ If you start by waiting 1 second you can use w instead of .W. \$\endgroup\$ – Riley May 8 '17 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\$ – Comrade SparklePony May 8 '17 at 20:00
  • \$\begingroup\$ @ComradeSparklePony You're right. it would have to be Gw. \$\endgroup\$ – Riley May 8 '17 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\$ – doppelgreener May 9 '17 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 '17 at 17:09
15
\$\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\$
  • 1
    \$\begingroup\$ why "+ 3 bytes"? \$\endgroup\$ – Cole Johnson May 8 '17 at 23:15
  • 2
    \$\begingroup\$ 9 blocks (e.g. Forever, n) + 3 bytes (e.g. x, 2) \$\endgroup\$ – OldBunny2800 May 8 '17 at 23:20
5
\$\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\$
  • \$\begingroup\$ You could save a byte on the print statement by switching to Python 2 :) \$\endgroup\$ – numbermaniac May 14 '17 at 2:35
  • 1
    \$\begingroup\$ @numbermaniac Yes, but then I'd have to switch to Python 2. :P \$\endgroup\$ – L3viathan May 14 '17 at 9:11
5
\$\begingroup\$

MATL, 8 bytes

`GD@WY.T

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\$
  • \$\begingroup\$ @programmer5000 Out of curiosity: did the online interpreter work for you? \$\endgroup\$ – Luis Mendo May 8 '17 at 20:02
  • \$\begingroup\$ Yes, it did, why? \$\endgroup\$ – programmer5000 May 8 '17 at 20:06
  • \$\begingroup\$ @programmer5000 Thanks. Just to check. Sometimes there are timeout issues \$\endgroup\$ – Luis Mendo May 8 '17 at 20:20
5
\$\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
    \$\begingroup\$ True is 1>0. But something like this is a bit shorter: ±n_:=#0[Print@n;Pause@#;2#]&@1 \$\endgroup\$ – Martin Ender May 8 '17 at 21:24
  • \$\begingroup\$ I put the 1<2 prior to your comment. However, your recursive solution does save a byte. Thanks @MartinEnder \$\endgroup\$ – Kelly Lowder May 8 '17 at 21:58
  • \$\begingroup\$ ± is one byte in CP-1252 encoding (default Windows encoding). \$\endgroup\$ – JungHwan Min May 8 '17 at 23:07
  • 3
    \$\begingroup\$ Even shorter: 1//.n_:>(Print@#;Pause@n;2n)& \$\endgroup\$ – ngenisis May 9 '17 at 0:43
5
\$\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
    \$\begingroup\$ A trick I learned from rahnema1: input(0) works \$\endgroup\$ – Luis Mendo May 9 '17 at 0:04
  • 1
    \$\begingroup\$ @LuisMendo Unfortunately the trick doesn't work in recent version of octave :( \$\endgroup\$ – rahnema1 May 9 '17 at 3:24
4
\$\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\$
  • 2
    \$\begingroup\$ +1 for "Do not use Java for golfing. It is a bad idea." Could you add a TIO link? \$\endgroup\$ – programmer5000 May 8 '17 at 20:26
  • \$\begingroup\$ @programmer5000 Sure, but it doesn't work, because TIO waits for the code to finish. \$\endgroup\$ – HyperNeutrino May 8 '17 at 20:30
  • 2
    \$\begingroup\$ Why an interface instead of a class? \$\endgroup\$ – rightfold May 8 '17 at 23:03
  • 2
    \$\begingroup\$ @rightfold An interface allows you to omit the public in public static void main. \$\endgroup\$ – Leaky Nun May 9 '17 at 2:23
  • 1
    \$\begingroup\$ @ColeJohnson the arguments are required. \$\endgroup\$ – Leaky Nun May 9 '17 at 2:24
4
\$\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\$
  • \$\begingroup\$ Why not start at i=1 then use i=i*2 at the end and sleep just i? \$\endgroup\$ – pajonk May 9 '17 at 6:16
  • \$\begingroup\$ that's a great idea...I'll change that. \$\endgroup\$ – Giuseppe May 9 '17 at 13:03
4
\$\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\$
  • \$\begingroup\$ It will sleep one second before reading the input, but you can enter it already \$\endgroup\$ – John Dvorak May 8 '17 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 '17 at 20:33
  • \$\begingroup\$ print without an argument is equivalent to puts$_ -- one byte saved \$\endgroup\$ – daniero May 9 '17 at 13:45
4
\$\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\$
4
\$\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
    \$\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 '17 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 '17 at 6:22
3
\$\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\$
  • \$\begingroup\$ oh wow this looks like a neat language \$\endgroup\$ – Skidsdev May 9 '17 at 8:20
3
\$\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\$
  • \$\begingroup\$ Yeah, it only returns output upon completion. \$\endgroup\$ – Jonathan Allan May 8 '17 at 22:01
3
\$\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\$
3
\$\begingroup\$

TI-Basic, 21 bytes

Prompt Str0
1
While 1
Disp Str0
Wait Ans
2Ans
End
\$\endgroup\$
2
\$\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\$
2
\$\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\$
2
\$\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\$
2
\$\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\$
2
\$\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\$
2
\$\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\$
  • \$\begingroup\$ You can save 1 byte by moving the Write statement to the body of the loop. \$\endgroup\$ – raznagul May 9 '17 at 9:00
  • \$\begingroup\$ @raznagul Don't know how I missed that one, thanks! \$\endgroup\$ – TheLethalCoder May 9 '17 at 9:01
2
\$\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\$
  • \$\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 '17 at 8:57
2
\$\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\$
  • 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 '17 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\$ – SomeShinyMonica May 10 '17 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 '17 at 11:35
  • \$\begingroup\$ @Joey, sweet that does work. Thanks for the tip \$\endgroup\$ – SomeShinyMonica May 11 '17 at 3:20
2
\$\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\$
  • \$\begingroup\$ Clever, but this is a memory hog. \$\endgroup\$ – eush77 May 13 '17 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 '17 at 22:00
1
\$\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\$
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\$
  • 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\$ – Business Cat May 8 '17 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\$ – HyperNeutrino May 8 '17 at 20:21
  • 1
    \$\begingroup\$ How about i=1,y=x=>setTimeout(y,i*=2,console.log(x))? \$\endgroup\$ – Neil May 8 '17 at 20:59
1
\$\begingroup\$

Common Lisp, 49 bytes

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

first delay should be 1 second.

\$\endgroup\$
  • 1
    \$\begingroup\$ You have 321 rep! \$\endgroup\$ – programmer5000 May 9 '17 at 13:49
  • \$\begingroup\$ @programmer5000 you have 3683 rep! \$\endgroup\$ – Cyoce May 9 '17 at 17:50
1
\$\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\$
1
\$\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\$
1
\$\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, privacy policy and cookie policy

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