16
\$\begingroup\$

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.

\$\endgroup\$
11
  • 3
    \$\begingroup\$ Why 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. \$\endgroup\$ Aug 8, 2014 at 15:12
  • 4
    \$\begingroup\$ @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. \$\endgroup\$
    – fuandon
    Aug 8, 2014 at 15:15
  • 4
    \$\begingroup\$ The ellipses represent numbers of arrows that aren't even representable without using up-arrow notation \$\endgroup\$ Aug 8, 2014 at 15:16
  • 2
    \$\begingroup\$ @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. \$\endgroup\$
    – Tally
    Aug 8, 2014 at 15:58
  • 11
    \$\begingroup\$ It 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. \$\endgroup\$ Aug 8, 2014 at 18:18

14 Answers 14

10
\$\begingroup\$

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
\$\endgroup\$
4
  • \$\begingroup\$ What does that output mean? \$\endgroup\$
    – fuandon
    Aug 8, 2014 at 15:17
  • 13
    \$\begingroup\$ It means he didn't want to spam the answers with a reproduction of the huge ascii output above, I think. \$\endgroup\$ Aug 8, 2014 at 15:18
  • 1
    \$\begingroup\$ Ah, fair enough. I get it now, result was hashed. \$\endgroup\$
    – fuandon
    Aug 8, 2014 at 15:20
  • \$\begingroup\$ Nice replacement of a for loop with a while loop. \$\endgroup\$
    – qwr
    Aug 9, 2014 at 2:31
8
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$ Aug 8, 2014 at 18:10
  • 1
    \$\begingroup\$ You 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! \$\endgroup\$
    – hmatt1
    Aug 8, 2014 at 20:05
7
\$\begingroup\$

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
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Damn 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". \$\endgroup\$ Aug 8, 2014 at 20:04
  • \$\begingroup\$ @comperendinous Good point, thanks. \$\endgroup\$
    – Ray
    Aug 8, 2014 at 20:16
  • \$\begingroup\$ You can replace "\\"++ with '\\': \$\endgroup\$ Aug 10, 2014 at 1:45
4
\$\begingroup\$

CJam - 64

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

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

\$\endgroup\$
4
\$\begingroup\$

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,'')))
\$\endgroup\$
3
\$\begingroup\$

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

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"
\$\endgroup\$
1
\$\begingroup\$

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

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

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");
\$\endgroup\$
1
\$\begingroup\$

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");
  }
}
\$\endgroup\$
1
\$\begingroup\$

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.

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

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

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;
\$\endgroup\$
7
  • \$\begingroup\$ Only changing the output to console you can save up to 11 characters (&nbsp;->' ', <br>->\n) \$\endgroup\$
    – avall
    Aug 20, 2014 at 10:00
  • \$\begingroup\$ Also, you must add <? at the start of the code - it must be a valid program, not a function. \$\endgroup\$
    – avall
    Aug 20, 2014 at 10:01
  • \$\begingroup\$ 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 :) \$\endgroup\$
    – Peter
    Aug 20, 2014 at 10:25
  • \$\begingroup\$ 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; \$\endgroup\$
    – avall
    Aug 20, 2014 at 10:33
  • \$\begingroup\$ md5 check: php grahams.php | md5sum : c84d0e20eac2a7f891ab42a8eb77759f - \$\endgroup\$
    – avall
    Aug 20, 2014 at 10:35
0
\$\begingroup\$

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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