If a program terminates and there is no one to see it, does it halt?

100

23

It's time to face the truth: We will not be here forever, but at least we can write a program that will outlive the human race even if it struggles till the end of time.

Your task is to write a program that has a expected running time greater than the remaining time till the end of the universe.

You may assume that:

  • The universe will die from entropy in 101000 years.
  • Your computer:
    • Will outlive the universe, because it is made of Unobtainium.
    • Has infinite memory/stack/recursion limit.
    • Its processor has limited speed.

You must show that your program terminates (sorry, no infinite loops) and calculate its expected running time.

The standard loopholes apply.

This is a code golf challenge, so the shortest code satisfying the criteria wins.

EDIT:

Unfortunately, it was found (30min later) that the improbability field of Unobtainium interferes with the internal clock of the computer, rendering it useless. So time based programs halt immediately. (Who would leave a program that just waits as its living legacy, anyway?).

The computer processor is similar to the Intel i7-4578U, so one way to measure the running time is to run your program in a similar computer with a smaller input (I hope) and extrapolate its running time.


Podium

#CharsLanguageUpvotes        Author        
1    5      CJam              20       Dennis                  
2    5      J                      5         algorithmshark      
3    7      GolfScript       30       Peter Taylor          
4    9     Python             39       xnor                      
5    10   Matlab             5         SchighSchagh      

* Upvotes on 31/08

kb_sou

Posted 2014-08-24T22:02:52.477

Reputation: 985

You should post a question with the same title on Philosophy.SE, and maybe on one of the 50 programming sites.

– Cyoce – 2016-01-14T16:18:18.797

I am voting to close this as too broad because we require objective validity criteria for all challenges. As it stands, there is no clear way to determine the validity of a submission. Additionally, there is no specification of the behavior of a valid submission. – Mego – 2016-03-31T07:16:16.593

Will have a play with this a go on Turing Machine code. Meanwhile, gold badge vote. – ouflak – 2019-10-27T09:15:13.960

40I was tempted to create a [slowest-code] tag for this question. :P – Doorknob – 2014-08-24T22:08:20.740

5A Bogosort wouldn't work because while it's infinitely improbable that it would never finish, it may require an infinite amount of time to finish. There are, however many awful NFA-based regular expressions that could satisfy the "will finish, but not before the universe is dead" criteria. – DavidO – 2014-08-24T22:16:22.380

2Is this actually different from a challenge to make large numbers? – xnor – 2014-08-25T02:21:23.957

49Your title should be a tshirt – user-2147482637 – 2014-08-25T02:54:03.403

4Nice question, but shouldn't it be popularity contest ? – IazertyuiopI – 2014-08-25T06:38:31.770

just out of curiosity, how long would it take to generate a duplicate GUID ? – Thousand – 2014-08-25T08:56:19.977

3

@IazertyuiopI, no.

– Peter Taylor – 2014-08-25T13:01:21.000

1

Been done :-) by John Cage and his ASAP organ piece http://en.wikipedia.org/wiki/As_Slow_as_Possible

– Carl Witthoft – 2014-08-25T13:17:17.733

The in-character edit is what took this over the top for me, giving t it a +1. – trlkly – 2014-08-25T13:29:15.780

12

I think Isaac Asimov wrote a story about this.

– David Conrad – 2014-08-25T17:02:02.317

2@IazertyuiopI Why? [tag:code-golf] seems like the obvious scoring metric for this. – Calvin's Hobbies – 2014-08-25T20:41:09.423

1@Calvin'sHobbies I was afraid it would encourage people to submit similar short answers (only counting to/evaluating a large number) at the expense of creativity , but since the long answers get upvoted, I guess there is nothing to worry about =P – IazertyuiopI – 2014-08-26T05:31:09.450

1Infinite memory isn't necessary. The processor can access only a finite amount of memory in the finite amount of time. 10^5030 bits sounds like a reasonable upper bound. – MSalters – 2014-08-26T14:51:17.057

@Doorknob: The tag you were looking for is [tag:busy-beaver]. I added it. – Nate Eldredge – 2014-08-27T16:00:56.417

This challenge reminded me of the minecraft 'Universe Death Clock' https://www.youtube.com/watch?v=F1CddzgVW14

– RoboKozo – 2014-08-27T22:50:37.153

3Am I the only one who thought of the Ackermann function instantly when I read the description? – PsHegger – 2014-08-28T09:29:11.683

After the death of the universe no one can see you halt. – marczellm – 2014-08-28T11:33:12.613

@PsHegger me too! – shortstheory – 2014-09-01T10:05:39.887

I improved my answer to 8 chars, I'm in 4th place now. :) – Nicu Stiurca – 2014-09-03T13:33:25.760

Answers

35

CJam, 5 bytes

0{)}h

How it works

 0   " Push 0.                                 ";
 {   "                                         ";
   ) " Increment the Big Integer on the stack. ";
 }h  " Repeat if the value is non-zero.        ";

This program will halt when the heap cannot store the Big Integer anymore, which won't happen anytime soon on a modern desktop computer.

The default heap size is 4,179,623,936 bytes on my computer (Java 8 on Fedora). It can be increased to an arbitrary value with -Xmx, so the only real limit is the available main memory.

Time of Death

Assuming that the interpreter needs x bits of memory to store a non-negative Big Integer less than 2x, we have to count up to 28 × 4,179,623,936 = 233,436,991,488. With one increment per clock cycle and my Core i7-3770 (3.9 GHz with turbo), this will take 233,436,991,488 ÷ 3,400,000,000 > 1010,065,537,393 seconds, which is over 1010,065,537,385 years.

Dennis

Posted 2014-08-24T22:02:52.477

Reputation: 196 637

14I don't think you can rely on finite resources, as the question states "Your computer has infinite memory/stack/recursion limit". – Greg Hewgill – 2014-08-25T01:10:34.047

4Infinite memory != infinite data types. If I have a terabyte of RAM, an unsigned 8-bit integer still only goes up to 255. – wchargin – 2014-08-25T01:13:30.643

6@GregHewgill: With unlimited resources, you can increase the maximum Java heap size to any arbitrary value, but it will always be finite. – Dennis – 2014-08-25T01:46:03.440

