40
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ This takes my tetration question to a whole new level! \$\endgroup\$
    – MrZander
    Jun 25, 2012 at 3:47
  • 2
    \$\begingroup\$ There was once a similar programming contest called the Bignum Bakeoff. Some of the entries are quite interesting; the results are here: djm.cc/bignum-results.txt \$\endgroup\$
    – Danny
    Jun 9, 2013 at 19:01
  • 1
    \$\begingroup\$ This takes 19 bytes in Binary Lambda Calculus, based on a 114-bit BLC program for computing Graham's number, with an additional 27 bits needed for replicating a final byte. \$\endgroup\$
    – John Tromp
    Apr 29, 2020 at 19:54

15 Answers 15

21
\$\begingroup\$

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.

\$\endgroup\$
9
  • \$\begingroup\$ 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). \$\endgroup\$
    – r.e.s.
    Jun 25, 2012 at 12:33
  • \$\begingroup\$ 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... \$\endgroup\$ Jun 25, 2012 at 12:44
  • \$\begingroup\$ Fixed it now, had to use explicit recursion to implement %. \$\endgroup\$ Jun 25, 2012 at 12:51
  • \$\begingroup\$ 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), .... \$\endgroup\$
    – r.e.s.
    Jun 29, 2012 at 14:15
  • \$\begingroup\$ 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? \$\endgroup\$ Jun 29, 2012 at 21:43
11
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ f_(ω^ω)(2) is about as large as g_(f_8(8)), so not as overkill as that expression would imply. \$\endgroup\$ Dec 3, 2017 at 20:47
6
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

Python 3, 53 bytes

m=lambda x:-x if x<0 else m(x-m(x-1))/2;print(1/m(9))

It computes the reciprocal of the gap between the 9 and the smallest tame fusible number above it, which is shown here to be at least \$f_{\varepsilon_0}(2)\$ in the fast-growing hierarchy, thus beating Graham's number.

Pyth, 24 bytes

DlHR?<HZ_H/l-Hl-H12)/1lT

