Sleep for 1000 years

82

13

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)

anatolyg

Posted 2019-12-11T18:15:43.127

Reputation: 10 719

Answers

22

MATL, 6 5 bytes

35WY.

This waits for 34359738368 seconds, which is a little more than 1089 years and a half.

Don't try it online!

Explanation

35     % Push 35
W      % 2 raised to that. Gives 34359738368
Y.     % Pause for that many seconds

Luis Mendo

Posted 2019-12-11T18:15:43.127

Reputation: 87 464

2I doubt anyone will find any way that beats this. Then again, I'm surprised to see it this short, so I could be surprised again. The only way I can think of is if another language can do basically the same thing but has a 1-character sleep function (as opposed to 2 ["Y."]). – Aaron – 2019-12-12T17:26:24.827

72

C (gcc), 40 36 32 29 26 24 bytes

i;f(){--i&&f(sleep(7));}

Try it online!

-3 thanks to @gastropner.

-1 recursive approach thanks to @AZTECCO.

4294967295*7/86400/365.25 ~ 952.69

ceilingcat

Posted 2019-12-11T18:15:43.127

Reputation: 5 503

2Can bump the sleep a hair more with a more accurate year: 365.242193… – WGroleau – 2019-12-12T03:11:13.970

12Witchcraft! Dark Arts!!! Why is it possible to call a function as it is being declared, and more precisely, how can you give a parameter to a parameterless function??? – M.Herzkamp – 2019-12-12T09:46:06.207

14@M.Herzkamp (1) because why wouldn't you be able to? (2) in C, a function defined with an empty parameter list (as opposed to a void parameter list) is treated as a varargs function. – Sneftel – 2019-12-12T10:08:41.417

3would be more interested why you think this runs 2^32 times - and not 2^31 times - as an i variable would be predefined as going from -2^31 to (2^31)-1 (signed) and I dont see an unsigned definition – eagle275 – 2019-12-12T12:42:39.577

3@eagle275 Because i goes from 0 to -1, then all the way to -(2^31), the next decrement takes i to (2^31)-1, then i goes all the way back to 0; – Noodle9 – 2019-12-12T13:34:13.810

6Is i somehow guaranteed to be initialized to 0? I still operate under the assumption that it should be treated as garbage if not initialized; then again, I frequently have to work with ancient legacy code that still uses old compilers. – Aaron – 2019-12-12T17:07:12.717

16

@Aaron Globals in C are initialized to 0

– ceilingcat – 2019-12-12T19:10:11.883

3Signed overflow is undefined, so the compiler is allowed to optimize the check out. – Erbureth says Reinstate Monica – 2019-12-13T12:58:29.403

@ErburethsaysReinstateMonica Good thing there are no optimisation flags used then. The default is optimization off: "generates unoptimized code".

– Noodle9 – 2019-12-13T17:42:46.083

2This answer is more appropriate on stackoverflow ;) – rrauenza – 2019-12-14T16:10:25.933

1I hope the compiler implements proper tail call elimination. Otherwise you will run into an out of memory error, unless your computer happens to have 45*8 TB heap memory. Ok, not that difficult if you are using virtual memory and swap it all out. – Roland Illig – 2019-12-14T18:36:01.393

I voted for this answer when it wasn't so great. Too bad I cannot vote again! – anatolyg – 2019-12-15T19:26:43.310

1

Wrong, unfortunately: this relies on something that C cannot guarantee (and in fact, guarantees to be false in many old systems and probably all future (>32bit) ones...) : that the size of int is 32bits. Read carefully the (old but GREAT) C faq : it's answer 1.1 for exemple: http://c-faq.com/decl/inttypes.html states carefully that int "... can hold values up to 32,767" because the only thing C says is than "int" is AT LEAST 16 bit (not 32, and can be 36, 18, and any number of bits that can hold 16 bits inside). Thus you end up sleeping very different amount of times depending on the system.

– Olivier Dulac – 2019-12-18T13:58:17.440