2@Dennis, but then just add a line every time thru the loop to double the heap size. It's a funny thing about infinities :-) – Carl Witthoft – 2014-08-25T13:19:45.147

9@CarlWitthoft: You can't do that from inside the program. – Dennis – 2014-08-25T13:33:29.180

63

JavaScript, 39

(function f(x){for(;x!=++x;)f(x+1)})(0)

Explanation

Since JavaScript does not precisely represent large integers, the loop for(;x!=++x;) terminates once x hits 9007199254740992.

The body of the for loop will be executed Fib(9007199254740992) - 1 times, where Fib(n) is the nth fibonacci number.

From testing, I know my computer will do less than 150,000 iterations per second. In reality, it would run much slower because the stack would grow very large.

Thus, the program will take at least (Fib(9007199254740992) - 1) / 150000 seconds to run. I have not been able to calculate Fib(9007199254740992) because it is so large, but I know that it is much larger than 101000 * 150,000.

EDIT: As noted in the comments, Fib(9007199254740992) is approximately 4.4092*101882393317509686, which is indeed large enough.

Peter Olson

Posted 2014-08-24T22:02:52.477

Reputation: 7 412

9Since fib(n) can be approximated by phi^n, we can use log((sqrt(5) + 1)/2)*9007199254740992 to calculate how many digits fib(9007199254740992) turns out it's about 1.8823933*10^15. – overactor – 2014-08-25T11:26:47.980

11

@overactor, According to Wolfram Alpha, Fib(9007199254740992) (using continuous form with phi) is approximately 4.4092... * 10^1882393317509686. Calculation

– Brian S – 2014-08-25T22:26:02.937

1growing stack does not reduce CPU speed... unless you take the limited memory address line width / unlimited address width into account (in which case the slowdown is still linear in address length assuming reasonable encoding) or even the physical limitations in memory storage and the speed of light (in which case the slowdown is cbrtic in address value assuming spatial storage; even DNA levels of data density eventually start adding up, even if you manage space-efficient random access) – John Dvorak – 2014-08-29T08:12:16.377

Due to the way stack is implemented, you cannot have both infinite stack and infinite heap. Even if your RAM is infinite (unless having the "top of the stack" starting at @infinite -- and assuming infinite RAM is from 0 to infinite, not from -infinite to +infinite). At some point both will collide. Unless JavaScript allocates its stack frames on the heap and does not use stack at all on recursion? – Sylvain Leroux – 2014-08-30T08:49:10.253

Why the for loop? wouldn't it be the same as function f(x){if(x!=++x)f(x)}(0) ? – James Khoury – 2014-09-01T04:06:58.993

1@JamesKhoury No, the function you just wrote is equivalent to for(x=0;x!=++x;) and only iterates 9007199254740992 times. – Peter Olson – 2014-09-01T15:34:18.433

4@SylvainLeroux an architecture with infinite amounts of RAM would probably just interleave the heap and stack and have them both grow upwards. – John Dvorak – 2014-09-02T05:01:09.397

47

Python (9)

9**9**1e9

This has more than 10**10000000 bits, so computing it should take us far past heat death.

I checked that this takes more and more time for larger but still reasonable values, so it's not just being optimized out by the interpreter.

Edit: Golfed two chars by removing parens thanks to @user2357112. TIL that Python treats successive exponents as a power tower.

xnor

Posted 2014-08-24T22:02:52.477

Reputation: 115 687

4Even 9**9e9 should be enough, shouldn't it? – Level River St – 2016-01-14T18:04:05.090

4OverflowError: (34, 'Result too large') – apple16 – 2014-08-25T04:30:38.550

93@apple16 Maybe on your computer, but mine has an "infinite memory/stack/recursion limit". – xnor – 2014-08-25T04:46:42.927

3Even if you had infinite memory, the i7 only supports, at best, 48 bits of address space. :) – user1354557 – 2014-08-25T21:58:10.617

64

It's OK, guys. I ran it last universe and got ...82528057365719799011536835265979955007740933949599830498796942400000000009 (2.6*10^954242509 digits omitted to avoid black hole collapse). You should really upgrade to Unobtanium.

– xnor – 2014-08-25T22:53:18.420

10Exponentiation is right-associative, so you can drop the parentheses. – user2357112 supports Monica – 2014-08-26T01:52:25.160

1@user2357112 Thank, edited that in! – xnor – 2014-08-26T22:55:52.167

3Erm, actually the computation is optimized out during compilation. The fact is: the compiler is trying to compute the value to put in the bytecode constant. The loading of the result is not optimized out, however the computation of the constant is. In other words the running time of your program is only the time taken to create the int object from the binary representation, not the time taken to compute it using powers. (try to check the disassembly using the dis module). In any case the running time would be big enough. – Bakuriu – 2014-08-27T13:52:52.710

3@Bakuriu This is true for CPython, but it's not part of the specification of Python, so I think the calculation should count towards the running time. (Not that it matters, really.) – flornquake – 2014-08-27T22:23:03.290

12It's worth noting that 9**9**9e9 is just as short and takes slightly more universe-lengths to compute, as well as looking a little nicer. – abarnert – 2014-08-29T19:31:49.067

3@abarnert "Slightly" is a slight understatement... – Tobias Kienzler – 2014-09-01T06:17:48.950

35

Marbelous 68 66 bytes

}0
--@2
@2/\=0MB
}0@1\/
&0/\>0!!
--
@1
00@0
--/\=0
\\@0&0

Marbelous is an 8 bit language with values only represented by marbles in a Rube Goldberg-like machine, so this wasn't very easy. This approach is roughly equivalent to the following pseudo-code:

function recursiveFunction(int i)
{
    for(int j = i*512; j > 0; j--)
    {
        recursiveFunction(i - 1);
    }
}

since the maximum value is 256, (represented by 0 in the Marbleous program, which is handled differently in different places) recursiveFunction(1) will get called a total of 256!*512^256 which equals about 10^1200, easily enough to outlive the universe.

Marbelous doesn't have a very fast interpreter, it seems like it can run about 10^11 calls of this function per year, which means we're looking at a runtime of 10^1189 years.

Further explanation of the Marbelous board

00@0
--/\=0
\\@0&0

