Shortest terminating program whose output size exceeds Graham's number

37

8

Write the shortest possible program (length measured in bytes) satisfying the following requirements:

  • no input
  • output is to stdout
  • execution eventually terminates
  • total number of output bytes exceeds Graham's number

Assume that programs run until "normal" termination on an ideal computer1 able to access unlimited resources, and that the common programming languages are modified if necessary (without changing the syntax) to allow this. Because of these assumptions, we might call this a kind of Gedankenexperiment.

To get things started, here's a 73-byte Ruby program that computes fω+1(99) in the fast-growing hierarchy:

f=proc{|k,n|k>0?n.times{n=f[k-1,n]}:n+=1;n};n=99;n.times{n=f[n,n]};puts n

1 EDIT: More precisely, suppose we're taking an existing system and modifying it only to have no upper limit on storage size (but it is always finite). The execution-times of instructions are not supposed to be modified, but the machine is assumed to be ideal in that it will have no upper limit on its operating lifetime.

r.e.s.

Posted 2012-06-22T02:37:46.443

Reputation: 2 872

This takes my tetration question to a whole new level!

– MrZander – 2012-06-25T03:47:07.190

1

There was once a similar programming contest called the Bignum Bakeoff. Some of the entries are quite interesting; the results are here: http://djm.cc/bignum-results.txt

– Danny Chia – 2013-06-09T19:01:34.973

Answers

11

GolfScript (49 47 chars)