so maybe edit to change i to int32_t i=0 (as not sure int32_t is set to 0 by default). Needs a c99 (or more recent) compiler, and a machine that can have int on 32 bits (this depends on the system, and the compiler will then barf out if your system doesn't support int32_t) – Olivier Dulac – 2019-12-18T14:03:00.353

@OlivierDulac As long as it works for some compiler on some system, the answer is valid.

– Doorknob – 2019-12-20T15:57:47.330

36

Python 3 (Excluding CPython on Windows), 28 bytes

import time
time.sleep(3e10)

Try it online! (Remember to put something in your will so future generations can check that it ended on time.)

time.sleep(seconds) takes either an int or a float.

1000*365.2422*24*60*60/3e10 == 1.051897536 which is an error of less than 10%.

Noodle9

Posted 2019-12-11T18:15:43.127

Reputation: 2 776

13+1 for the remark about future generations (and also because it is a great answer). – ElPedro – 2019-12-11T21:34:59.453

4

This does not work in CPython on Windows. time.sleep of CPython only supports sleeping up to 2^32-1 ms = a little bit less than 50 days. (Limit imposed by WinAPI Sleep)

– JiminP – 2019-12-12T09:02:04.240

32@JiminP Just goes to show you: If you want to run something for a millennium - don't do it on Windows! T_T – Noodle9 – 2019-12-12T10:48:28.030

24

bash, 26 bytes

I think part of the challenge here is to not have a signed 32 bit overflow, so:

ping -i86400 -c365243 t.co

The idea here is to make 1000 years of pings (365243), once per day (86400).

"t.co" is simply a four character internet hostname (in this case, a link shortener). If your local host table has a one character hostname, you can subtract 3 bytes.

David G.

Posted 2019-12-11T18:15:43.127

Reputation: 541

10You have more faith in the longevity of IPv4 than I do. – Caleb – 2019-12-13T05:34:52.020

3@Caleb: This only has to start today, on a system that will stay up for 1000 years, not continue to do IPv4 DNS lookups. But yes, ping on my GNU/Linux system supports IPv6 addresses (without having to invoke it as ping6), and localhost is ::1 saving 1 byte. ping :: also works, for the IPv6 unspecified address, saving 2 bytes but getting error messages (ping: sendmsg: Network is unreachable). – Peter Cordes – 2019-12-13T08:51:08.097

Do you need to ping a working domain? Wouldn't this still work according to spec if all the pings failed by calling a fake domain like t? – Ferdz – 2019-12-13T15:18:57.997

3With my version of ping, ping 1 attempts to ping 0.0.0.1, which saves 3 characters if you can get that IP to ping. – Vortico – 2019-12-13T16:01:22.650

@Vortico It does not on my ubuntu 16.04 system and ping does not work on TIO so i wouldn't say this is valid. – Luc H – 2019-12-13T16:24:14.733

I don't like the IPv6 addresses as it requires a particularly recent Linux and IPv6 enabled. It wont work to simply ping t because the address must resolve, though it is not the case that there must be a host there. ping 1 should not work, and does not for me. I also don't want to assume a shorter alias in /etc/hosts, as not everyone would have that, but as noted, it can shorten the golf. As for the longevity of IPv4: it has already survived decades past it's replacement. :-) – David G. – 2019-12-13T20:09:36.580

20

Ruby, 10 bytes

sleep 3e10

Try it online!

There are approximately \$3*10^{10}\$ seconds in a thousand years.

79037662

Posted 2019-12-11T18:15:43.127

Reputation: 1 739

15sleep 32e9 is more accurate and has the same size in bytes – G B – 2019-12-13T09:13:12.133

31557600000 seconds – HelloWorld – 2019-12-13T12:47:28.477

10"Try it online!". LOL – Eric Duminil – 2019-12-13T18:21:49.577

18

PowerShell, 35 32 24 21 19 bytes

1..3e4|%{sleep 1mb}

Try it online!

Strangely, the maximum seconds value for Start-Sleep is 2147483. No, I don't know why it's such an odd value. That works out to a little over 2 megabytes (2097152). 1mb * 30000 / 86400 / 365.25 is 996.821051030497, so a little under 1000 years. I could get more exact using a different value than 3e4, but this is within the allowed margin and I'm lazy.

Saved 3 bytes thanks to ceilingcat.
Saved 8 bytes thanks to Jeff Zeitlin.
Saved 3 bytes thanks to Neil.
Saved 2 bytes thanks to Mark Henderson and Nahuel Fouilleul.

AdmBorkBork

Posted 2019-12-11T18:15:43.127

Reputation: 41 581

13

Well, 2147483 is 1/1000th of a max 32-bit integer, so perhaps Powershell internally does some unit conversions with milliseconds or something. See here (MaxIdleTimeoutSec)

– aphrid – 2019-12-11T19:04:09.487

Save 8 bytes with 1..15000|%{sleep 2.04mb}. – Jeff Zeitlin – 2019-12-11T19:09:54.507

@JeffZeitlin Good grief, I'm missing the obvious golfs today. Thanks! – AdmBorkBork – 2019-12-11T19:11:56.330

2Why not sleep 2mb? You'll get a little under 1000 years, but other than that it should be fine, I'd have thought? – Neil – 2019-12-11T21:08:26.570

@Neil Sure thing. Thanks! – AdmBorkBork – 2019-12-11T21:22:15.733

2Save yourself an extra byte with 1..15e3 – Mark Henderson – 2019-12-12T03:19:24.950

2or 1..3e4 and sleep 1mb – Nahuel Fouilleul – 2019-12-12T07:45:27.157

17

6502 Machine Code on an Apple II, 10 9 bytes

Code is actually platform-independent, other than it relies on the Apple's clock speed of 1.023 MHz for timing.

Code starts at address 0x0000:

0000: A2 CA F6 44 F0 FB D0 F8 F7

Saved an additional byte. Details are below original answer.

Original answer:

Code starts at address 0x0059:

0059: A2 08 F6 60 D0 FA CA D0 F9 F5

Disassembly:

loop1: 0059- A2 nn     LDX #$nn   ; 2 cyc
loop2: 005B- F6 60     INC $60,X  ; 6 cyc
       005D- D0 FA     BNE loop1  ; 2-3 cyc
       005F- CA        DEX        ; 2 cyc
       0060- D0 F9     BNE loop2  ; 2-3 cyc
       0062- pp        DB  $pp    ; data byte

This is a fairly simple routine that increments a multi-byte counter. When the counter rolls over, the last branch instruction gets modified so that it points to an RTS instruction, which provides the exit for the routine.

loop1 is taken for each increment of the counter and takes 11 cycles per iteration. loop2 is taken for each byte carried over and takes 13 cycles per iteration. So if we increment the counter N times, we spend approximately:

11*N + 13*(1/256 + 1/(256^2) + 1/(256^3) + ...)*N = 11.05*N cycles

1000 years is 365242*86400*1023000 = 32282717702400000 cycles

So we need N = 2921512914244345 +/- 10%

Or a range of 2629361622819911 - 3213664205668779
= 0x095763F583A447 - 0x0B6ACF816801AB

In the code above, set pp = 0xF5 and nn = 0x08.

This gives us a 7-byte counter in memory locations 0x62-0x68 (with MSB at lowest address, i.e. big endian). Only location 0x62 is initialized, so our starting counter value could be anywhere from 0xF5000000000000 to 0xF5FFFFFFFFFFFF.

We'll increment the counter until it rolls over to 0, which will cause the byte at 0x61 to increment by 1, which happens to be the branch target for loop2. On the first byte carry after rollover-- when the counter hits 0x100-- we'll hit the modified branch instruction for the first time. This will take us to address 0x5C (loop2+1). The 0x60 byte there is the opcode for "Return from Subroutine" (RTS) which provides our exit.

So our total loop count is between, 0x0A000000000101 and 0x0B000000000100, which is a subset of the range we calculated which gives us the necessary number of cycles +/- 10%.

Now that we have the exact starting and ending counter values, we could go back and calculate the exact cycle counts, but given how much margin there is, I'm willing to hand-wave that part.

You can actually test it out with smaller values of nn. For example nn of 4 will pause for several seconds.

9-byte answer:

0000- A2 CA     LDX #$CA   ; 2 cyc
0002- F6 44     INC $44,X  ; 6 cyc
0004- F0 FB     BEQ $0001  ; 2-3 cyc (rollover)
0006- D0 F8     BNE $0000  ; 2-3 cyc (non-rollover)
0008- F7        DB  $F7    ; data byte

Saved one byte by folding the DEX opcode into the argument for LDX.

Instead of 11.05 cycles, each counter increment is now 13 + 11*(1/256 + 1/(256^2) + ...) = 13.043 cycles.

Now we need 2475073064976549 +/- 10% iterations, or a range of 0x7E9F591BF7A2E - 0x9AC2C23EA071C.

So now we initialize the 7-byte counter to something between 0xF7000000000000 and 0xF7FFFFFFFFFFFF. This ends up giving between 0x08000000000001 and 0x09000000000000 loop iterations, which is within the +/-10% needed.

When the counter rolls over to 0, the last branch gets modified to jump to $0001, which leads to an increment of the BNE opcode itself (to a CMP instruction, which for our purpose is effectively a no-op). Code then falls through to $0008, which now contains a 0 (because of counter rollover). A 0 byte is a BRK instruction, which drops you back to the system monitor, ending the routine.

gone_native

Posted 2019-12-11T18:15:43.127

Reputation: 1 753

15

05AB1E, 6 bytes

27;°.W

Inspired by the other answer in 05AB1E.

Waits for 1027/2 milliseconds, or about 1002 years.

Explanation:

27            push the number 27
  ;           divide by 2
   °          replace X by 10 to the power of X
    .W        wait that number of milliseconds

Try it online!

anatolyg

Posted 2019-12-11T18:15:43.127

Reputation: 10 719

Can you explain this a bit more? How does that translate to 10^(27/2)? – Aaron – 2019-12-12T17:19:41.080

Nice approach! I actually just found another 6-byter myself as well. :) – Kevin Cruijssen – 2019-12-13T12:53:46.113

