85
\$\begingroup\$

Some sleep commands implement a delay of an integer number of seconds. However, 232 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\$

45 Answers 45

1
2
1
\$\begingroup\$

Red, 9 bytes

wait 3e10

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Tcl, 23 bytes

after 30000000000000000

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\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.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

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

sleep 8e6h

-4 bytes thanks to @Grimmy

Explanation

Linux sleep command can take a suffix of either s (seconds), m (minutes), h (hours), or d (days).

365250 days is equivalent to 1000 years

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ sleep 3e10, sleep 8e6h, sleep 5e8m are all 10 bytes and within the 10% tolerance. \$\endgroup\$ – Grimmy Dec 16 '19 at 12:51
  • \$\begingroup\$ @Grimmy Thank you! \$\endgroup\$ – smac89 Dec 16 '19 at 18:38
1
\$\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.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

SmileBASIC, 19 bytes

@L
WAIT 12e7GOSUB@L

Waits 120 000 000 frames (around 21.2 days), and loops until the GOSUB stack overflows (after 16383 iterations)
This lasts 1038.32 years

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Implicit outer loop - this is a great idea! \$\endgroup\$ – anatolyg Dec 17 '19 at 11:13
1
\$\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
| improve this answer | |
\$\endgroup\$
1
\$\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.

| improve this answer | |
\$\endgroup\$
  • \$\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 Jan 6 at 9:53
1
\$\begingroup\$

TI-BASIC, 17 bytes

For(I,0,2^32:rand(564:End

TI-BASIC doesn't have any wait commands/functions, so I just used one of the slowest functions available: generating random lists!

Generating a random list of 564 elements takes \$\approx7.95\$ seconds to make and the loop goes through \$2^{32}\$ iterations, so that results in \$7.95*2^{32}=34144990003.2\$ seconds or \$1082.01255\$ years.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

JavaScript (V8), 35 26 bytes

setTimeout(alert(''),3e13)

Try it online!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Is the space after the comma needed? \$\endgroup\$ – Ad Hoc Garf Hunter Jan 19 at 19:13
  • \$\begingroup\$ setTimeout() can take the first argument as a string, which is evaluated when the timer expires. Since all the program needs to do is terminate or do something observable, we can just make this a 1 character string. setTimeout("a",3e13). However I'm not sure that setTimeout can actually delay for 3e13 milliseconds, as when I run it in my browser, it runs the code immediately. \$\endgroup\$ – mabel Jan 19 at 19:26
0
\$\begingroup\$

at, 19 bytes

at now + 1000 years
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ @JoKing I think it's linux command at \$\endgroup\$ – Pierre Cathé Dec 13 '19 at 13:03
  • 1
    \$\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 Dec 15 '19 at 5:59
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
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Bash and a fast spaceship, 7 bytes

sleep 1

Try it online!

This requires that the computer running the program (e.g. TIO) be

  1. Accelerated away from Earth for 250 years from the point of view of observers on Earth at about \$3.4\times10^{10}\frac{m}{s^2}\$ (observers on the spacecraft, if suitably restrained, will experience the acceleration for 0.25 seconds)
  2. Accelerated toward Earth for 500 years (observers on the spacecraft will experience the acceleration for 0.5 seconds) at about \$3.4\times10^{10}\frac{m}{s^2}\$
  3. Accelerated away from Earth for the final 250 years (or 0.25 seconds in the spacecraft frame) at \$3.4\times10^{10}\frac{m}{s^2}\$.

The required acceleration was estimated by setting the integral of the relativistic proper time equation to be \$T_s\$ (quarter of the parameter you want to pass to sleep()) and setting \$T_e\$ to 250 years.

\$\displaystyle T_s=\int_0^{T_e} \sqrt{1-\left(\frac{v(t)}{c}\right)^2} \ \mathrm{d}t\$

where

\$v(t)=\frac{at}{\sqrt{1+\left(\frac{at}{c}\right)^2}}\$

It turns out that

\$\displaystyle \int_0^T \sqrt{1-\left(\frac{at}{c\sqrt{1+\left(\frac{at}{c}\right)^2}}\right)^2} \ \mathrm{d}t=\frac{c}{a}\sinh^{-1}\left(\frac{a T}{c}\right)\$

Solving for a for \$T_e\$=250 years and \$T_s\$=.25 seconds yields a~3.4e10. Viola!

Relativistic Twin Paradox

Hyperbolic Motion (Relativity)

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Apart from all the other problems with this answer, doesn't that speed exceed the speed of light? \$\endgroup\$ – Jo King Dec 19 '19 at 6:06
  • \$\begingroup\$ @JoKing From the second equation, \$at\$ can be set arbitrarily large and \$v(t)\lt c\$ \$\endgroup\$ – ceilingcat Dec 19 '19 at 6:55
-3
\$\begingroup\$

[C#] A bit larger than 32 bytes

class Program
{
    static void Main(string[] args)
    {
        Sleep(1000);
    }

    public static void Sleep(int years)
    {
        BigInteger bigTemp = (BigInteger)years;
        BigInteger integ = bigTemp*365*24*60*60;
        int temp = 0;

        while (integ > int.MaxValue)
        {
            Thread.Sleep(int.MaxValue);
            integ -= int.MaxValue;
        }
        temp = (int)integ;
        Thread.Sleep(temp);
    }
}

I see no error there

| improve this answer | |
\$\endgroup\$
  • 8
    \$\begingroup\$ The winning criteria for this question is code-golf, so you should be attempting to reduce the length of program as much as you can. Your program has a lot of excess whitespace and long variable names, as well as some other redundant stuff. Can you also add your actual score to the header? \$\endgroup\$ – Jo King Dec 12 '19 at 7:45
  • 3
    \$\begingroup\$ You would need using System.Numerics; using System.Threading;, too. Thread.Sleep needs milliseconds, you give it seconds. Also, while it otherwise (probably; haven't tested it) does, what it should, it's not very "golfy". The comment \\Thread.Sleep(int.MaxValue); should be removed completely. Also, you could put everything on one line to save newlines/spaces. And save in other places, like using var instead of BigInteger where possible and also don't do int temp = 0;, just say var temp = (int)integ; -- oh yes, and the variable names are unnecessarily long. \$\endgroup\$ – Corak Dec 12 '19 at 7:50
  • \$\begingroup\$ Does it count if there's a dll for it that takes a bigint? \$\endgroup\$ – DudeWhoWantsToLearn Dec 12 '19 at 10:25
  • 3
    \$\begingroup\$ @DudeWhoWantsToLearn - that might fall under the forbidden loopholes; something in the spirit of "outsourcing the real answer". So probably only functionality, that is either "built-in" or that is in a "standard library" is allowed. \$\endgroup\$ – Corak Dec 12 '19 at 12:23
  • \$\begingroup\$ Hint: instead of using a BigInteger -= big number, divide your total number of seconds by int.MaxValue (manually, not in the source) so you only need i--. You can manually handle big numbers by using 2 int-sized numbers. \$\endgroup\$ – Peter Cordes Dec 13 '19 at 9:07
-3
\$\begingroup\$

JavaScript, 15 bytes

setTimeout(3e10)
| improve this answer | |
\$\endgroup\$
  • 4
    \$\begingroup\$ Although it might seem like it works, it shouldn't. You've provided the timeout to the callback parameter, and javascript will carry on execution of any code outside the timeout. You'd have to use a promise to make it really pause. \$\endgroup\$ – Kobe Dec 13 '19 at 9:41
  • \$\begingroup\$ That is a good point. Thanks for pointing that out \$\endgroup\$ – maestro.inc Dec 13 '19 at 9:43
1
2

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.