00 is a language literal (or a marble), represented in hexadecimal (so 0). This marble falls down onto the --, which decrements any marble by 1 (00 wraps around and turns into FF or 255 in decimal). The Marble with now the value FF falls down onto the \\ which shoves it one column to the right, onto the lower @0. This is a portal and teleports the marble to the other @0 device. There, the marble lands on the /\ device, which is a duplicator, it puts one copy of the marble on the -- to its left (this marble will keep looping between the portals and get decremented on every loop) and one on the =0 to its right. =0 compares the marble to the value zero and lets teh marble fall trough if it's equal and pushes it to the right if not. If the marble has teh value 0, it lands on &0, a synchonizer, which I will explain further, later.

All in all, this just starts with a 0 value marble in a loop and decrements it until it reaches 0 again, it then puts this 0 value marble in a synchronizer and keeps looping at the same time.

}0@1
&0/\>0!!
--
@1

}0 is an input device, initially the nth (base 0) command line input when calling the program gets placed in every }n device. So if you call this program with command line input 2, a 02 value marble will replace this }0. This marble then falls down into the &0 device, another synchronizer, &n synchronizers hold marbles until all other corresponding &n's are filed as well. The marble then gets decremented, teleported and duplicated much like in the previously explained loop. The right copy then get checked for inequality with zero (>0) if it's not 0, it falls through. If it is 0, it gets pushed to the right and lands on !!, which terminates the board.

Okay, so far we have a loop that continuously counts down from 255 to 0 and lets another, similar loop (fed by the command line input) run once every time it hits 0. When this second loop has run n times (maximum being 256) the program terminates. So that's a maximum of 65536 runs of the loop. Not nearly enough to outlive the universe.

}0
--@2
@2/\=0MB

This should start looking familiar, the input gets decremented once, then this value loops around and get copied (note that the marble only gets decremented once, not on every run of the loop). It then gets checked for equality to 0 and if it's not zero lands on MB. This is a function in Marbelous, every file can contain several boards and each board is a function, every function has to be named by preceding the grid by :[name]. Every function except for the first function in the file, which has a standard name: MB. So this loop continuously calls the main board again with a value of n - 1 where n is teh value with which this instance of teh function was called.

So why n*512?

Well, the first loop runs in 4 ticks (and 256 times) and the second loop runs n times before the board terminates. This means the board runs for about n*4*256 ticks. The last loop (which does the recursive function calling) is compacter and runs in 2 ticks, which means it manages to call the function n*4*256/2 = n*512 times.

What are the symbols you didn't mention?

\/ is a trash bin, which removes marbles from the board, this makes sure discarted marbles don't interfere with other marbles that are looping a round and prevent the program from terminating.

Bonus

Since marbles that fall off the bottom of a marbelous board get output to STDOUT, this program prints a plethora of ASCII characters while it runs.

overactor

Posted 2014-08-24T22:02:52.477

Reputation: 3 500

2Great explanation, thanks! – Beta Decay – 2014-08-25T09:24:52.750

2Wow, this is a brilliant idea! The Marbelous language is so fun! – rubik – 2014-09-01T08:46:57.340

2+1 Just what I wanted to see. A crazier language than BrainFuck :) Is there a website with tutorial and more info about it? (The title link seem to have less doc than your answer) – Sylwester – 2014-09-01T22:06:19.170

2@Sylwester, I'm glad you like it, Marbelous is currently still in development but we expect to have it in a more stable condition in the near future, at which point tutorials, more extensive documentation, standard libraries and hopefully an online interpreter will follow. – overactor – 2014-09-02T09:33:38.397

35

GolfScript (12 7 chars)

9,{\?}*

This computes and prints 8^7^6^5^4^3^2 ~= 10^10^10^10^183230. To print it (never mind the computation) in 10^1000 years ~= 10^1007.5 seconds, it needs to print about 10^(10^10^10^183230 - 10^3) digits per second.

Peter Taylor

Posted 2014-08-24T22:02:52.477

Reputation: 41 901

22But it will halt long before that with a "printer out of paper" message... – Floris – 2014-08-26T22:17:18.427

1@Floris who the hell uses physical media in these days? – John Dvorak – 2014-08-29T08:13:31.203

3@JanDvorak, I just assumed that Floris and the 7 people who upvoted him are from my grandad's generation, when all output was to continuous feed paper. – Peter Taylor – 2014-08-29T08:21:41.173

2@PeterTaylor - maybe not quite that old, but I am old enough to recall submitting "batch jobs" to "the computer" (in the days when there was no doubt, in a student population of 20k, which computer you meant), and collecting the printout the following day. You (and 7 others) correctly surmised this was an attempt at humor, not a serious critique of your excellent and ridiculously short script. – Floris – 2014-08-29T11:49:05.787

21

Perl, 66 58 characters

sub A{($m,$n)=@_;$m?A($m-1,$n?A($m,$n-1):1):$n+1;}A(9,9);

The above is an implementation of the Ackermann–Péter function. I have no idea how large A(9,9) is but I am fairly certain it will take an astoundingly long time to evaluate.

Greg Hewgill

Posted 2014-08-24T22:02:52.477

Reputation: 2 641

5+1 ... I was trying to find a language with a built-in Ackermann function, but failed to do so before my patience ran out. :D – Martin Ender – 2014-08-24T23:36:34.817

1

You should try using Graham's Numbers

– kb_sou – 2014-08-24T23:42:33.210

3$n?A($m-1,A($m,$n-1)):A($m-1,1) admits an easy 8-char saving by pushing in the ternary operator. – Peter Taylor – 2014-08-25T16:04:51.080

3I am pretty sure the number of digits in A(9,9) is larger than the volume of the observable universe measured in cubic Planck lengths. – kasperd – 2014-08-28T06:51:43.800

6@kasperd That's a pretty massive understatement. The volume of the observable universe is only on the order of 10^184 planck volumes. By comparison, there are something like 10^19700 digits in the number describing the number of digits in A(4,4), which in turn is incomprehensibly tiny compared to A(9,9). – user19057 – 2014-08-28T22:02:00.130

3@user19057 It sounds like calling Kasperd's claim a "massive understatement" is a massive understatement. :P – Nicu Stiurca – 2014-09-02T23:25:24.297

20

MATLAB, 58 52 characters

We need at least one finite-precision arithmetic solution, hence:

y=ones(1,999);while y*y',y=mod(y+1,primes(7910));end