11

Jelly, 8 bytes

32œS$ȷ9¡

Try it online!

A niladic link which waits for 32 seconds 1 billion times. 32,000,000,000 is within 10% of 31,557,600,000 seconds which is 1,000 years (ignoring leap seconds and ignoring the fact that centuries indivisible by 400 are not leap years).

Nick Kennedy

Posted 2019-12-11T18:15:43.127

Reputation: 11 829

11

Charcoal, 7 bytes

F³³RXφ⁴

Try it online! Link is to verbose version of code with a speed up factor of 1e9 (obtained by changing the ⁴ into a a ¹) so that it doesn't time on TIO. Explanation:

F³³

Repeat 33 times.

RXφ⁴

Refresh the screen, but delay 1000⁴ milliseconds between refreshes.

Although there are 33 refreshes, there are only 32 intervals, so the total delay is 32000000000000 milliseconds or approximately 1015 years.

Neil

Posted 2019-12-11T18:15:43.127

Reputation: 95 035

11I love the implications of “Try it online!” It was the year 12019, and the ancient computer finally woke up. Amazed, the engineers used the methods of long-dead programmers to find what was running. The computer said, Charcoal. The gods had spoken, and in that moment, programming became an important force in the world once again. – BalinKingOfMoria Reinstate CMs – 2019-12-12T04:40:30.373