4.,{\):i\.0={.0+.({<}+??\((\+.@<i*\+}{(;}if.}do

See Lifetime of a worm for lots of explanation. In short, this prints a number greater than fωω(2).

Peter Taylor

Posted 2012-06-22T02:37:46.443

Reputation: 41 901

f_(ω^ω)(2) is about as large as g_(f_8(8)), so not as overkill as that expression would imply. – Simply Beautiful Art – 2017-12-03T20:47:40.000

21

Haskell, 59 57 55 63

(f%s)1=s;(f%s)n=f.(f%s)$n-1
main=print$((flip((%3)%(3^))3)%4)66

How it works: % simply takes a function and composes it n-1 times on top of s; i.e. %3 takes a function f and returns a function of n that equals applying it f to 3, n-1 times in a row. If we iterate the application of this higher-order function, we get a fast-growing sequence of functions – starting with exponentiation, it's exactly the sequence of Knuth-arrow-forest sizes:
((%3)%(3^))1 n = (3^)n     = 3ⁿ = 3↑n
((%3)%(3^))2 n = ((3^)%3)n = (3↑)ⁿ⁻¹ $ 3 = 3↑↑n
((%3)%(3^))3 n = (((3^)%3)%3)n = (3↑↑)ⁿ⁻¹ $ 3  = 3↑↑↑n
and so on. ((%3)%(3^))n 3 is 3 ↑ⁿ 3, which is what appears in the calculation to Graham's number. All that's left to do is composing the function (\n -> 3 ↑ⁿ 3) ≡ flip((%3)%(3^))3 more than 64 times, on top of 4 (the number of arrows the calculation starts with), to get a number larger than Graham's number. It's obvious that the logarithm (what a lamely slow function that is!) of g₆₅ is still larger than g₆₄=G, so if we print that number the output length exceeds G.

ceased to turn counterclockwis

Posted 2012-06-22T02:37:46.443

Reputation: 5 200

When I test this with print$((flip((%3)%(3*))3)%2)1, there's a run-time error - can you say why? It succeeds when the 2 is changed to 1 (output is 81).

– r.e.s. – 2012-06-25T12:33:28.017

Oh... ideone seems to run a 32-bit version, so it gets to an overflow of Int quickly. On a 64-bit system, that consumes too much memory to reproduce, but of course it still won't allow to reach G. I need the (big-int) Integer type, so I can't use !!; wait... – ceased to turn counterclockwis – 2012-06-25T12:44:16.023

Fixed it now, had to use explicit recursion to implement %. – ceased to turn counterclockwis – 2012-06-25T12:51:26.137

I find ((%3)%(3*))2 n actually grows faster than you say (a good thing), but my Haskell-fu is inadequate to understand why. For n = 0, 1, 2, ..., instead of giving 3, 3^3, 3^(3^3), ..., it gives 3, 3^(3+1), 3^((3^(3+1))+1), .... – r.e.s. – 2012-06-29T14:15:39.310

As I said: "((%3)%(3*))n 3 is larger than 3 ↑ⁿ 3". Or do you mean something else? Anyway, I changed the definition so that it's all equalities (at least I think so, to lazy to check now...) rather than larger-thans. And if you change 66 to 65, it actually produces G itself, ain't that nice? – ceased to turn counterclockwis – 2012-06-29T21:43:57.933

With respect to the program before your latest changes, I wrote 2 n, not n 3: ((%3)%(3*))2 n grows faster than what you said, i.e. faster than 3↑↑n. The specific growth pattern is in my previous comment, and I don't understand Haskell syntax well enough to know what produced the +1 in that pattern. But I think your new version eliminates this discrepancy, and it's very neat that now your ((flip((%3)%(3^))3)%4)n produces exactly g_(n-1), where g_64 is Graham's number. – r.e.s. – 2012-06-30T01:38:31.367

I'm struggling with the syntax, and it would help immensely if I could see why, given your definition of %, does ((%3)%(3^))2 n == ((3^)%3)n. I'm deriving (incorrectly?) that ((%3)%(3^))2 n == ((%3).(3^))n, then not seeing why ((%3).(3^))n==((3^)%3)n. (I know comments are not the place for lengthy discussion, but perhaps something brief will help others too?) – r.e.s. – 2012-07-02T14:02:04.937

Hm... I suppose this wouldn't suffice for a StackOverflow question, would it? I could of course explain it in more detail here, but this challenge isn't really about how Haskell function compositions work, is it? – ceased to turn counterclockwis – 2012-07-02T14:06:49.237

No, of course this challenge isn't about how Haskell syntax works (hence my remark about brevity). SO might be the best idea. (I'm about to go offline for the day, though -- maybe I'll post something there later.) Thanks for your answer ... I'm trying to grok Haskell because of it. – r.e.s. – 2012-07-02T14:38:25.930

5

Pyth, 29 28 bytes

M?*GHgtGtgGtH^ThH=ZTV99=gZTZ

Defines a lambda for hyper-operation and recursively calls it. Like the definition for Graham's number, but with larger values.

This:

M?*GHgtGtgGtH^3hH

Defines a lambda, roughly equal to the python

g = lambda G, H:
  g(G-1, g(G, H-1)-1) if G*H else 3^(H+1)

This gives the hyper-operation function, g(G,H) = 3↑G+1(H+1).
So, for example, g(1,2)=3↑23 = 7,625,597,484,987, which you can test here.

V<x><y> starts a loop that executes the body, y, x times.
=gZT is the body of the loop here, which is equivalent to Z=g(Z,10)

The code:

M?*GHgtGtgGtH^3hH=Z3V64=gZ2)Z

Should recursively call the hyperoperation above 64 times, giving Graham's Number.

In my answer, however, I've replaced the single digits with T, which is initialized to 10, and increased the depth of recursion to 99. Using Graham Array Notation, Graham's Number is [3,3,4,64], and my program outputs the larger [10,11,11,99]. I've also removed the ) that closes the loop to save one byte, so it prints each successive value in the 99 iterations.

KSmarts

Posted 2012-06-22T02:37:46.443

Reputation: 1 830

3

Python (111+n), n=length(x)

Although this one is not as short as the answerer's Ruby program, I'll post it anyway, to rule this possibility out.

It uses the Ackermann function, and calls the Ackermann function with m and n being the values from another call to the Ackermann function, and recurses 1000 times.

This is probably bigger than Graham's number, but I'm not sure, because nobody knows the exact length of it. It can be easily extended, if it's not bigger.

x=999
b='A('*x+'5,5'+')'*x
def A(m,n):n+1 if m==0 else A(m-1,A(m,n-1)if n>0 else 1)
exec('print A('%s,%s')'%(b,b))

beary605

Posted 2012-06-22T02:37:46.443

Reputation: 3 904

output to stdout? also, you need a return statement or a lambda. – boothby – 2012-06-22T05:43:07.010

7Also if A(m,n) returns a single value, then isn't A(A(5,5)) missing an argument? ... This is the problem with a challenge like this: it encourages people not to test their code, because a complete run is purely theoretical. – breadbox – 2012-06-22T06:19:58.710

If you replace your last line with exec'x=A(x,x);'*x;print x, then the program is ok and the output is approximately f_(ω+1)(x) (assumimg the Ackermann function code is correct), which has more than G bytes even for x=99, say. (In my Ruby program, f[m,n] is a version of A(m,n).) – r.e.s. – 2012-06-22T12:18:25.167

@breadbox - Good point ... Theoretical questions like this require us to make sure a program is ok for small-parameter (i.e. non-theoretical) test-cases that clearly generalize to give a correct answer. – r.e.s. – 2012-06-22T12:46:36.380

1Its's longer, but if you want to use eval instead of exec, your last line could be f=lambda x:A(x,x);print eval('f('*x+'x'+')'*x). Also, your def of A(m,n) needs to be corrected per boothby's comment. – r.e.s. – 2012-06-22T13:51:15.720

Boothby: Yes, something I forgot to do. Breadbox: Yep, I noticed that at about midnight, when I was in bed, half-asleep. r.e.s: Will do. Thanks, guys! – beary605 – 2012-06-22T20:22:09.553

@breadbox: Except if you have an compiler, which proves your theorem for free. – user unknown – 2012-06-23T10:24:23.337

2

Ruby, 54 52 50 bytes

f=->b{a*=a;eval"f[b-1];"*b*a};eval"f[a];"*a=99;p a

Ruby, 85 81 76 71 68 64 63 59 57 bytes

f=->a,b=-a{eval"a*=b<0?f[a,a]:b<1?a:f[a,b-1];"*a};p f[99]

Pretty much fast growing hierarchy with f(a+1) > fω+1(a).


Ruby, 61 bytes

f=->a,b=-a{a<0?9:b==0?a*a:f[f[a-1,b],b>0?b-1:f[a,b+1]]};f[99]

Basically an Ackermann function with a twist.


Ruby, 63 59 bytes

n=99;(H=->a{b,*c=a;n.times{b ?H[[b-1]*n*b+c]:n+=n}})[n];p n

Another Ruby, 74 71 bytes

def f(a,b=a)a<0?b:b<0?f(a-1):f(a-1,f(a,b-1))end;n=99;n.times{n=f n};p n

Basically Ackermann function to itself 99 times.

Simply Beautiful Art

Posted 2012-06-22T02:37:46.443

Reputation: 2 140

0

Python: 85

f=lambda a,a:a*a
exec'f=lambda a,b,f=f:reduce(f,[a]*b,1)'*99
exec'f('*64+'3'+',3)'*64

Which maybe could be shortened to 74 + length(X):

f=lambda a,a:a*a
exec'f=lambda a,b,f=f:reduce(f,[a]*b,1)'*int('9'*X)
f(3,3)

Where X is an appropriate big number such that the resultant hyperoperation on 3, 3 is bigger than Grahams number(if this number is less than 99999999999 then some byte is saved).


Note: I assume the python code is executed on the interactive interpreter hence the result is printed to stdout, otherwise add 9 bytes to each solution for the call to print.

Bakuriu

Posted 2012-06-22T02:37:46.443

Reputation: 781

2Your 74ish byte solution does not produce an output nearly large enough. – lirtosiast – 2015-12-05T02:08:46.600

0

JavaScript, 68 bytes, however uncompeting for using ES6

a=(x,y)=>y?x?a(a(x-1,y)*9,y-1):a(9,y-1):x;b=x=>x?a(9,b(x-1)):9;b(99)

a function is similar to up-arrow notation with base 9.

       /a(a(x-1,y)*9,y-1)  x>0, y>0
a(x,y)=|a(9,y-1)           x=0, y>0
       \x                  y=0

b function is: b(x)=bx(9).

b(99) is ~fω+1(99), compared to Graham's number<fω+1(64).

Naruyoko

Posted 2012-06-22T02:37:46.443

Reputation: 459

If you've marked this non-competing due to the language being newer than the question, you don't have to do that anymore

– Jo King – 2019-04-29T21:48:33.370

0

Javascript, 83 bytes

Another Ackermann function solution.

(function a(m,n,x){return x?a(a(m,n,x-1),n,0):(m?a(m-1,n?a(m,n-1):1):n+1)})(9,9,99)

SuperJedi224

Posted 2012-06-22T02:37:46.443

Reputation: 11 342