x=ones(1,999);y=x;while any(y),y=mod(y+x,primes(7910));end

(with thanks to @DennisJaheruddin for knocking off 6 chars)

The number of cycles needed to complete is given by the product of the first 999 primes. Since the vast majority of these are well over 10, the time needed to realize convergence would be hundreds or thousands of orders of magnitude greater than the minimum time limit.

COTO

Posted 2014-08-24T22:02:52.477

Reputation: 3 701

+1 Took a while for me to see what you are doing there. Nice! – Fixed Point – 2014-08-25T01:17:54.077

+1 CRT, isn't it? – flawr – 2014-08-25T08:08:01.697

Nice, I think some chars can be saved like so: y=ones(1,999);while y*y',y=mod(y+1,primes(7910));end – Dennis Jaheruddin – 2014-08-25T09:47:00.863

@DennisJaheruddin: Brilliant shortening. I'll update. – COTO – 2014-08-25T12:53:22.850

Though it is not the same solution anymore, this should still be similar enough, and again a bit shorter: p=1:9e9;y=p;while+y*y',y=mod(y+1,p),end – Dennis Jaheruddin – 2014-08-25T14:48:09.587

19

Mathematica, 25 19 bytes

This solution was posted before time functions were disqualified.

While[TimeUsed[]<10^10^5]

TimeUsed[] returns the seconds since the session started, and Mathematica uses arbitrary-precision types. There are some 107 seconds in a year, so waiting 1010000 seconds should be enough.

Shorter/simpler(/valid) alternative:

For[i=0,++i<9^9^9,]

Let's just count instead. We'll have to count a bit further, because we can do a quite a lot of increments in a second, but the higher limit doesn't actually cost characters.

Technically, in both solutions, I could use a much lower limit because the problem doesn't specify a minimum processor speed.

Martin Ender

Posted 2014-08-24T22:02:52.477

Reputation: 184 808

Love it! This answer made me actually laugh out loud with a big smile on my face. – Todd Lehman – 2014-08-24T23:28:53.070

1Sorry, for creativity sake, I had to cut out time based solutions (like your first one). Please don't hate me. :) – kb_sou – 2014-08-24T23:39:11.407

5@kbsou Well, I've beaten it with my other one, so I don't really care. But otherwise disqualifying answers retrospectively for rule changes isn't cool though. ;) – Martin Ender – 2014-08-24T23:40:34.257

1Is Mathematica really so slow, that computing 9^9^9 takes more than 10^1000 years? I estimate that computing 9^9^9 on my 1.3GHz U7300 using bc would take less than 6 months. (Based on extrapolating the time to compute 9^200000 and 9^400000.) – kasperd – 2014-08-27T22:40:31.597

Just tested: Mathematica needs 4 milliseconds to determine that the value of 9^9^9 is Overflow[]. – celtschk – 2014-08-27T22:46:52.507

kk, I'm ditching that one then ;) – Martin Ender – 2014-08-27T23:25:08.130

@MartinBüttner The idea is not totally bad though. I think 9^9^9^9 would qualify. – kasperd – 2014-08-28T06:30:00.573

@kasperd Hm, good point. In fact, that's probably the one I actually meant, as it's much closer to xnor's 9^9^1e9. – Martin Ender – 2014-08-28T06:35:31.520

@MartinBüttner 1e9 is a little bit larger than 9^9, but still use the same number of digits. If the language supports the scientific notation, one could use 9^9^9e9 which would be even larger and thus take more time to compute. – kasperd – 2014-08-28T06:54:36.460

@kasperd It does, but only in the form 9*^9 so that's another character. But I think 9^9^9^9 should be enough. – Martin Ender – 2014-08-28T06:57:37.517

Just another note: JavaScript takes (apparently) 0 milliseconds to determine 9^9^9 as Infinity – ArtOfCode – 2014-08-31T20:29:56.083

2@ArtOfCode Mathematica uses arbitrary-precision types so it will actually try to determine the correct value. – Martin Ender – 2014-08-31T20:32:46.237

17

Python 3 - 49

This does something useful: calculates Pi to unprecedented accuracy using the Gregory-Leibniz infinite series.

Just in case you were wondering, this program loops 10**10**10**2.004302604952323 times.

sum([4/(i*2+1)*-1**i for i in range(1e99**1e99)])

Arbitrary precision: 78

from decimal import*
sum([Decimal(4/(i*2+1)*-1**i)for i in range(1e99**1e99)])

Image source

The Terminal Breath

Due to the huge calculations taking place, 1e99**1e99 iterations takes just under 1e99**1e99 years. Now, (1e99**1e99)-1e1000 makes barely any difference. That means that this program will run on far longer than the death of our universe.

Rebirth

Now, scientists propose that in 10**10**56 years, the universe will be reborn due to quantum fluctuations or tunnelling. So if each universe is exactly the same, how many universe will my program live through?

(1e99**1e99)/(1e10+1e1000+10**10**56)=1e9701

Assuming that the universe will always live 1e10+1e1000 years and then take 10**10**56 years to 'reboot', my program will live through 1e9701 universes. This is assuming, of course, that unobtainium can live through the Big Bang.

Beta Decay

Posted 2014-08-24T22:02:52.477

Reputation: 21 478

But does it terminate? – Philipp – 2014-08-25T10:15:38.667

3it terminates once it reaches the end of the range @Philipp. yes it terminates, eventually. – Malachi – 2014-08-25T13:17:22.237

@Philipp it's a for loop, how wouldn't it terminate? – Vogel612 – 2014-08-25T13:21:58.967

11000**1000 is 1e3000, not 1e2000. – Cornstalks – 2014-08-25T13:56:36.010

Does that simple algorithm actually calculate pi to arbitrary precision? – Cruncher – 2014-08-25T15:38:20.743

1@Cornstalks Thanks, I didn't have a calculator good enough to find that so I made a guess based on the fact that 100**100=1E200. – Beta Decay – 2014-08-25T17:10:12.253

1

@BetaDecay: I might suggest Wolfram|Alpha as an online calculator. If you haven't ever used it, it's pretty awesome!

– Cornstalks – 2014-08-25T17:13:16.220