13@BalinKingOfMoria 1000 years after now is 3019, not 12019 ;) – Omega Krypton – 2019-12-12T05:16:20.000

3@OmegaKrypton The code sleeps for almost 1015 years anyway, so it would wake up some time during 3034. – Neil – 2019-12-12T09:23:17.747

@OmegaKrypton Oooops, I misremembered it as 10,000 years facepalm – BalinKingOfMoria Reinstate CMs – 2019-12-12T17:12:31.577

1I can't stand cryptic or archaic languages and would prefer everyone forget about them and use ones that are more readable (maybe Charcoal is and this is just an (ab)use of it?), but this still gets +1 for the effort even though it makes me cringe. – Aaron – 2019-12-12T17:14:30.950

can this really be expressed in 7 bytes? Those are 7 characters but in UTF-8 they would be more bytes – Agos – 2019-12-13T11:11:36.077

@Agos Charcoal has its own code page (see the wiki in the linked github) although it will accept UTF-8 as well (there's a command-line flag for it). – Neil – 2019-12-13T11:51:16.603

clever! then the above translates to these 7 bytes: 0x46 B3 B3 B3 52 58 E6 B4 – Agos – 2019-12-13T13:55:01.533

@Agos Nearly; they're actually 0xC6 B3 B3 B3 D2 D8 E6 B4. – Neil – 2019-12-13T15:07:19.707

11

Java (JDK), 23 bytes

v->Thread.sleep(7L<<42)

Try it online!

It's hard to find a short way to write a number in Java. Thread.sleep only accepts a long number of milliseconds. So the standard answer 3e10 doesn't work because it's a double. Casting it to a long would be the appropriate action. But it would still be 1000 too small. So enter 3e13 which is closer. But fortunately, 7L<<42 is 30,786,325,577,728, which is close to the actual count of 1000 years, and is a long without cast, so 4 bytes shorter than (long)3e13.

Credits

  • -4 bytes thanks to Neil by replacing (long)3e13 with 7L<<42.

Olivier Grégoire

Posted 2019-12-11T18:15:43.127

Reputation: 10 647

@XtremeBaumer No: 3*10^13 = 19. Surely we want to sleep for more than 19 milliseconds. The ^ operator is the binary xor. There is no power operator in Java. Only Math.pow which also returns a double and therefore requires a cast, increasing the total byte count, not decreasing it. – Olivier Grégoire – 2019-12-12T13:42:47.700

Does this count here without alle the main class and the try catch for the interruption exception? – avalancha – 2019-12-12T14:10:58.473

2

@avalancha Consensus is that lambdas are fine. You don't need to try/catch in lambdas since it relies on declaring interfaces that do all that for you. That's why I declared the Sleeper interface in my TIO. We're using several distinct rulings to get this far in golfing in Java.

– Olivier Grégoire – 2019-12-12T14:17:06.267

awesome, thx for the link! – avalancha – 2019-12-12T14:52:06.247

5Can you use 7L<<42? It's about 3e13, I think. – Neil – 2019-12-13T00:41:51.193

8

Batch, 41 25 bytes

-8 bytes thanks to AdmBorkBork
-6 bytes thanks to ceilingcat
-2 bytes thanks to inspiration from Neil

ping 1 -n 31556952000>nul

Using 31,556,952 seconds / year and a (default) 1 second delay between each ping, this will wait 1000 years before returning nothing.

Note that ping 1 results in failure but it'll fail 31 billion times so that still works.

Engineer Toast

Posted 2019-12-11T18:15:43.127

Reputation: 5 769

2How about 1.1.1.1 to save two bytes? Sure, it's not a valid "test" address (sorry, Cloudflare), but hey, it's code-golf. ;-) – AdmBorkBork – 2019-12-11T20:19:53.687