Same algorithm as before. I changed the argument 9 to 10 (which due to the predefined variable T doesn't increase the length), thus making the output at least \$f_{\varepsilon_0}(3)\$, absolutely dominating Graham's number and every other answer so far.

\$\endgroup\$
4
  • \$\begingroup\$ This is very cool! I think you can also make a great submission to Write a program whose nontermination is independent of Peano arithmetic based the termination of M for all natural numbers. (Edit: Or not, the "for all" quantifier goes the wrong way.) \$\endgroup\$
    – xnor
    Sep 22, 2021 at 23:21
  • \$\begingroup\$ By the way you can golf the base case to -x*(x<0)or ... \$\endgroup\$
    – xnor
    Sep 22, 2021 at 23:30
  • \$\begingroup\$ You don't need to take the reciprocal, because it's just the total number of output bytes that matters. \$\endgroup\$
    – r.e.s.
    Sep 23, 2021 at 5:58
  • 1
    \$\begingroup\$ This program is an infinite loop due to floating point precision. While Python ints are arbitrarily long, Python floats are not. We get m(9) = m(9 - m(8 - m(7 - m(6 - m(5 - m(4 - m(2.999023318232446)/2/2/2/2)/2)/2)/2)/2)/2)/2, which then recurses forever at m(2.999023318232446) = m(2.999023318232446 - 1.1102230246251565e-16)/2 = m(2.999023318232446)/2. Consider using the fractions module or pairs of ints. \$\endgroup\$ Nov 28, 2021 at 21:32
4
\$\begingroup\$

Binary Lambda Calculus, 78 bits = 9.75 bytes

Here is an expression in Binary Lambda Calculus that is less than 10 bytes, yet surpasses Graham's Number. This is over 5x shorter than my previous post in JavaScript. Since Binary Lambda Calculus is radically different than JavaScript, I decided to answer this in a separate post.

010101000001110011101000000101011000000101101101011010000110100000011100111010

You can find some interpreters here.

Explanation I made on another post

Essentially, this binary expression encodes this lambda calculus expression:

(\f x.f (f x))(\f n.n (\g m.m g m) f n)(\x.x x)(\f x.f (f x))

The decodings can be described like this:

  • (\f x.f (f x)) at the start and at the end corresponds to the Church Numeral \$2\$.
  • (\f n.n (\g m.m g m) f n) takes in a function \$f\$ and a number \$n\$, and returns \$f_n (n)\$ where \$f_0 (n) = f(n)\$ and \$f_{m+1} (n) = f_m^n (n)\$ using functional recursion. Let's call this function \$S\$.
  • (\x.x x) corresponds to the function that raises a number to the power of itself, or \$f(x)=x^x\$.

Since Church Numerals naturally nest the function that they apply to, the lambda calculus string gets reduced as follows: $$2Sf2 \rightarrow S(Sf)2$$ Since each \$S\$ adds \$\omega\$ to the growth rate of the function, and the \$f(x)=x^x\$ has growth rate \$f_2\$ in the Fast Growing Hierarchy, then \$(Sf)\$ has growth rate \$f_{\omega}\$, and \$S(Sf) = 2Sf\$ has growth rate \$f_{\omega2}\$.

This means the expression \$2Sf2\$ described above has the value of about \$f_{\omega2} (2)\$ in the Fast-growing Hierarchy. This is greater than Graham's Number.

Therefore, Graham's Number can be beaten by a program that less than 10 bytes!

\$\endgroup\$
2
  • 2
    \$\begingroup\$ You can remove 1 byte by doing (\t. t S f t) 2 -> 2 S f 2 at the beginning. The 70 bits: 0100010101100000010101100000010110110101101000011010100000011100111010 \$\endgroup\$
    – 2014MELO03
    Jun 1, 2021 at 1:02
  • 1
    \$\begingroup\$ 63 bits: 010001010110010110000000010101101110110101010100000011100111010. This encodes (\t.t (t (\h g m.m h g m) t) t t) (\f x.f (f x)) and becomes 2 (\f n.n (\g m.m 2 g m) f n) (\x.2 x) 2, which is greater than 2 S f 2. \$\endgroup\$
    – 2014MELO03
    Aug 24, 2021 at 19:48
4
\$\begingroup\$

Javascript, 50 Bytes, \$f_{\omega+8} (9)\$

n=(y,x=9)=>y?y-9?n(y-1,x?n(y,x-1)*9:9):x:n(x);n(8)

Here, n is a binary function. The function is defined as followed:

  • n(9,x) = x
  • n(y,0) = n(y-1,9)
  • n(y,x) = n(y-1,n(y,x-1)*9)

The *9 ensures that n(y,x) is an increasing function with respect to x.

  • n(9,x) = x
  • n(10,x) = 9^(x+1)
  • n(11,x) ~ 9^^x
  • n(12,x) ~ 9^^^x
  • n(y+1,x) recurses over n(y,x) with respect to x.

We also defined n(y)=n(y,9), so n(y) grows as fast as \$f_{\omega}\$ in the Fast Growing Hierarchy.

Now here is the twist:

  • n(0,x)=n(x)=n(x,9)

This starts an entirely new hierarchy starting from y=0 all the way up to y=8.

  • n(0,x) grows at \$f_{\omega}\$ in the FGH
  • n(1,x) grows at \$f_{\omega+1} = G\$
  • n(2,x) grows at \$f_{\omega+2}\$
  • ...
  • n(8,x) grows at \$f_{\omega+8}\$

Therefore, n(8) ~ \$f_{\omega+8} (9) > G_{64}\$

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

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))
\$\endgroup\$
7
  • \$\begingroup\$ output to stdout? also, you need a return statement or a lambda. \$\endgroup\$
    – boothby
    Jun 22, 2012 at 5:43
  • 7
    \$\begingroup\$ Also 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. \$\endgroup\$
    – breadbox
    Jun 22, 2012 at 6:19
  • \$\begingroup\$ 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).) \$\endgroup\$
    – r.e.s.
    Jun 22, 2012 at 12:18
  • \$\begingroup\$ @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. \$\endgroup\$
    – r.e.s.
    Jun 22, 2012 at 12:46
  • 1
    \$\begingroup\$ Its'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. \$\endgroup\$
    – r.e.s.
    Jun 22, 2012 at 13:51
3
\$\begingroup\$

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.

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

GolfScript, 24 20 18 bytes

"`'1$,*~'+"{.~~})*

Try it online!

"`'1$,*~'+"         # Push this string
           {.~~})*  # Becomes {.~}126* and this duplicates and executes the string 126 times