2@anyoneinterested Or 1000^1000 = (10^3)^1000 = (101010)(101010)...(1010*10) [1000 times] = 10^3000 – IazertyuiopI – 2014-08-26T05:37:30.543

12

Python 59 (works most of the time)

I couldn't resist

from random import*
while sum(random()for i in range(99)):0

While its true that this could theoritically terminate in under a millisecond, the average runtime is well over 10^400 times the specified lifespan of the universe. Thanks to @BetaDecay, @undergroundmonorail, and @DaboRoss for getting it down a 17 characters or so.

KSab

Posted 2014-08-24T22:02:52.477

Reputation: 5 984

To get it down to 71 you can replace continue with pass – Beta Decay – 2014-08-27T15:44:39.113

@BetaDecay Nice catch – KSab – 2014-08-27T18:21:45.757

I'm not sure where your character count is coming from. I'm getting 66. You can (probably, I haven't tested it) get that down to 64 by removing the square brackets. – undergroundmonorail – 2014-08-27T19:06:12.643

@undergroundmonorail Oh I forgot to change that from an older version and I did test removing the brackets and it worked (I also changed xrange to range for a total score of 63). – KSab – 2014-08-28T01:53:46.153

Thought of another improvement: random is a long enough module name that you save overall by doing from random import* and taking away the random. from random.random(). – undergroundmonorail – 2014-08-28T02:10:03.037

3I think since the question asks for the expected running time, it's not a problem that this might terminate early. The larger issue is that it can't be proven to terminate at all. – user19057 – 2014-08-28T22:08:44.247

@user19057 Well assuming that Python's random is truly random, each possible outcome having an equal chance of occurring and being completely mutually exclusive from previous outcomes (which admittedly it might not be), I don't think you could say that it would NEVER terminate. As the runtime approaches infinity, the probability of termination approaches one. – KSab – 2014-08-29T04:53:18.183

4@user19057 Assuming what KSab said, the expected running time is finite and the program terminates with 100% probability. Of course the random module actually uses a PRNG, which is cyclic, so most likely this will never terminate. – Jerome Baum – 2014-08-31T16:56:55.040

1I think you can cut off 3 characters by replacing 'pass' with '0'. – daboross – 2014-08-31T20:08:54.550

8

J - 5 chars, I think

Note that all of the following is in arbitrary precision arithmetic, because the number 9 always has a little x beside it.

In seven characters, we have !^:!!9x, which is kinda like running

n = 9!
for i in range(n!):
    n = n!

in arbitrary precision arithmetic. This is definitely over the limit because Synthetica said so, so we have an upper bound.

In six chars, we can also write ^/i.9x, which calculates every intermediate result of 0 ^ 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8. Wolfram|Alpha says 2^3^4^5^6^7^8 is approximately 10 ^ 10 ^ 10 ^ 10 ^ 10 ^ 10 ^ 6.65185, which probably also clears inspection.

We also have the five char !!!9x, which is just ((9!)!)!. W|A says it's 10 ^ 10 ^ 10 ^ 6.2695, which should still be big enough... That's like 1.6097e1859933-ish digits, which is decidedly larger than 3.154e1016, the number of nanoseconds in the universe, but I'm gonna admit I have no idea how one might figure out the real runtimes of these things.

The printing alone should take long enough to last longer than the universe, though, so it should be fine.

algorithmshark

Posted 2014-08-24T22:02:52.477

Reputation: 8 144

7

C, 63 56 chars

f(x,y){return x?f(x-1,y?f(x,y-1):1):y+1;}main(){f(9,9);}

This is based on an idea by a guy named Wilhelm. My only contribution is condensing the code down to this short (and unreadable) piece.

Proving that it terminates is done by induction.

  • If x is 0, it terminates immediately.
  • If it terminates for x-1 and any y, it also terminates for x, this itself can be shown by induction.

Proving the induction step by induction:

  • If y is 0 there is only one recursive call with x-1, which terminates by induction assumption.
  • If f(x, y-1) terminates then f(x, y) also terminates because the innermost call of f is exactly f(x, y-1) and the outermost call terminates according to the induction hypthesis.

The expected running time is A(9,9) / 11837 seconds. This number has more digits than the number of quarks in the observable universe.

kasperd

Posted 2014-08-24T22:02:52.477

Reputation: 829

(Ab)use the preprocessor and define m=main, r=return, and z=99999, then rewrite your program as, f(x,y){r x?f(x-1,y?f(x,y-1):1):y+1;}m(){f(z,z);} which will take an amazingly long time :-) – ChuckCottrill – 2014-08-30T00:15:11.707

5@ChuckCottrill If the rules allowed programs, which require specific preprocessor macros, and those did not count towards program length, then any task can be solved in one character. – kasperd – 2014-08-30T09:46:11.823

6

Matlab (10 8 chars)

1:9e1016

IMHO, most entries are trying too hard by computing large, complicated things. This code will simply initialize an array of 9x101016 doubles counting up from 1, which takes 7.2x10^1017 bytes. On a modern CPU with a maximum memory bandwidth of 21 GB/s or 6.63x10^17 bytes/year, it will take at least 1.09x101000 years to even initialize the array, let alone try to print it since I didn't bother suppressing the output with a trailing semicolon. (;


old solution(s)

nan(3e508)

Alternatively

inf(3e508)

This code will simply create a square matrix of NaNs/infinities of size 3e508x3e508 = 9e1016 8-byte doubles or 7.2e1017 bytes.

Nicu Stiurca

Posted 2014-08-24T22:02:52.477

Reputation: 160

1What's that? 1016? That must be 9999! (Or did I misunderstand something?) – Mega Man – 2016-07-17T11:05:04.500

@MegaMan The problem prompt asks for runtime lower bound of 10^1000 years. This being golf, I didn't want to be wasteful and compute too much longer than that, so I tried to get it to stop as soon after reaching the threshold as possible. :) – Nicu Stiurca – 2016-07-18T22:33:41.883

ah, ok, didn't know this rule – Mega Man – 2016-07-20T17:15:19.743

5

Perl, 16 chars

/$_^/for'.*'x1e9

This builds a string repeating ".*" a billion times, then uses it as both the needle and the haystack in a regex match. This, in turn, causes the regex engine to attempt every possible partition of a string two billion characters long. According to this formula from Wikipedia, there are about 1035218 such partitions.