1Actually, more than that, because you can therefore do -n 31556952000000>nul and skip the -w, since there's a one second delay between each ping. – AdmBorkBork – 2019-12-11T20:33:10.983

1Why not 1.0.0.1, which abbreviates to 1.1? – Neil – 2019-12-11T21:09:49.113

8

APL (Dyalog Unicode), 9 8 bytesSBCS

-1 thanks to Eric Towers.

Full program. This works by observing that $$\sum_{n=1}^{8^6}n=3.44×10^{10}\approx3.16×10^{10}$$which is the number of seconds in a thousand years.

⎕dl¨⍳8*6

Don't try it online!

8*6\$8^6=262144\$

ɩntegers until that

⎕dl¨delay each of those many seconds

Adám

Posted 2019-12-11T18:15:43.127

Reputation: 37 779

I don't know APL, but is ⎕dl¨3e10 good for 8 bytes? – 79037662 – 2019-12-11T23:31:53.183

@79037662 ¨ is "each" which wouldn't be needed in this case, but no, 3e10 is for some reason too big an argument for ⎕dl which appears to have a limit of 2147483 seconds. – Adám – 2019-12-11T23:37:09.600

8*6 = 262144, giving 1088.8... years. – Eric Towers – 2019-12-13T04:07:52.583

@EricTowers Incorporated. Thanks! – Adám – 2019-12-13T08:13:47.727

7

Wolfram Language (Mathematica), 11 bytes

Pause[2^35]

2 to the 35th power is about 8.9% greater than 31,556,926,000.

Greg Martin

Posted 2019-12-11T18:15:43.127

Reputation: 13 940

I’m not at my computer right now to check, but can you save a byte with Pause@2^35 ? – Michael Stern – 2019-12-15T00:15:53.637

Tried that—it pauses for 2 seconds and returns Null^35 :D – Greg Martin – 2019-12-15T08:23:27.003

6

T-SQL, 56 bytes

DECLARE @ INT=0a:WAITFOR TIME'9:0'SET @+=1IF @<4E5GOTO a
  • The SQL WAITFOR command is limited to 24 hours, so it has to go in a loop.
  • WAITFOR has two options: DELAY that waits a certain length of time, or TIME which waits until a certain time of day. Turns out WAITFOR TIME'9:0' (which will pause until 9am the following day) is a couple of bytes shorter than WAITFOR DELAY'24:0'.
  • Using a GOTO loop is shorter than a WHILE loop.
  • I'm looping 4E5 times (400,000), which is within 10% of the 10-year (365,242 day) goal.
  • If I start it today, this would complete in February, 3115

BradC

Posted 2019-12-11T18:15:43.127

Reputation: 6 099

6

Lua, 38 bytes

t=os.time;o=t()+2^35while t()<o do end

Try it online!

Being ANSI C compliant, Lua does not implement any sleep functions, so we have to make one ourselves. Oh, and make sure to keep the dust off that One Thousand Year Computer, we're busy waiting.

Lua, 38 bytes

t=os.time;o=t()+2^35repeat until o<t()

Try it online!

Same thing, different loop. Tried to see if I can save some bytes, but nope.

val says Reinstate Monica

Posted 2019-12-11T18:15:43.127

Reputation: 409

4What the dingle bells, why does 35while not throw the parser into conniptions? Today I learned something about Lua I didn't want to know. – Caleb – 2019-12-13T05:41:03.500