When executed, this string modifies the string at the top of the stack:

 `          # Parse it to a string inside the string  'foo'  -> '"foo"'
  '1$,*~'+  # Add '1$,*~' at the end                  '"foo"' -> '"foo"1$,*~'

When we execute this new string it does whatever foo used to do, but many times.

     1$,     # Get the number of bytes of the string at the top of the stack
"foo"   *    # Make that many copies of foo
         ~   # Execute all of them

We will be dealing with only one string, that will modify itself 126 times. Let L be the number of loops and B the number of bytes the string has. These values can be calculated with \$B=2^{L+1}+3L+7\$, but for simplicity's sake we will round B down to L. The number of bytes grows exponentially because every time a loop is added, every " becomes \" and every \ becomes \\. Now the loops execute L times instead of B. When the string has no loops it just adds " "1$,*~ around itself, in other words L = L + 1. With a loops it executes L times the version with a-1 loops. This is exactly the same definition of f in the fast growing hierarchy for when a is a successor:

$$f_0(L)=L+1$$ $$f_a(L)=f_{a-1}^L(L)$$

Before each execution a and L are the same number, so every time we execute the string, L becomes \$f_\omega(L)\$.

We execute it 126 times starting with L = 0, so the number of bytes in the output will be:

$$f_\omega^{126}(0)=f_\omega^{122}(f_8(8))>f_\omega^{122}(122)=f_{\omega+1}(122)$$

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

Python 3, 174 171 168 148 125 117 116 112 101 bytes

3^^^...^3 with Graham's number arrows i.e. G(65) (boring, I know)

a=lambda b,c:3if b<2else(a(a(b-1,c),c-1)if c else 3*b)
G=lambda k:a(3,G(k-1))if k else 4
print(G(65))

Pretty human-readable. a implements arrow notation, G is Graham's sequence.

Improvements

  • Replaced G(64)+1 with G(65), saving two bytes
  • Replaced if c==1:return a**b with if c==0:return a*b, saving one byte
  • Renamed ar function to a and renamed variable a to k, saving three bytes
  • Turned G function from a proper function into a lambda, saving twenty bytes, believe it or not!
  • Turned a function from a proper function also into a lambda, saving twenty-three bytes!
  • Removed k from a function since we will only be using it with base 3, saving eight bytes.
  • Removed an unnecessary space
  • Removed some more unnecessary spaces, saving four bytes
  • Shortened definitions of a and G, saving eleven bytes
\$\endgroup\$
2
  • \$\begingroup\$ You can save 7 bytes with a=lambda b,c:3if b<2else(a(a(b-1,c),c-1)if c else 3*b), and another 4 bytes with G=lambda k:a(3,G(k-1))if k else 4. \$\endgroup\$
    – r.e.s.
    Nov 28, 2021 at 17:21
  • \$\begingroup\$ Thank you so much! I will definitely do that. \$\endgroup\$
    – Binary198
    Nov 28, 2021 at 18:23
0
\$\begingroup\$

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.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Your 74ish byte solution does not produce an output nearly large enough. \$\endgroup\$
    – lirtosiast
    Dec 5, 2015 at 2:08
0
\$\begingroup\$

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)
\$\endgroup\$
0
\$\begingroup\$

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

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

Ruby, 60 bytes

f=->n{n.times{eval("n.times{"*n+"n+=1"+"}"*n)};n};f.call(64)

This is \$f_{\omega+1}(64)\$, exactly, which is slightly bigger than Graham's number :) I copied this from Simply Beautiful Art on their Bignum Bakeoff post.

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

Functoid, 14 bytes

B"W(BiCA])9.Ef

Try it online!

The pointer wraps around whenever it moves out of the source code, this happens once here, making the code equivalent to B"W(BiCA])9.EfB"W(BiCA])9.Ef. The string "W(BiCA])9.EfB" gets converted to base 10 and becomes "91772447243986". Now we can rewrite the code as BkW(BiCA])9.Ef, where k represents that big number.

BkW(BiCA])9         # set the current function to B k W (B i C A ]) 9
           .        # evaluate and print the current function as a number
            Ef      # terminate the program
            

Let f_a be a lambda function equivalent to \$f_a\$ in the fast growing hierarchy. To increase the index by 1, the function needs to be applied to itself n times \$f_a^n(n) = f_{a+1}(n)\$. In code this would be f_(a+1) n = n f_a n = C A f_a n. To get any finite index we just need to apply C A a lot of times to f_0 = ]:

f_0 = ]
f_1 = C A ]
f_2 = C A (C A ])
f_3 = C A (C A (C A ]))
...

\$f_\omega(n) = f_n(n)\$ = n (C A) ] n = W (i (C A) ]) n

The expression B k W (B i C A ]) 9 can be reduced to k (W (i (C A) ])) 9 = k f_ω 9 and this returns \$f_\omega^{91772447243986}(9)\$.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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