The above solution is 16 characters long, but only requires about 2Gb of memory, which means it can be run on a real computer. If we assume infinite memory and finite register size (which probably doesn’t make sense), it can be shortened to 15 characters while dramatically increasing the runtime:

/$_^/for'.*'x~0

(I haven’t tested it, but I think it could work with a 32-bit Perl built on a 64-bit machine with at least 6Gb of RAM.)

Notes:

  • x is the string repeat operator.
  • the for isn’t an actual loop; it is only used to save one character (compared to $_=".*"x1e9;/$_^/).
  • the final ^ in the regex ensures that only the empty string can match; since regex quantifiers are greedy by default, this is the last thing the engine will try.
  • benchmarks on my computer for values (1..13) suggest that the running time is actually O(exp(n)), which is even more than the O(exp(sqrt(n))) in the Wikipedia formula.

Grimmy

Posted 2014-08-24T22:02:52.477

Reputation: 12 521

4

J (12)

(!^:(!!9))9x

What this comes down to in Python (assuming ! works):

a = 9 
for i in range((9!)!):
    a = a!

EDIT:

Well, the program can take, at most, 2 × 10^-1858926 seconds per cycle, to complete within the required time. Tip: this won't even work for the first cycle, never mind the last one ;).

Also: this program might need more memory than there is entropy in the universe...

ɐɔıʇǝɥʇuʎs

Posted 2014-08-24T22:02:52.477

Reputation: 4 449

3"might need more memory than there is entropy in the universe" - You can cut down on that with xrange() ;) – Stefan Majewsky – 2014-08-25T22:04:23.727

1Also, ! doesn't work in Python. You need import math and math.factorial(). – daviewales – 2014-08-27T01:04:13.227

4

First attempt at code golf but here goes.

VBA - 57 45

x=0
do
if rnd()*rnd()<>0 then x=0
x=x+1
while 1=1

So X will go up by one if a 1 in 2^128 event occurs and reset if it does not occur. The code ends when this event happens 2^64+1 times in a row. I don't know how to begin calculating the time but I'm guessing it is huge.

EDIT: I worked out the math and the probability of this happening in each loop is 1 in 2^128^(1+2^64) which is about 20000 digits long. Assuming 1000000 loops/sec (ballpark out of thin air number) and 30000000 s/yr that's 3*10^13 cycles per year time 10^1000 years left is 3*10^1013 cycles, so this would likely last around 20 times the remaining time left in the universe. I'm glad my math backs up my intuition.

Myles Horne

Posted 2014-08-24T22:02:52.477

Reputation: 41