@Caleb In upcoming Lua 5.4, this will no longer works. They have learnt this lesson. Also, tio.run really should provide different Lua versions due to significant changes (this one will cause a lot of 5.3 golf break if they'll just update). – val says Reinstate Monica – 2019-12-13T11:04:11.213

5

C# (Visual C# Interactive Compiler), 59 bytes

b=>{for(var d=DateTime.Now.AddYears(999);d>DateTime.Now;);}

Can be tested by replacing AddYears() with AddSeconds()

Try it online!

Innat3

Posted 2019-12-11T18:15:43.127

Reputation: 791

Couldn't this be shortened by eliminating 'd' and making the 'for' a 'while'? – bta – 2019-12-14T02:06:01.897

@bta you mean while (DateTime.Now.AddYears(999) > DateTime.Now); ? but that would result in an endless loop, as if I don't initially store DateTime.Now in a variable it will keep updating it`s value. – Innat3 – 2019-12-14T12:18:17.930

5

R, 24 bytes

sapply(1:25e4,Sys.sleep)

Try it online!

A solid starter, feels like there's optimisation to be had with a different function & number combo. This sleeps for 1s, then 2s, then 3s, then 4s... up to 250000s, at which point it's been running for about 990 years and outputs a list of 250k empty elements.

CriminallyVulgar

Posted 2019-12-11T18:15:43.127

Reputation: 501

1why not 3e10 like the other answers – qwr – 2019-12-12T22:09:17.037

@qwr Sys.sleep(3e10) doesn't work, and I couldn't figure out a shorter way to write some version of for(i in 1:3e10)Sys.sleep(1) that works. If there is one then more fool me! – CriminallyVulgar – 2019-12-13T09:52:08.007

@qwr it occurred that there's a less obvious reason for this being the shortest one I found: 25e4 is the same length as 3e10, but sapply allows us to pass the counter into the function without parentheses making it valuable to find a number with sums along to roughly 3e10. The 1:25e4 is doing double duty here. – CriminallyVulgar – 2019-12-13T10:42:39.993

why doesn't Sys.sleep(3e10) work? – qwr – 2019-12-13T18:49:32.363

I'm not 100% on the why of it, but the documentation says on Windows Sys.sleep uses Sleep. I'm not knowledgeable of the specifics there, but the outcome is that it sleeps for a few milliseconds rather than a thousand years. Might behave differently on Linux? – CriminallyVulgar – 2019-12-19T12:18:18.887

4

Pyth, 10 bytes

VT.dC"¼€

Try it online!

There are exactly 31,557,600 seconds in the Julian astronomical year., totalling 31,557,600,000 seconds in 1000 years. 2^32 is about a tenth of this, so we just wait for 3,155,760,000 seconds ten times. Note that € is a blank codepoint in the TIO, not sure why it translates to this on SE

Here you can see that C"¼€ is equal to 3,155,760,000

And here is an example that waits for only 22 seconds using a similar method

Pyth, 8 bytes

.dC"XúÃ

Try it online!

Alternatively, this one just uses C"XúÃ for 31,557,600,000. I thought it was more in line with the spirit of the challenge to have a 2^32 limit, though


The average length of a sidereal year, however, is 365.256363004 days or around 31,558,149.763 seconds, giving us a total of ~31,558,149,764 seconds in 1000 years. In this case, the more accurate solution would be

This: VT.dC"¼ê@

This:.dC"Y&ƒ(1 byte longer than Julian solution)

frank

Posted 2019-12-11T18:15:43.127

Reputation: 941

4

Perl 5, 18 bytes

sleep 1e9for 1..30

Try it online!

10 bytes (doesn't work)

sleep 3e10

Try it online!

Nahuel Fouilleul

Posted 2019-12-11T18:15:43.127

Reputation: 5 582

Polyglot with Perl 6/Raku – Jo King – 2019-12-12T07:46:43.480

actually it doesn't work : sleep() with negative argument – Nahuel Fouilleul – 2019-12-13T06:40:10.430

4

PHP, 30 bytes

The time() function can be used instead of microtime(1), as when plused with 3e10, PHP converts the value into a float, even when the original value is an int and thereby save some more bytes.

time_sleep_until(time()+3e10);

See how php converts values


As stated by "manassehkatz-Reinstate Monica" a flag can be set to make it run without the tags in PHP, so a more clean version is here made. (36 bytes)

time_sleep_until(microtime(1)+3e10);



Google tells me that 1000 years = 3.1556926 × 1010 seconds (31,556,926,000)
The method cloud even be made recoverable in the event of power failures. (not done in this example)

<?php time_sleep_until(microtime(true)+31556926000)); ?>


The shorter but more imprecise version (45 bytes with the php tags)

<?php time_sleep_until(microtime(1)+3e10); ?>


With a correction from "Kaddath" this option below is dropped as the documentation state that it is a int not a float used in sleep()
It can be done like so:

<?php sleep(31556926000); ?>

Try it online!

Mikki Sørensen

Posted 2019-12-11T18:15:43.127

Reputation: 41

2I love how you've added a "Try it online!" link :') – Martijn – 2019-12-12T09:35:37.613

I think it would work with 1+"3e10" which saves 3 bytes ;) (automatic conversion to float and actually 31556926000 is already a float, not an int) – Kaddath – 2019-12-12T10:02:06.803

