101
\$\begingroup\$

Some sleep commands implement a delay of an integer number of seconds. However, 2³² seconds is only about 100 years. Bug! What if you need a larger delay?

Make a program or a function which waits for 1000 years, with an error of less than ±10%. Because it's too time-consuming to test, please explain how/why your solution works! (unless it's somehow obvious)

You don't need to worry about wasting CPU power - it can be a busy-loop. Assume that nothing special happens while your program waits - no hardware errors or power failures, and the computer's clock magically continues running (even when it's on battery power).

What the solution should do after the sleep is finished:

  • If it's a function: return to caller
  • If it's a program: terminate (with or without error) or do something observable by user (e.g. display a message or play a sound)
\$\endgroup\$
2
  • 2
    \$\begingroup\$ “Around computers it is difficult to find the correct unit of time to measure progress. Some cathedrals took a century to complete. Can you imagine the grandeur and scope of a program that would take as long?” —Epigrams in Programming, ACM SIGPLAN Sept. 1982. (via fortune CLI app) \$\endgroup\$
    – roblogic
    Commented Dec 17, 2023 at 1:44
  • 1
    \$\begingroup\$ Because it's too time-consuming to test,: If your implementation is based on polling a real‑time clock (RTC), you may simply adjust the RTC to a date/time about 1 kiloyears in the future. In UNIX‑like environments like Linux or FreeBSD you can invoke the date command‑line utility to that end. What if you need a larger delay?: Easy, employ time dilation. 😉 \$\endgroup\$ Commented Mar 8 at 23:07

59 Answers 59

1
2
3
\$\begingroup\$

Applescript, 10

delay 3e10

Sleeps for 3 * 1010 seconds.

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

05AB1E, 7 6 bytes

35oF.Z

~8.879% too large

Also take a look at this excellent alternative 6-byter by @anatolyg.

Previous 7-byters:

žqþ¨¨.W     # ~0.449% too small
žG14∍.W     # ~3.837% too large
13°3*.W     # ~4.936% too small

For 100% precision (: assuming a year is 365.25 days), use •Tε‚šä¦•.W (10 bytes) instead.

Explanations:

35          # Push 35
  o         # Pop and push 2 to the power 35: 34359738368
   F        # Loop that many times:
    .Z      #  And sleep for 1 second every iteration

žq          # Push PI with 15 decimal digits by default: 3.141592653589793
  þ         # Remove the dot by only leaving the digits: 3141592653589793
   ¨¨       # Remove the last two digits: 31415926535897
     .W     # Sleep that many ms

žG          # Push builtin integer 32768
  14∍       # Extend it to size 14: 32768327683276
     .W     # Sleep that many ms

13°         # Push 10 to the power 13: 10000000000000
   3*       # Multiply it by 3: 30000000000000
     .W     # Sleep that many ms

•Tε‚šä¦•    # Push compressed integer 31557600000000
        .W  # Sleep that many ms

See this 05AB1E tip of mine (section How to compress large integers?) to understand why •Tε‚šä¦• is 31557600000000.

05AB1E is built in Elixir, and its sleep builtin can even hold :infinity apparently, so I can assume the .W has no maximum.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 5 bytes: ₆9e.W (waits ~8% too long). \$\endgroup\$
    – Grimmy
    Commented Dec 13, 2019 at 13:50
  • \$\begingroup\$ @Grimmy Nice, I knew 5 bytes should be possible somehow with some of the 1-byte constants and 1-byte builtins! I was playing around with palindromize, factorize, euler_constant, etc. but forgot about e. Feel free to post it as your own answer in this case, since there are already two 05AB1E answers and my answer is already a bit too full (don't really want to remove the 7 byters). I'll upvote you when you do. \$\endgroup\$ Commented Dec 13, 2019 at 13:52
3
\$\begingroup\$

JavaScript (browser), 19 bytes

setTimeout("a",3e13)

This will throw and error in about a 1000 years.

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

Minecraft Command, 45 bytes

/summon wind_charge ~5 ~ ~ {Motion:[-7e-12d]}

Summons a wind charge 5 blocks away from the player, with a motion towards the player of 7×10⁻¹² blocks per gametick. The wind charge moves slowly towards the player until it's less than 0.6 blocks away, where it touches the player's hitbox and explodes.

This takes (5 - 0.6) / 7e-12 = 628571428571 gameticks or 31428571429 seconds, which is 0.4% less than 1000 years (assuming each year is 365.2425 days long).

This is what it looks like sped up by a factor of 10¹⁰.

gif of wind charge sped up

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

SAS, 38 bytes

data;do i=1to 3e10;a=sleep(1);end;run;

This loop seems to be the shortest option as both sleep and wakeup are capped at 46 days - slightly less than 222 seconds. I wonder what the rationale for that was?

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

JavaScript (Node.js), 34 bytes

uses the 3e13 like most answers but in a for loop. The 1 at the end is to make it a valid for loop.

d=Date.now;for(i=d();i>d()-3e13;)1

Try it online!

Don't actually try it...

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

Roblox, 11 bytes

wait(3e+10)

Waits 30,000,000,000 seconds, which is roughly equal to 950 years.

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

MSX-BASIC, 47 characters

1FORK=1TO31557600000:TIME=0
2IFTIME<50GOTO2
3NEXT

TIME is a MSX-BASIC system variable holding a 50Hz counter that can be initialized to any value at will. There are 31557600000 seconds in 1000 years (assuming one leap year every 4 years) so there you go.

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

RPL, 8 bytes

3E10WAIT

shall wait 3e10 seconds on, say, your hp48sx. Good the OP excluded hardware issues, so we can ignore the reboot at the end of Dec. 31st, 2088 (the date is reinitialized to Jan. 1st, 1989.)

For RPL/2, add one byte as a space is needed before WAIT.

I am surprised such commands are accepted anyhow…

Btw a challenge about 2763 years would allow us to use the factorial as in 14!WAIT.

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

MathGolf, 15 bytes

{tï╜Ä_⌐_¬Wó+<}▲

Just like my top 05AB1E answer, it's ~8.879% too large.

Try it online with debugging argument -d to see what's going on.

Explanation:

There were a few difficulties to tackle here:

  1. Apparently there is no way in MathGolf to access a value outside of the scope of a while-loop. I therefore had to push the initial timestamp inside the loop, and by using some duplicates and stack-rotates access it to check against the current timestamp.
  2. After that I initially had {t_⌐_¬Wó+<}▲ (12 bytes) to overcome the issue mentioned at point 1. Unfortunately there are some bugs in MathGolf, so this required a leading 1, otherwise the do-while didn't work: 1{t_⌐_¬Wó+<}▲.
  3. MathGold is built in Python, and since Python's List size is limited to 536,870,912 items, I therefore had to remove unused garbage from the stack instead of letting it grow by one additional item every iteration. (Since it executes roughly 5-10 iterations per ms on TIO, the stack size grows way too fast to last even close to 1000 years.) I did this by adding an if-statement in front of that initial duplicate, so I only duplicate it in the very first iteration. For some reason I could now also remove that 1 which was used as a bug-workaround..

Putting it all together gave me the 15-byter you see here, with the following code explanation:

{            }▲  # Do-while true with pop:
 t               #  Push the current timestamp
  ï              #  Push the current 0-based loop-index
   ╜             #  If it's falsey (so it's the first iteration):
    Ä            #   Execute the following single command:
     _           #    Duplicate this initial timestamp
      ⌐          #  Rotate the stack, so the bottom item is at the top
       _         #  Duplicate it
        ¬        #  And rotate the stack back, so its both at the top and bottom now
         Wó      #  Push 35, and then pop and push 2**35: 34359738368
           +     #  Add it to the timestamp of the first iteration
            <    #  And check whether it's still lower than the current timestamp
\$\endgroup\$
2
\$\begingroup\$

Python 3, all platforms, 91 bytes

from datetime import *;z=datetime.now()+timedelta(days=365242)
while z>datetime.now(): pass

The rules say simple termination is fine, which happens after the while loop is done. I couldn't put it all on one line, because the while loop has to start on its own line for the interpreter to recognize it. I also didn't want to use sleep since the challenge specifically calls that out as the problem.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ The spaces before * and after : are not necessary. Instead of pass you can use a simple value, the shortest would be a single digit number. \$\endgroup\$
    – manatwork
    Commented Jan 6, 2020 at 9:53
2
\$\begingroup\$

Rust, 55 bytes

||std::thread::sleep(std::time::Duration::new(2<<34,0))

Try it online! (or not, it will take ~1089 years to do anything interesting)

Rust's thread::sleep takes a Duration as an input, and those conveniently store the count of seconds as a u64.

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

VBA, 27 bytes

This program adds an approximation of 1000 years, in days, to the current time, and instructs the program to wait until that time is encountered.

Application.Wait 365242+Now

For for a more accurate result, the below may be used at a cost of 41 bytes.

Application.Wait DateAdd("yyyy",1000,Now)
\$\endgroup\$
2
\$\begingroup\$

CJam, 15 14 bytes

-1 byte because I forgot that e[numeric literal] existed.

es3e13+{_es>}g

Please don't try it online, it won't work!

This waits in a while loop until the current Unix timestamp is equal to the timestamp at the beginning of execution plus 3*10^13 milliseconds (or ~951.3 years).

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

J, 11 bytes

    6!:3\i.2^18

Explanation:

  • 6!:3: 'delay' function
  • \: Loop over the entire following value
  • i.2^18: A list of values from 0 to (2^18)-1

Thanks to @CriminallyVulgar for the 2^18 idea! Try it online... or maybe don't

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Code Golf, and nice answer! \$\endgroup\$
    – rydwolf
    Commented May 23, 2022 at 23:13
2
\$\begingroup\$

Python 3, 46 48 bytes

import time
for _ in range(7889):time.sleep(4e6)

Try it online!

Based on this answer. Used for loop to circumvent the overflow in time.sleep(). 3e10 shows overflow but 3e9 doesn't. No Overflow errors are show on the online interpreters(TIO and ATO) but on attempting on personal computer it does show error. The suggestion by @anatolyg has been implemented, which is working on both interpreters.

Previous erroneous answer

import time
for _ in range(10):time.sleep(3e9)
\$\endgroup\$
2
  • \$\begingroup\$ On my Windows machine, sleep(3e9) overflows. Maximal number of seconds for which sleep succeeds is 4294967 (2**32/1000). Here, the following code would work: for _ in range(7889):time.sleep(4e6). On which platform does sleep(3e9) succeed? \$\endgroup\$
    – anatolyg
    Commented May 18, 2022 at 7:36
  • 1
    \$\begingroup\$ @anatolyg sleep(3e9) works for me on TIO and ATO(online code execution). I have now checked my code on my computer and it does show Overflow error for 3e9. Perhaps the online interpreters are using some kind of optimization. Either way, I will change my answer to reflect your point. \$\endgroup\$
    – Saphereye
    Commented May 18, 2022 at 8:26
2
\$\begingroup\$

Fortran (GFortran), 42 bytes

integer(kind=8)i;i=56**6
call sleep(i)
end

Try it online!

Gnu Fortran's sleep routine only works with an integer number of seconds. Problem is, the default integer type in GFortran only goes up to 2147483647 which only gets us to less than 100 years, and (according to the units utility) we need approx 3.1556926e+10 seconds to get us to 1000 years. But we can tweak i to a larger type, using kind=8, which works fine with sleep.

Setting i=56**6 gets us to 30840979456 seconds, which is more interesting (and closer to 1000 years) than i=3e10. I found this number by messing around with cube roots and higher with gnu bc -l. Example: e(l(3e10)/3) in bc gets the cube root, e(l(3e10)/6) gets the 6th root!

I suppose I could have just looped the program a few times instead of faffing about with integer types but where's the fun in that?

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

Zsh/Bash/Sh, 23,19,14,10 bytes

sleep 8e6h

-4 bytes thanks to @Grimmy

Explanation

GNU coreutils sleep command can take a suffix of either s (seconds), m (minutes), h (hours), or d (days). It also accepts IEEE-754 numbers, which allows us to use the shorter form of 8e6 to specify 8M hours.

8 million hours is equivalent to roughly 912½ years.

\$\endgroup\$
2
  • \$\begingroup\$ sleep 3e10, sleep 8e6h, sleep 5e8m are all 10 bytes and within the 10% tolerance. \$\endgroup\$
    – Grimmy
    Commented Dec 16, 2019 at 12:51
  • \$\begingroup\$ @Grimmy Thank you! \$\endgroup\$
    – smac89
    Commented Dec 16, 2019 at 18:38
1
\$\begingroup\$

Red, 9 bytes

wait 3e10

Try it online!

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

Tcl, 23 bytes

after 30000000000000000

Try it online!

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

at, 19 bytes

at now + 1000 years
\$\endgroup\$
3
  • \$\begingroup\$ @JoKing I think it's linux command at \$\endgroup\$ Commented Dec 13, 2019 at 13:03
  • 4
    \$\begingroup\$ You don't need the plural on "years" for the date parsing here, nor the spaces actually: at now+1000year is 15 bytes and does the same work. \$\endgroup\$
    – Caleb
    Commented Dec 15, 2019 at 5:59
  • 3
    \$\begingroup\$ @ceving @Caleb I'm not familiar with this language, but why not use at now+999year? An error of less than ±10% is allowed. \$\endgroup\$
    – qarz
    Commented Oct 16, 2020 at 19:57
1
\$\begingroup\$

Factor, 10 bytes

3e16 sleep

Try it online!

Factor's sleep word is a generic word that can take either a real number denoting nanoseconds or a duration. 3e16 nanoseconds is within 5% error of 1000 years if my calculations are correct.

More straightforward at the cost of some bytes:

Factor, 15 bytes

1e3 years sleep

Try it online!

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

Stax, 8 7 6 bytes

ê¼@µ~¼

Run and debug it

Approach

17¹⁰ times: wait for one animation frame at 60fps.

Here's a test for 10 seconds.

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

Commodore BASIC (Commodore C64/128, C16/+4, VIC-20, CBM/PET), 92 Tokenised BASIC bytes

A thousand years in the Gregorian Calendar at least 365,240 days, plus two or three days depending on the 400 year rule, whereby each start of a new century is a leap year but only once every 400 years, with the year 2000 being a leap year, but 1900 and 2100 are not leap years. As the next leap year at the start of a century will be 2400 I will assume that you want to sleep for 365,242 days. How do we do this with Commodore BASIC?

A day according to a Commodore BASIC interpreter is 5,184,001 jiffies (or 1/60 of a second), which is actually one 60th too many, so over 365,242 days, we are out by the same number of jiffies, which is about 101 minutes, so we will be sleeping for about 1000 years + 101 minutes based on our assumptions above. You can see an explanation of the Commodore TI and TI$ functions on the 8-BIT Show and Tell YouTube channel here.

Firstly, we need to take a copy of the current system time, which one can set in HHMMSS in a 24 hour format like TI$="123000" - we then need a busy loop to wait a second so that the copy of the current time and the system timer are not the same. I am also setting the number of days to wait in the W variable, which is 365241 as our counter D starts at zero.

0 C$=TI$: W=365241: D=0: FOR I=0 TO 937: NEXT I

Now, we need to wait until TI$ is equal to our copy stored in C$, each time it is we know that it has been 24 hours and 1/60th of a second. When this is true, we can jump to a sub routine that will add 1 to the number of days, and return. Note that using STEP 0 in our FOR/NEXT loop creates an endless loop with our break condition being in the subroutine on line 2.

1 FOR I=0 TO 1 STEP 0: ON -(TI$=C$) GOSUB 2: NEXT I

When we jump to the subroutine at line 2, TI$ and C$ may still contain the same values, so we need a busy loop until TI$ is one second ahead again of C$. Then we can add 1 to our day counter, and if the counter has fewer days than our W variable set above, we will return from our sub-routine which will wait again until another 24 hours and 1 jiffy has passed.

2 ON -(TI$=C$) GOTO 2: D=D+1: IF D<W THEN RETURN

The full crunched listing (with removed whitespaces) would look like this:

0C$=TI$:W=365241:D=.:FORI=.TO1E3:NEXT
1FORI=.TO1STEP.:ON-(TI$=C$)GOSUB2:NEXT
2ON-(TI$=C$)GOTO2:D=D+1:IFD<WTHENRETURN

Commodore C64 Sleep for 1000 years

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

YASEPL, 31 bytes

=aŔ9+31556952000000`1=bŔ9}2,a

terminates when finished.

sets a date to a, adds 1000 years in milliseconds, then goes into a while loop that checks if the current date is less than a. aka:

let a = new Date() + 31556952000000;

do {
  let b = new Date();
} while(b < a);
\$\endgroup\$
1
\$\begingroup\$

Uiua, 7 bytes

&sl3e10

Explanation

&sl3e10
   3e10 # Push 30000000000 to the stack
&sl     # Sleep for that many seconds

On Uiua's online pad, &sl hangs the page for how many seconds specified. Here's a sample that hangs for 10 seconds.

Unfortunately, 3e10 seconds is only around 951 years, so everyone'll have to wait 49 more. Thankfully it's only 95.07% of a millennium in seconds.

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

Scratch (scratchblocks3 syntax), 30 bytes

It's the obvious solution but it's surprisingly short for a Scratch program. Sleeps for exactly 951.3 years + a few milliseconds.

when gf clicked
wait(3e10)secs
\$\endgroup\$
3
  • \$\begingroup\$ I don't think it'd be a few milliseconds, since you're giving it an integer amount of seconds? \$\endgroup\$ Commented Nov 18, 2023 at 17:03
  • \$\begingroup\$ @noodleman Scratch adds a small artificial delay after each block \$\endgroup\$
    – qarz
    Commented Nov 20, 2023 at 5:43
  • \$\begingroup\$ Ah, forgot about the delay. \$\endgroup\$ Commented Nov 20, 2023 at 12:56
0
\$\begingroup\$

Pascal, 284 B

This complete Pascal program requires a processor supporting features defined by ISO standard 10206 “Extended Pascal”, in particular the timeStamp built‑in data type and getTimeStamp routine. The program simply performs a busy wait.

program p;var r,n:timeStamp;begin getTimeStamp(r);with r do begin
year:=year+1000;day:=day-ord(month=2)*ord(not(0 in[year mod 4]-[year mod 100]+[year mod 400]));repeat
getTimeStamp(n)
until[1=1]=[n.year=year,n.month=month,n.day=day,n.hour=hour,n.minute=minute,n.second=second]end end.

The program terminates if exactly 1 kiloyears have passed (second granularity), else it is non‑terminating (for instance if the operating system’s scheduler put the program in a waiting state during the crucial second).

program millenniumSleep;
    var
        reference, now: timeStamp;
    begin
        getTimeStamp(reference);
        { We presume `reference.dateValid` and `reference.timeValid` are `true`.
          The processor does feature an operational real­‑time clock. }
        
        with reference do
        begin
            { Add a millennium to `timeStamp`. }
            year ≔ year + 1000;
            { Accommodate for leap day. }
            day  ≔ day − ord(month = 2) *
                ord(not (0 in [year mod 4] − [year mod 100] + [year mod 400]));
            
            { We presume evaluation of the loop takes at most one second. }
            repeat
            begin
                getTimeStamp(now);
            end
            { The embracing `with reference do` language construct
              allows us to abbreviate `reference.year` to `year` and so forth. }
            until [true] = [now.year = year, now.month = month, now.day = day,
                now.hour = hour, now.minute = minute, now.second = second]
        end
    end.

The leap year test has been adopted from a different challenge. The program needs to be started before the year maxInt − 1000. The value of the built‑in constant maxInt is implementation‑defined.

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

Kotlin, 25 bytes

{Thread.sleep(7L shl 42)}

111 followed by 41 zeros (binary) is ~97.56% of 31556952000000 (decimal)

Try it online!

\$\endgroup\$
1
2

Your Answer

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

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