Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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.

share|improve this question
3  
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. –  raptortech97 Aug 8 at 15:12
3  
@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 Aug 8 at 15:15
3  
The ellipses represent numbers of arrows that aren't even representable without using up-arrow notation –  Eric Tressler Aug 8 at 15:16
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 Aug 8 at 15:58
8  
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. –  comperendinous Aug 8 at 18:18

15 Answers 15

up vote 2 down vote accepted

CJam - 64

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

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

share|improve this answer

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

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.

share|improve this answer
    
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 Aug 8 at 18:10
1  
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! –  chilemagic Aug 8 at 20:05

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
share|improve this answer
1  
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". –  comperendinous Aug 8 at 20:04
    
@comperendinous Good point, thanks. –  Ray Aug 8 at 20:16
    
You can replace "\\"++ with '\\': –  proud haskeller Aug 10 at 1:45

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,'')))
share|improve this answer

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  -
$ 
share|improve this answer

Ruby - 105

(You need ruby >= 1.9)

Golfed:

s=?\s;$><<s*63.times{|i|j=63-i;puts s*i+"3^^.#{?.*j*2}^3",s*i+s+?\\+?_*j+s+s+?_*j+?/,s*65+"\\/"}+"3^^^^3"

Ungolfed (out of sync):

63.times do |i|
  j=63-i
  puts ' ' * i + "3^^." + ".." * j + "^3"
  puts ' ' * (i + 1) + "\\" + "_" * j+ '  ' + "_" * j + "/"
  puts ' ' * 65 + "\\/"
end
puts ' ' * 63 + "3^^^^3"

EDIT: Reduced from 121 to 118

EDIT 2: Reduced from 118 to 117

EDIT 3: Reduced from 117 to 113 !

EDIT 4: @killmous: Fair game, 113 -> 109 :)

EDIT 5: 109 -> 106, no trailing newline.

EDIT 6: 106 -> 105

share|improve this answer

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.

share|improve this answer

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"
share|improve this answer

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

share|improve this answer

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");
share|improve this answer

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");
  }
}
share|improve this answer

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))
share|improve this answer

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;
share|improve this answer
    
Only changing the output to console you can save up to 11 characters (&nbsp;->' ', <br>->\n) –  avall Aug 20 at 10:00
    
Also, you must add <? at the start of the code - it must be a valid program, not a function. –  avall Aug 20 at 10:01
    
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 Aug 20 at 10:25
    
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(6‌​5-$i--)."\\$b".a(2)."$b/\n".a(65)."\/\n";}$v.=a(63)."3^^^^3";echo$v; –  avall Aug 20 at 10:33
    
md5 check: php grahams.php | md5sum : c84d0e20eac2a7f891ab42a8eb77759f - –  avall Aug 20 at 10:35

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")
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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