I think that the last line should be While x=1, right? (otherwise its an infinite loop). Also, you can shave off 12 chars if you substitute Dim x As Double with x=0 (VBA doesn't require to declare variables unless you specify Option Explicit) – kb_sou – 2014-08-29T16:15:20.390

I don't look at it as an infinite loop as it breaks when x overflows which is eventually. – Myles Horne – 2014-08-29T17:15:44.450

It definitely doesn't work with while x=1 as this would generally prevent the loop from running. – Myles Horne – 2014-08-29T17:17:17.780

If breaking the in this way loop doesn't meet the "no infinite loops" criteria the WHILE 1=1 could change to WHILE ISNUMERIC(X). – Myles Horne – 2014-08-29T17:31:30.337

4

C# 217

I'm not much of a golfer, but I couldn't resist Ackerman's function. I also don't really know how to calculate the runtime, but it will definitely halt, and it will definitely run longer than this version.

class P{
static void Main(){for(int i=0;i<100;i++){for(int j=0;j<100;j++){Console.WriteLine(ack(i,j));}}}
static int ack(int m,int n){if (m==0) return n+1;if (n ==0) return ack(m-1,1);return ack(m-1,ack(m,n-1));}
}

RubberDuck

Posted 2014-08-24T22:02:52.477

Reputation: 321

You can save 10 bytes by renaming the ack function to a single-character name like a. – pppery – 2019-09-02T17:59:32.140

4

C, 30 characters

main(i){++i&&main(i)+main(i);}

Assuming two's compliment signed overflow and 32-bit ints, this will run for about 2232 function calls, which should be plenty of time for the universe to end.

Uristqwerty

Posted 2014-08-24T22:02:52.477

Reputation: 41

You'll run out of stack long before then, though. – Sparr – 2014-08-30T00:08:32.257

1@Sparr One of the rules is to assume infinite stack and heap size. – scragar – 2014-09-26T09:29:04.683

3

GolfScript, 13 chars

0{).`,9.?<}do

This program just counts up from 0 to 1099−1 = 10387420488. Assuming, optimistically, that the computer runs at 100 GHz and can execute each iteration of the program in a single cycle, the program will run for 1099−12 seconds, or about 3 × 1099−20 = 3 × 10387420469 years.

To test the program, you can replace the 9 with a 2, which will make it stop at 1022−1 = 103 = 1000. (Using a 3 instead of a 2 will make it stop at 1033−1 = 1026, which, even with the optimistic assumptions above, it will not reach for at least a few million years.)

Ilmari Karonen

Posted 2014-08-24T22:02:52.477

Reputation: 19 513

3

Autohotkey 37

loop {
if (x+=1)>10^100000000
break
}

Person93

Posted 2014-08-24T22:02:52.477

Reputation: 171

3

Haskell, 23

main=interact$take$2^30

This program terminates after reading 1073741824 characters from stdin. If it's run without piping any data to stdin, you will have to type this number of characters on your keyboard. Assuming your keyboard has 105 keys, each rated for 100k mechanical cycles and programmed to generate non-dead keystrokes, autorepeat is off, and your keyboard socket allows 100 connection cycles, this gives the maximum number of keystrokes per computer uptime of 1050000000, which is not enough for the program to terminate.

Therefore, the program will only terminate when better hardware becomes available in terms of number of cycles, which is effectively never in this running universe. Maybe next time, when quality has higher priority than quantity. Until then, this program terminates in principle but not in practice.

TheSpanishInquisition

Posted 2014-08-24T22:02:52.477

Reputation: 421

What if you hot-swap keyboards as you go? – Thomas – 2014-08-29T09:55:15.097

That's covered by the 100 connection cycles of the keyboard socket. – TheSpanishInquisition – 2014-08-29T11:52:14.310

But the point of the problem is that the program does terminate, somewhere after the heat death of the universe. This program can't possibly ever terminate; once entropy gets high enough, you will never have another keyboard to plug in. – abarnert – 2014-08-29T19:40:01.973

1I am still not convinced. If you run the program remotely (or in a VM), then you aren't limited by the hardware capabilities of a single computer, and 1 billion strokes really isn't that much. Besides, the problem says the computer is made of unobtainium, and so the keyboard should also be, hence, it can handle 2^30 keystrokes... – Thomas – 2014-08-30T06:32:19.550

3

~ATH, 56

In the fictional language ~ATH:

import universe U;
~ATH(U) {
} EXECUTE(NULL);
THIS.DIE()

~ATH is an insufferable language to work with. Its logic is composed of nothing but infinite loops, or at best, loops of effectively interminable construction.

What many ~ATH coders do is import finite constructs and bind the loops to their lifespan. For instance the main loop here will terminate on the death of the universe, labeled U. That way you only have to wait billions of years for it to end instead of forever.

I apologize for the borderline loophole violations; I thought it was too relevant to pass up.

If anyone was actually amused by this, more details: (1), (2), (3), (4)

DBN

Posted 2014-08-24T22:02:52.477

Reputation: 39

3

APL, 10

I don't think this is a valid answer (as it is non-deterministic), but anyway......

{?⍨1e9}⍣≡1

This program calculates a random permutation of 1e9 numbers (?⍨1e9) and repeats until two consecutive outputs are equal (⍣≡)

So, every time a permutation is calculated, it has a 1 in 1000000000! chance of terminating. And 1000000000! is at least 10108.

The time it takes to calculate a permutation is rendered irrelevant by the massiveness of 1000000000!. But some testing show this is O(n) and extrapolating gives about 30 seconds.

However, my interpreter refuses to take inputs to the random function larger than 231-1 (so I used 1e9), and generating permutations of 1000000000 numbers gave a workspace full error. However, conceptually it can be done with a ideal APL interpreter with infinite memory.

This leads us to the possibility of using 263-1 in place of 1e9 to bump the running time up to at least 101020, assuming a 64-bit architecture.

But wait, is architecture relevant in an ideal interpreter? Hell no so there is actually no upper bound on running time!!

TwiNight

Posted 2014-08-24T22:02:52.477

Reputation: 4 187

3

R, 45 bytes

(f=function(x)if(x)f(x-1)+f(x-1)else 0)(9999)

It's an old thread but I see no R answer, and we can't be having that!

The runtime for me was about 1s when x was 20, suggesting a runtime of 2^9979 seconds.

If you replace the zero with a one, then the output would be 2^x, but as it stands the output is zero whatever x was (avoids overflow problems).

JDL

Posted 2014-08-24T22:02:52.477

Reputation: 1 135

Equivalent 38 byte version.

– Robin Ryder – 2019-10-27T18:21:55.617

I'm not sure yours is equivalent — it doesn't fork the calls in the same way, and indeed your TIO example runs for argument 9999 in under a second (I think a pair of brackets would fix that, though). – JDL – 2019-10-28T08:37:14.423

My bad, I included an extra !. This is equivalent (and 37 bytes). You can also shave off another byte with 1e4 instead of 9999.

– Robin Ryder – 2019-10-28T10:55:55.823

2

Ruby(34)

The line ([0]*9).permutation.each{print} takes about 2.47 seconds for 9! prints on my machine, while the line ([0]*10).permutation.each{print} takes about 24.7 seconds for 10! prints, so I guess I can extrapolate here and calculate (24.7/10!)*470! seconds in years which is 6.87 * 10^1040, which should be the run time of:

([0]*470).permutation.each{print}

Boris

Posted 2014-08-24T22:02:52.477

Reputation: 161

2

JavaScript 68 62 chars

(function a(m,n){return m==0?n+1:a(m-1,n==0?1:a(m,n-1))})(5,1)

This uses the Ackermann-function which can be written as

function ackermann(a, b) {
  if (a == 0) return b + 1;
  if (b == 0) return ackermann(a-1, 1);
  else return ackermann(a-1, ackermann(a, b-1));
}

Its runtime time increases over exponentially and takes therefore very long to calculate. Even though it is not english here you can get an overview of its return values. According to the table ackermann(5,1) equals 2↑↑(65533)-3 which is, you know, very big.

henje

Posted 2014-08-24T22:02:52.477

Reputation: 121

instead of n==0?X:Y, you can always do n?Y:X – Cyoce – 2016-10-05T05:04:13.377

2This can benefit from some of the same optimisations as the earlier Perl Ackermann function implementation. – Peter Taylor – 2014-08-26T22:23:35.357

I must have overlooked the perl solution. Thanks for pointing that out. – henje – 2014-08-27T07:57:07.270

2

Befunge '93 - 40 bytes

(20x2 program)

v<<<<<<<<<<<<<<<<<<<
>??????????????????@

This program relies on random numbers to give it delay. Since Befunge interpreters are quite slow, this program should fit the bill. And if it doesn't, we can always expand it horizontally. Im not exactly sure how to go about calculating the expected running time of this program, but I know that each ? has a 50/50 chance of either starting over or changing its horizontal position by 1. There are 18 ?'s. I think it should be something along the lines of (18^2)!, which google calculator says is "Infinity"

EDIT: Whoops I did not notice the other Befunge answer, this is my first post here. Sorry.

rodolphito

Posted 2014-08-24T22:02:52.477

Reputation: 795

Hey, don't worry about the other befubnge answer, or, or in general, using the same language as someone else. I mean, nobody is going to beat the mathlab one, so all other submissions are about fun. Mine was. – AndoDaan – 2014-08-29T18:29:59.207

1

Turing Machine But Way Worse, 167 bytes

0 0 1 1 2 0 0
1 0 1 0 4 0 0
0 1 1 1 2 0 0
1 1 1 1 5 0 0
0 2 1 0 3 0 0
1 2 0 1 1 0 0
0 3 1 1 4 0 0
1 3 0 0 2 0 0
0 4 1 0 0 0 0
1 4 0 1 3 0 0
0 5 1 0 6 0 1
1 5 1 1 2 0 0

Try it online!

Should run the 6-state 2-symbol Busy Beaver from the Wikipedia page.

As said in the Wikipedia page, it runs in \$7.412 \times 10^{36534}\$ steps. This is way more than the heat death, assuming it executes each command in more than or equal to a nanosecond.

MilkyWay90

Posted 2014-08-24T22:02:52.477

Reputation: 2 264

1

Javascript, 120 bytes

a=[0];while(a.length<1e4)(function(){var b=0;while(b<a.length){a[b]=(a[b]+1)%9;if(a[b])return;b++}a.push(1)})();alert(a)

Can be done with minimal memory (probably less than half a megabyte) but takes (probably) about 108,750 years to halt.

Repeatedly increments a little-endian base-9 BigInteger until it reaches 9104-1.

SuperJedi224

Posted 2014-08-24T22:02:52.477

Reputation: 11 342

1

Python 3, 191 Bytes

from random import*
r=randint
f=lambda n:2if n<2else f(n-1)
x=9E999
s=x**x
for i in range(f(x)**f(s)):
 while exec(("r(0,f(x**i))+"*int(f(x)))+"r(0,f(x**i))")!=0:
  s=f(x**s)
  print(s)

First, f is a recursive factorial function and ultra slow. Then, there is 9*10⁹⁹⁹ powed with itself, which generates an OverflowError, but this doesn't happen on this Unobtanium computer. The For-Loop iterates 9E999! ^ (9E999^9E999)! times and it only goes to the next iteration, if 9E999!+1 random ints between 0 and 9E99*^i! are all 0 and in every iteration of the while-loop s gets set to (9E999^s)!. Uh, I forgot that the printing of s takes muuuuccchhhh time...
I know it's not the shortest soloution, but I think it's really effective. Can someone help me calculating the running time?

Mega Man

Posted 2014-08-24T22:02:52.477

Reputation: 1 379

0

Runic Enchantments, ~1823 bytes

\DB͍R"000000000000000000000000000000000000001"$
.{ww;'''''''''''''''''''''''''''''''''''''''
.10{BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
a+'0000000000000000000000000000000000000000
kk0~111111111111111111111111111111111111111
$:{~+++++++++++++++++++++++++++++++++++++++
1'~}567890123456789012345678901234567890123
J://XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
.=? 000001111111112222222222333333333344444
/\/v\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Try it online!

Or rather, some arbitrarily large variation thereof. Taken from this answer where an explanation on how it works can be found. Additionally, running this "in perpetuity" would require a version of the interpreter which does not self-halt after 1,000,000 execution steps, but I will assume this as true as the unobtaining machine the code is running on as it only requires an adjustment to one line of code.

According to TIO this can print numbers out to 100 in about 0.141s (the flux here is almost irrelevant in terms of the final result: +/- ~10 bytes for being 10 times faster or slower for needing an extra digit of precision). So reaching 99999999999999999999999999999 should take about 4,471,080,669,710,806,697 (1018) years...and ten of its most significant decimals would still be 0, which bumps things to 1028 years. Depicted code only has 39 digits worth of precision in base-10 and we can go bigger.

Only one byte different takes it to an effective base 78 (with a maximum value around 6x1073). Tweaking the "0" down to the lowest allowable character ("#") brings that to base 92 (with a maximum value around 4x1076).

That brings us to needing a 515 digit base 92 number, which is also doable using the ´ command to need only n+1 cells to represent a base 10 number with n digits (for the purposes of write location offsets), e.g. this program changes the 4X3+ to 4´3, and as long as an empty row of cells is supplied between the column value (~1 byte per digit for all the spaces in order to allow for 3 digit numbers) and the 10B command any value can be expressed and needing approximately 5722 bytes.

However, we can still raise the integer base a bit more. ˿ is the last non-combining Unicode value accessible without performing any additional logic checks, allowing for a base 733 number which means that we only need 353 digits of precision, reducing the program size of about 3940 bytes.

But we can still go smaller still.

35-767 is not the largest unicode block usable without needing additional logic. If we instead use Unicode values 880 to 6831 (inclusive) we have an effective base of 5953, requiring only 268 digits of precision. Accounting for the two bytes needed to represent the initial characters, that gives a program size of approximately 3275 bytes.

Adding in the additional logic would allow for a maximum base of 64654, requiring only 211 digits of precision and costing only another 79 bytes or so based on this answer.

But we can still golf a few bytes. Remember the ´ command? We don't really need it. Now that we're down to only a 211 digit number, all the values needed (up to 216) can be represented with only 2-3 bytes (instead of 5): char values implicitly convert to integers when required in any command that expects integers and nearly all the chars needed (from 5 to 216) can be utilized in this manner. Only 10, 60, 62, 94 and 118 are problematic. And all of them can still be expressed in 4 bytes or less:

10  -> a
60  -> 6X
62  -> 6X2+
94  -> 9x4+
118 -> bX8+

For 62, 94, and 118 it ends up being easier to just not use those columns. Increases program size by 8 bytes each (24 bytes) in order to not need the spaces to align the commands out to 118 (236 bytes).

Final score: 1823 bytes.

211 digit number      -  1 byte per digit of initial value
Programmatic overhead -  56 bytes (roughly, the left 4 columns and right 3 columns)
Digit processing      -  7 bytes * digits (1477 bytes)
Bad Character logic   -  ~79 bytes (some additional control flow is likely required)

(Intermediate values were approximated at 10 bytes (original program height) per digit of initial value +57 +unicode penalty where appropriate: eg. 1 byte per digit of initial value +1 or +2 for representation in programmatic overhead).

Final thoughts:

It may be possible to turn this answer into a self-repeating execution instead of a quine, but I have so far not had success getting it to Eval properly. I suspect that it attempts to Eval the string and the parser throws an error for some reason. If that were fixed, a shorter solution to the "take forever" problem would be possible: push the quine, duplicate, Eval, compare with original (lossless). If not the same: loop, else terminate. The number of Eval attempts (i.e. the original period count) is already near the number of years required by this challenge, so increasing the number of loops by a factor of ~32 billion (iteration -> years) seems relatively doable in less than 1300 bytes. Increasing the program length from 387 "digits" to 485 "digits" looks like it would be enough.

Draco18s no longer trusts SE

Posted 2014-08-24T22:02:52.477

Reputation: 3 053