Well after having tested, my previous comment's suggestion doesn't work, but that means yours will not either: sleep converts argument to an int, so the value used will be 1492154928 when you use 31556926000, which is "only" 47.3 years (hard to verify, I know, must trust the documentation on this).. Sorry! – Kaddath – 2019-12-12T10:27:47.923

However, the second code with time_sleep_until will work, because it uses a float, and there you actually can use microtime(true)+"3e10".. EDIT: even better: microtime(1)+"3e10" – Kaddath – 2019-12-12T10:32:21.910

I updated my response based on your feedback Kaddath, but we do not need "" around 3e10, so we can save 2 bytes more :) – Mikki Sørensen – 2019-12-12T12:14:32.533

1Congrats Mikki! Thanks for representing PHPers! PS: do you really need the closing tag ?>? ;D – Victor F – 2019-12-12T15:15:13.220

You can remove ?> And I know there is a flag to make it run without <?php that is routinely used in Code Golf. – manassehkatz-Moving 2 Codidact – 2019-12-12T20:12:27.027

Victor F and manassehkatz-Reinstate Monica you are right, i think i will switch my score and make a note :) As you might have spotted im new to Code Golf, so thanks for the tips :) – Mikki Sørensen – 2019-12-13T08:33:24.000

Just as a note, here’s the related meta post, explaining why and in which cases the opening tag can be discarded: https://codegolf.meta.stackexchange.com/q/7098/41859

– insertusernamehere – 2019-12-24T08:35:14.473

4

Mumps (M): 7 Bytes

H 9**11

Mumps likes single-letter commands & big numbers... H(ang) sleeps for ## of seconds; 9 to the 11th power gives 31381059609 seconds, which is almost 0.6% low for 1000 years; well within the margin of error.

Keeping with the 7 byte code length, I tested "power of 9" sleep times on 2 different Mumps implementations; the largest power of 9 on YottaDB/GT.M I can find without numeric overflow would be:

H 9**49

which gives a sleep time of just over 1,814 decillion millennia. InterSystems' Cache doesn't overflow and can go to:

H 9**99

which gives 9.352x1083 millennia. That's quite a nap in 7 bytes!

zmerch

Posted 2019-12-11T18:15:43.127

Reputation: 541

4

JavaScript (Node.js), 70, 67, 34 bytes

-3 thanks to Joost K, see their answer for why I used 8e6
-33 thanks to Benjamin Gruenbaum for pointing out callbacks are better, using the concept of his answer

I've found 2 ways of solving this problem. The first defines a globally:

(f=_=>a&&setTimeout(f,--a))(a=8e6)

the other method passes a through each time, using setTimeout's further args:

(f=a=>a&&setTimeout(f,--a,a))(8e6)

These solutions remain the same length when using IIFEs or just calling the function manually.

Kobe

Posted 2019-12-11T18:15:43.127

Reputation: 189

You can remove the space to save another byte :) – Kevin Cruijssen – 2019-12-13T13:04:08.033

you can cut down 3 bytes by changing the loop like t=77e5;--t; and using t as the time to wait.setTimeout(r,t) See my C# answer where I use this in C# – Joost K – 2019-12-13T13:39:10.457

1@JoostK Clever, I'll add that in now, thanks :) – Kobe – 2019-12-13T14:09:49.603

2Same concept but with callbacks instead of promises :] (function f(a){setTimeout(f,--a)})(8e6); at 40 characters – Benjamin Gruenbaum – 2019-12-13T19:13:09.240

@BenjaminGruenbaum Wouldn't this carry on forever? and isn't a undefined? – Kobe – 2019-12-17T09:55:24.267

@Kobe no - We are defining and immediately executing a function and passing 8e6 as a parameter. – Benjamin Gruenbaum – 2019-12-17T11:45:04.753

I mean it will recurse forever, since there is not condition stopping the setTimeout from calling f again. And a will not be passed through on any of the iterations, you'd have to use _=>f(a) right? @BenjaminGruenbaum – Kobe – 2019-12-17T13:33:11.040

Oh yeah you're right it's missing the a&& part – Benjamin Gruenbaum – 2019-12-17T16:02:05.733

3

Applescript, 10

delay 3e10

Sleeps for 3 * 1010 seconds.

Digital Trauma

Posted 2019-12-11T18:15:43.127

Reputation: 64 644

3

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.

Kevin Cruijssen

Posted 2019-12-11T18:15:43.127

Reputation: 67 575

15 bytes: ₆9e.W (waits ~8% too long). – Grimmy – 2019-12-13T13:50:08.660

@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. – Kevin Cruijssen – 2019-12-13T13:52:11.400

3

05AB1E, 5 bytes

