Output Graham's Number

16

Write a program that outputs all 64 layers of this representation of Graham's number from Wikipedia:

  Graham's number

Your output must exactly match this ASCII version (in which ^ represents Knuth's up-arrows and combinations of slashes and underscores represent the large horizontal curly brackets):

3^^...............................................................................................................................^3
 \_______________________________________________________________  _______________________________________________________________/
                                                                 \/
 3^^.............................................................................................................................^3
  \______________________________________________________________  ______________________________________________________________/
                                                                 \/
  3^^...........................................................................................................................^3
   \_____________________________________________________________  _____________________________________________________________/
                                                                 \/
   3^^.........................................................................................................................^3
    \____________________________________________________________  ____________________________________________________________/
                                                                 \/
    3^^.......................................................................................................................^3
     \___________________________________________________________  ___________________________________________________________/
                                                                 \/
     3^^.....................................................................................................................^3
      \__________________________________________________________  __________________________________________________________/
                                                                 \/
      3^^...................................................................................................................^3
       \_________________________________________________________  _________________________________________________________/
                                                                 \/
       3^^.................................................................................................................^3
        \________________________________________________________  ________________________________________________________/
                                                                 \/
        3^^...............................................................................................................^3
         \_______________________________________________________  _______________________________________________________/
                                                                 \/
         3^^.............................................................................................................^3
          \______________________________________________________  ______________________________________________________/
                                                                 \/
          3^^...........................................................................................................^3
           \_____________________________________________________  _____________________________________________________/
                                                                 \/
           3^^.........................................................................................................^3
            \____________________________________________________  ____________________________________________________/
                                                                 \/
            3^^.......................................................................................................^3
             \___________________________________________________  ___________________________________________________/
                                                                 \/
             3^^.....................................................................................................^3
              \__________________________________________________  __________________________________________________/
                                                                 \/
              3^^...................................................................................................^3
               \_________________________________________________  _________________________________________________/
                                                                 \/
               3^^.................................................................................................^3
                \________________________________________________  ________________________________________________/
                                                                 \/
                3^^...............................................................................................^3
                 \_______________________________________________  _______________________________________________/
                                                                 \/
                 3^^.............................................................................................^3
                  \______________________________________________  ______________________________________________/
                                                                 \/
                  3^^...........................................................................................^3
                   \_____________________________________________  _____________________________________________/
                                                                 \/
                   3^^.........................................................................................^3
                    \____________________________________________  ____________________________________________/
                                                                 \/
                    3^^.......................................................................................^3
                     \___________________________________________  ___________________________________________/
                                                                 \/
                     3^^.....................................................................................^3
                      \__________________________________________  __________________________________________/
                                                                 \/
                      3^^...................................................................................^3
                       \_________________________________________  _________________________________________/
                                                                 \/
                       3^^.................................................................................^3
                        \________________________________________  ________________________________________/
                                                                 \/
                        3^^...............................................................................^3
                         \_______________________________________  _______________________________________/
                                                                 \/
                         3^^.............................................................................^3
                          \______________________________________  ______________________________________/
                                                                 \/
                          3^^...........................................................................^3
                           \_____________________________________  _____________________________________/
                                                                 \/
                           3^^.........................................................................^3
                            \____________________________________  ____________________________________/
                                                                 \/
                            3^^.......................................................................^3
                             \___________________________________  ___________________________________/
                                                                 \/
                             3^^.....................................................................^3
                              \__________________________________  __________________________________/
                                                                 \/
                              3^^...................................................................^3
                               \_________________________________  _________________________________/
                                                                 \/
                               3^^.................................................................^3
                                \________________________________  ________________________________/
                                                                 \/
                                3^^...............................................................^3
                                 \_______________________________  _______________________________/
                                                                 \/
                                 3^^.............................................................^3
                                  \______________________________  ______________________________/
                                                                 \/
                                  3^^...........................................................^3
                                   \_____________________________  _____________________________/
                                                                 \/
                                   3^^.........................................................^3
                                    \____________________________  ____________________________/
                                                                 \/
                                    3^^.......................................................^3
                                     \___________________________  ___________________________/
                                                                 \/
                                     3^^.....................................................^3
                                      \__________________________  __________________________/
                                                                 \/
                                      3^^...................................................^3
                                       \_________________________  _________________________/
                                                                 \/
                                       3^^.................................................^3
                                        \________________________  ________________________/
                                                                 \/
                                        3^^...............................................^3
                                         \_______________________  _______________________/
                                                                 \/
                                         3^^.............................................^3
                                          \______________________  ______________________/
                                                                 \/
                                          3^^...........................................^3
                                           \_____________________  _____________________/
                                                                 \/
                                           3^^.........................................^3
                                            \____________________  ____________________/
                                                                 \/
                                            3^^.......................................^3
                                             \___________________  ___________________/
                                                                 \/
                                             3^^.....................................^3
                                              \__________________  __________________/
                                                                 \/
                                              3^^...................................^3
                                               \_________________  _________________/
                                                                 \/
                                               3^^.................................^3
                                                \________________  ________________/
                                                                 \/
                                                3^^...............................^3
                                                 \_______________  _______________/
                                                                 \/
                                                 3^^.............................^3
                                                  \______________  ______________/
                                                                 \/
                                                  3^^...........................^3
                                                   \_____________  _____________/
                                                                 \/
                                                   3^^.........................^3
                                                    \____________  ____________/
                                                                 \/
                                                    3^^.......................^3
                                                     \___________  ___________/
                                                                 \/
                                                     3^^.....................^3
                                                      \__________  __________/
                                                                 \/
                                                      3^^...................^3
                                                       \_________  _________/
                                                                 \/
                                                       3^^.................^3
                                                        \________  ________/
                                                                 \/
                                                        3^^...............^3
                                                         \_______  _______/
                                                                 \/
                                                         3^^.............^3
                                                          \______  ______/
                                                                 \/
                                                          3^^...........^3
                                                           \_____  _____/
                                                                 \/
                                                           3^^.........^3
                                                            \____  ____/
                                                                 \/
                                                            3^^.......^3
                                                             \___  ___/
                                                                 \/
                                                             3^^.....^3
                                                              \__  __/
                                                                 \/
                                                              3^^...^3
                                                               \_  _/
                                                                 \/
                                                               3^^^^3

Notice that there are 64 layers, that there are no trailing spaces, and that two ^ occur before the dots and only one occurs after.

The MD5 hash of your output should precisely be c84d0e20eac2a7f891ab42a8eb77759f, or 1ad3b10d06e38693436c14958b2c47a8 if there is a trailing newline (which is ok).

The shortest code in bytes wins. Output to stdout. There is no input.

Calvin's Hobbies

Posted 2014-08-08T13:59:52.150

Reputation: 84 000

3Why are they dots and not more up-arrows? I'm pretty sure the purpose of the ellipses in the picture was just to show the missing arrows. – raptortech97 – 2014-08-08T15:12:16.430

4@raptortech97 Because there's a LOT of missing arrows. Graham's number is bloody massive. I'm hnonestly curious to see what languages can actually handle it. – fuandon – 2014-08-08T15:15:31.433

4The ellipses represent numbers of arrows that aren't even representable without using up-arrow notation – Eric Tressler – 2014-08-08T15:16:45.543

Ah... yeah I just read the Wikipedia page and I get it now. – raptortech97 – 2014-08-08T15:17:36.363

2@fuandon You won't get any language to actually compute the number, even if you allowed for a universe sized computer, it's just plain too big. I suppose you could calculate an arbitrarily large amount the rightmost digits, but that just isn't the same. – Tally – 2014-08-08T15:58:23.633

Would this be considered [tag:ascii-art]? – NinjaBearMonkey – 2014-08-08T16:31:37.060

10It might be worth giving out the MD5 hashes for quick verification: 1ad3b10d06e38693436c14958b2c47a8 as given by grc, or c84d0e20eac2a7f891ab42a8eb77759f if the final newline character can be omitted. – comperendinous – 2014-08-08T18:18:13.463

I believe Graham's number requires more electrons than exists in the universe to represent in binary therefore languages like Tcl and Ruby can do it in theory because the specifications of the language doesn't limit the size of integers but practically you'd run out of RAM. – slebetman – 2014-08-09T00:31:09.310

Of course, the much more interesting task of actually computing Graham's number (assuming super-naturally memory resources etc.) has been covered elsewhere already.

– ceased to turn counterclockwis – 2014-08-09T10:46:06.963

1I am a bit disappointed with the expected output. Would be a nicer challenge, if the program was expected to output the actual number. As @slebetman pointed out, there are languages, which could do it. (Implementation limitations prevent the program from actually running to completion, but the program can still be written, and sizes can be compared.) – kasperd – 2014-08-09T23:55:59.427

@kasperd There is already a challenge like that.

– Calvin's Hobbies – 2014-08-10T00:33:07.493

Answers

4

CJam - 64

63{SI*3'^_'._63I-:J*_'^3NSI)*'\'_J*_S_@'/NS65*"\/
"}fIS63*3'^4*3

Try it at http://cjam.aditsu.net/

aditsu quit because SE is EVIL

Posted 2014-08-08T13:59:52.150

Reputation: 22 326

9

Python, 122

i=63
while i:
 for s in'3^^'+'..'*i+'.^3','\%s  %s/'%(('_'*i,)*2),'\/':print' '*(66-len(s)/2)+s
 i-=1
print' '*63+'3^^^^3'

Example:

$ python grahams.py | md5
1ad3b10d06e38693436c14958b2c47a8

grc

Posted 2014-08-08T13:59:52.150

Reputation: 18 565

What does that output mean? – fuandon – 2014-08-08T15:17:24.850

13It means he didn't want to spam the answers with a reproduction of the huge ascii output above, I think. – Eric Tressler – 2014-08-08T15:18:01.830

1Ah, fair enough. I get it now, result was hashed. – fuandon – 2014-08-08T15:20:26.280

Nice replacement of a for loop with a while loop. – qwr – 2014-08-09T02:31:44.327

8

Perl -- 113 (112 + 1 for -E)


Golfed:

$.=$"x63,$y="_"x($!=63-$_),$x.=($z=$"x$_)."3^^".".."x$!.".^3$/ $z\\$y  $y/$/ $. \\/$/"for 0..62;say"$x$.3^^^^3"

Ungolfed:

for(0..62) {
    $.=$"x63;
    $!=63-$_;
    $y="_"x$_;
    $z=$"x$!;
    $x.=$z."3^^".".."x$!.".^3$/ $z\\$y  $y/$/ $. \\/$/";
}
say"$x$.3^^^^3"

EDIT: There was an error in string interpolation where perl thought $m3 was a variable, so I had to add 2 characters to fix that. Solved by changing $m to $.

NINJA EDIT: Replaced instances of " " with $". Saves 2 characters

NINJA EDIT 2: Thanks @chilemagic for pointing out that I can use say instead of print with -E to save a character.

NOT-SO-NINJA EDIT 3: @Nax's solution beat my old solution, so I buffed it up a little to hold its ground.

killmous

Posted 2014-08-08T13:59:52.150

Reputation: 369

I was literally just about to post a Perl answer (a few characters longer, though) when yours shifted the page down. I like how you used $m inside and outside the loop to reduce the redundancy. – comperendinous – 2014-08-08T18:10:13.280

1You can use say instead of print and then run it with -E instead of -e (if your Perl version is high enough). Looks great though! – hmatt1 – 2014-08-08T20:05:12.587

7

Haskell, 152

(?)=replicate
g 0=["3^^^^3"]
g i=("3^^"++(i*2)?'.'++".^3"):map(' ':)(("\\"++i?'_'++"  "++i?'_'++"/"):((i+1)?' '++"\\/"):g(i-1))
main=putStr.unlines.g$63

Ray

Posted 2014-08-08T13:59:52.150

Reputation: 1 946

1Damn it! That's twice I've been ninja'd by seconds. I think I might save you a character, though, if you remove +1 and just add the dot before "^3". – comperendinous – 2014-08-08T20:04:46.450

@comperendinous Good point, thanks. – Ray – 2014-08-08T20:16:23.843

You can replace "\"++ with '\': – proud haskeller – 2014-08-10T01:45:43.400

4

C 161 182

(i think i maxed out this method, maybe i should find a new one that can be golfed down more)
compile as C99!

main(i){for(char d[]={[0 ...126]=46,i=0},l[]={[0 ...62]=95,0};i<63;)printf("3^^%s^3\n%*s\\%s  %s/\n%*s\n%*s",d+2*i++,i,"",l+i,l+i,67,"\\/",i,"");puts("3^^^^3");}

JS 158

r="repeat";o="";u="_";s=" ";(f=i=>i?"3^^"+"."[r](i*2+1)+"^3\n"+s[r](64-i)+"\\"+u[r](i)+s[r](2)+u[r](i)+"/\n"+s[r](65)+"\\/\n"+s[r](64-i)+f(i-1):"3^^^^3")(63);

xem.github.io/obfuscatweet/ 124:

eval(unescape(escape('').replace(/uD./g,'')))

bebe

Posted 2014-08-08T13:59:52.150

Reputation: 3 916

3

Bash+coreutils, 150

I thought I might be able to do better than this. Oh well.

r()(yes "${2:- }"|head -$[$1]|tr -d '
')
for i in {63..1};{
r 63-$i
echo "3^^`r 2*$i .`.^3
`r 64-$i`\\`r $i _`  `r $i _`/
`r 65`\/"
}
r 63
echo 3^^^^3

Output:

$ ./graham.sh | md5sum
1ad3b10d06e38693436c14958b2c47a8  -
$ 

Digital Trauma

Posted 2014-08-08T13:59:52.150

Reputation: 64 644

1

STATA 135

forv x=63(-1)1{
di _d(63-`x')" ""3^^"_d(2*`x'+1)".""^3"_n_d(64-`x')" ""\"_d(`x')"_""  "_d(`x')"_"_n-d(65)" ""\/"
}
di _d(63)"3^^^^3"

bmarks

Posted 2014-08-08T13:59:52.150

Reputation: 2 114

1

JavaScript 179

n='';a=Array;for(b=64;b>1;)n+=(c=a(65-b).join(' '))+'3^^'+a(2*b).join('.')+'^3\n'+c+' \\'+(u=a(b--).join('_'))+'  '+u+'/\n'+(d=a(64).join(' '))+'  \\/\n'
console.log(n+d+'3^^^^3')

Test it in your browser console or at http://jsfiddle.net/2qwvrvcw/.

NinjaBearMonkey

Posted 2014-08-08T13:59:52.150

Reputation: 9 925

1

Javascript (203)

u="_";s=" ";for(i=64;i>1;i--)console.log(s.repeat(64-i)+"3^^"+".".repeat(i*2-1)+"^3\n"+s.repeat(64-i)+"\\"+u.repeat(i)+s.repeat(2)+u.repeat(i)+"/\n"+s.repeat(65)+"\\/");console.log(s.repeat(63)+"3^^^^3")

Ungolfed:

underscore = "_";
space = " ";
for (i = 64; i > 1; i--) console.log(space.repeat(64 - i) + "3^^" + ".".repeat(i * 2 - 1) + "^3\n" + 
                                     space.repeat(64-i) + "\\" + underscore.repeat(i) + space.repeat(2) + underscore.repeat(i) + "/\n" + 
                                     space.repeat(65) + "\\/");
console.log(space.repeat(63) + "3^^^^3");

kitcar2000

Posted 2014-08-08T13:59:52.150

Reputation: 2 689

1

C# (212211)

using C=System.Console;class P{static void Main(){for(int i=64;i>1;)C.Write(@"{0}3^^{2}.^3
{0} \{1}  {1}/
{3,65}\/
",new string(' ',64-i),new string('_',--i),new string('.',i*2),"");C.Write("{0,69}","3^^^^3");}}

.Net Fiddle (it seems to require that the class and Main method are public)

Slightly ungolfed:

using C = System.Console;
class Program {
  static void Main() {
    for (int i = 64; i > 1; ) {
      C.Write("{0}3^^{2}.^3\n{0} \\{1}  {1}/\n{3,65}\\/\n",
 new string(' ', 64 - i),
 new string('_', --i),
 new string('.', i * 2), "");
    }
    C.Write("{0,69}", "3^^^^3");
  }
}

Lauri Helkkula

Posted 2014-08-08T13:59:52.150

Reputation: 11

1

Perl, 116 112 109 108 (including -E)

My first attempt at golfing. Please take it as such:

Golfed:

perl -E'map{$_=" $_!^".(($b=63-$_)?"2.$b.$b.^!
 $_ \\_$b 2_$b/
 65\\/":"4!");s#(.)(\d+)#"$1"x$2#eg;y/!/3/;say}0..63'

Very slightly ungolfed (not maintained ... ):

map
{
    $b = 63 - $_;
    $_ = " $_!^".($b
        ? "2.$b.$b.^!N $_ \\_$b 2_$b/N 65\\/"
        : "4!");
    s#(.)(\d+)#"$1"x$2#eg;    # behaves exactly as $_ =~ s/(.)(\d+)/"$1"x$2/eg
    y/!N/3\n/;                  # y == tr, so this does $_ =~ tr/!N/3\n/
    say;                        # once more $_ completion, this does say $_ == print "$_\n"
} (0..63);

Essentially it works like this: for each line of 3s and a brace (these are handled together), it writes their sequence RLE-encoded, then decodes it. The rule is simple: every char followed by a number is expanded to that char that number of times. Characters without a number are left alone. Finally, the character ! denotes a 3 and a N means a newline (3 had to be encoded because it is number and would eagerly participate as the number of repetitions).

Each line of 3s and its brace get an element in the 64-element array the map is generating. Say, like print, can output arrays -- if given an array, it prints all the elements (without any separators).

I used chilemagic's trick with -E. Thanks for it.

Reinforcements: Two 113 byte solutions started to lurk here. RLE shall not succumb without fight!

Cleansing of Feeds: I have now realized that I have no need for a special transliteration of \n, because all strings in Perl are automagically multiple-lined! Thus RLE clings nearer to the Ruby's back! Now at 109 bytes.

Agonizing Enlightment: To count characters properly is harder than I thought. After some struggling, I settled on

echo $((wc -c <<<'golf'-1))
(-1, because it counts a LF at the very end of the script). Moreover I have rearranged my assignments and saved 1 character, hereby settling on 108 bytes.

Ramillies

Posted 2014-08-08T13:59:52.150

Reputation: 1 923

0

PHP 214

Thought I was doing alright until I counted the characters, anyway here it is -

<?function a($n,$v=" "){for($j=0;$j<$n;$j++){$r.=$v;}return $r;}for($i=64;$i>1;$i--){$b=a($i-1,"_");$v.=a(64-$i)."3^^".a($i*2-1,".")."^3\n".a(64-$i+1)."\\".$b.a(2).$b."/\n".a(65)."\/\n";}$v.=a(63)."3^^^^3";echo $v;

Peter

Posted 2014-08-08T13:59:52.150

Reputation: 225

Only changing the output to console you can save up to 11 characters (&nbsp;->' ', <br>->\n) – avall – 2014-08-20T10:00:23.707

Also, you must add <? at the start of the code - it must be a valid program, not a function. – avall – 2014-08-20T10:01:44.980

Ah right thanks, and the tips you suggested, I was basing mine off the page output as opposed to the source code, (/n won't break the line and two ' ' next to each other still make only 1 space), so I guess the source code should be fine too :) – Peter – 2014-08-20T10:25:04.337

Golfed down to 194 chars: <?function a($n,$v=" "){return str_repeat($v, $n);}for($i=64;$i>1;){$b=a($i-1,"_");$v.=a(64-$i)."3^^".a($i*2-1,".")."^3\n".a(65-$i--)."\\$b".a(2)."$b/\n".a(65)."\/\n";}$v.=a(63)."3^^^^3";echo$v; – avall – 2014-08-20T10:33:37.867

md5 check: php grahams.php | md5sum : c84d0e20eac2a7f891ab42a8eb77759f - – avall – 2014-08-20T10:35:04.727

Another tip to your current code - ?> at the end is unnecessary. – avall – 2014-08-20T10:51:34.657

Ah thanks a lot for all the help, also I meant to say thanks to your two comments before but forgot to hit submit – Peter – 2014-08-20T10:59:49.397

0

MATLAB (325)

m=127:-2:0;n=0:63;p='UniformOutput';q=false;f=@ones;g=@arrayfun;a=g(@(x,y)[f(1,y)*' '  '3^^' f(1,x)*'.'  '^3'],m,n,p,q);b=g(@(x,y)[f(1,y+1)*' '  '\' f(1,(x-1)/2)*'_' '  ' f(1,(x-1)/2)*'_' '/'],m,n,p,q);a{64}(a{64}=='.')='^';g(@(x,y)fprintf('%s\n%s\n%s\n',x{:},y{:},[f(1,65)*' ' '\/']),a,b),fprintf(repmat('\b',1,137))

ungolfed:

m=127:-2:0;
n=0:63;
p='UniformOutput';
q=false;
f=@ones;
g=@arrayfun;
a=g(@(x,y)[f(1,y)*' '  '3^^' f(1,x)*'.'  '^3'],m,n,p,q);
b=g(@(x,y)[f(1,y+1)*' '  '\' f(1,(x-1)/2)*'_' '  ' f(1,(x-1)/2)*'_' '/'],m,n,p,q);
a{64}(a{64}=='.')='^';
g(@(x,y)fprintf('%s\n%s\n%s\n',x{:},y{:},[f(1,65)*' ' '\/']),a,b),fprintf(repmat('\b',1,137))

MATLAB (252)

n=0;f=@ones;g=@arrayfun;for i=127:-2:0 if n~=63 y=f(1,i)*'.';else y='^';z='';end;fprintf('%s\n%s\n%s\n',[f(1,n)*' '  '3^^' y  '^3'],[f(1,n+1)*' '  '\' f(1,(i-1)/2)*'_' '  ' f(1,(i-1)/2)*'_' '/'],[f(1,65)*' ' '\/']);n=n+1;end;fprintf(repmat('\b',1,137))

ungolfed:

n=0;  
f=@ones;
g=@arrayfun;
for i=127:-2:0 
    if n~=63 
        y=f(1,i)*'.';
    else y='^';
        z='';
    end
    fprintf('%s\n%s\n%s\n',[f(1,n)*' '  '3^^' y  '^3'],[f(1,n+1)*' '  '\' f(1,(i-1)/2)*'_' '  ' f(1,(i-1)/2)*'_' '/'],[f(1,65)*' ' '\/']);
    n=n+1;
end
fprintf(repmat('\b',1,137))

Minar Ashiq Tishan

Posted 2014-08-08T13:59:52.150

Reputation: 11

0

Lua - 174

p=print a=" "b="_"for i=0,62 do j=63-i p(a:rep(i).."3^^."..(".."):rep(j).."^3")p(a:rep(i+1).."\\"..b:rep(j).."  "..b:rep(j).."/")p(a:rep(65).."\\/")end p(a:rep(63).."3^^^^3")

Before that, I got two algorithmically different solutions (both 182 !) :

a=" "b="_"for i=0,62 do j=63-i print(a:rep(i).."3^^."..(".."):rep(j).."^3")print(a:rep(i+1).."\\"..b:rep(j).."  "..b:rep(j).."/")print(a:rep(65).."\\/")end print(a:rep(63).."3^^^^3")

a=" "b="_"i=63 while i>0 do c=b:rep(i/2)for _,s in pairs{"3^^."..(".."):rep(i/2).."^3","\\"..c.."  "..c.."/","\\/"}do print(a:rep(66-#s/2)..s)i=i-1 end end print(a:rep(63).."3^^^^3")

Adriweb

Posted 2014-08-08T13:59:52.150

Reputation: 333