₆9e.W

Sleeps for about 1082 years.

Try it online!

₆         # push 36
 9        # push 9
  e       # nPr (number of 9-element permutations of a 36-element list)
   .W     # wait that many milliseconds

Grimmy

Posted 2019-12-11T18:15:43.127

Reputation: 12 521

2

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?

user3490

Posted 2019-12-11T18:15:43.127

Reputation: 809

2

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

Joost K

Posted 2019-12-11T18:15:43.127

Reputation: 210

2

C# (Visual C# Interactive Compiler), 38 37 bytes

this waits 1 + 2 + 3.. +77e5-1 + 77e5ms.

I got this 77e5 magic number using the formula (x+1)/2*x=3e13 where x results in approximately 7.745.967 which I shortened to 77e5.

To check if it still falls in the allowed range I did 1000*365.25*24*3600*1000/((77e5+1)/2*77e5) which is 1.064 aka 6.45% of target

update : 8e6 results in 0.98 more precise and less bytes.

for(var i=0;i++<8e6;)Thread.Sleep(i);

Try it online!

Joost K

Posted 2019-12-11T18:15:43.127

Reputation: 210

2

Roblox, 11 bytes

wait(3e+10)

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

Corsaka

Posted 2019-12-11T18:15:43.127

Reputation: 581

1

Red, 9 bytes

wait 3e10

Try it online!

Galen Ivanov

Posted 2019-12-11T18:15:43.127

Reputation: 13 815

1

Tcl, 23 bytes

after 30000000000000000

Try it online!

game0ver

Posted 2019-12-11T18:15:43.127

Reputation: 621

1

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.

Konamiman

Posted 2019-12-11T18:15:43.127

Reputation: 291

1

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

smac89

Posted 2019-12-11T18:15:43.127

Reputation: 121

sleep 3e10, sleep 8e6h, sleep 5e8m are all 10 bytes and within the 10% tolerance. – Grimmy – 2019-12-16T12:51:42.703

@Grimmy Thank you! – smac89 – 2019-12-16T18:38:33.517

1

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.

Nacre

Posted 2019-12-11T18:15:43.127

Reputation: 81

1

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

12Me21

Posted 2019-12-11T18:15:43.127

Reputation: 6 110

Implicit outer loop - this is a great idea! – anatolyg – 2019-12-17T11:13:24.520

1

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

Kevin Cruijssen

Posted 2019-12-11T18:15:43.127

Reputation: 67 575

1

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.

numbertheory

Posted 2019-12-11T18:15:43.127

Reputation: 11

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. – manatwork – 2020-01-06T09:53:23.433

1

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.

Tau

Posted 2019-12-11T18:15:43.127

Reputation: 1 935

1

JavaScript (V8), 35 26 bytes

setTimeout(alert(''),3e13)

Try it online!

kel5isgod

Posted 2019-12-11T18:15:43.127

Reputation: 11

1Is the space after the comma needed? – Post Rock Garf Hunter – 2020-01-19T19:13:46.543

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. – mabel – 2020-01-19T19:26:19.303

0

at, 19 bytes

at now + 1000 years

ceving

Posted 2019-12-11T18:15:43.127

Reputation: 101

@JoKing I think it's linux command at

– Pierre Cathé – 2019-12-13T13:03:44.097

1You 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. – Caleb – 2019-12-15T05:59:45.877

0

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

qarz

Posted 2019-12-11T18:15:43.127

Reputation: 1

-1

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)

ceilingcat

Posted 2019-12-11T18:15:43.127

Reputation: 5 503

Apart from all the other problems with this answer, doesn't that speed exceed the speed of light? – Jo King – 2019-12-19T06:06:30.637

@JoKing From the second equation, $at$ can be set arbitrarily large and $v(t)\lt c$ – ceilingcat – 2019-12-19T06:55:21.727

-3

[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

DudeWhoWantsToLearn

Posted 2019-12-11T18:15:43.127

Reputation: 163

8The winning criteria for this question is [tag: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? – Jo King – 2019-12-12T07:45:52.523

3You 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. – Corak – 2019-12-12T07:50:10.453

Does it count if there's a dll for it that takes a bigint? – DudeWhoWantsToLearn – 2019-12-12T10:25:44.053

3

@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.

– Corak – 2019-12-12T12:23:22.590

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. – Peter Cordes – 2019-12-13T09:07:16.173

-3

JavaScript, 15 bytes

setTimeout(3e10)

maestro.inc

Posted 2019-12-11T18:15:43.127

Reputation: 101

4Although 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. – Kobe – 2019-12-13T09:41:16.113

That is a good point. Thanks for pointing that out – maestro.inc – 2019-12-13T09:43:34.903