Graham's number ends in a 7. It is a massive number, in theory requiring more information to store than the size of the universe itself. However it is possible to calculate the last few digits of Graham's number.

The last few digits are:


Your program may not contain these (or similar numbers), but must calculate them. It must calculate 200 digits or more.

Output to stdout. Running time of a maximum of 2 minutes on decent hardware. Shortest program wins.

  • \$\begingroup\$ How many digits should be print? \$\endgroup\$ – Dogbert Feb 8 '11 at 19:54
  • \$\begingroup\$ @Dogbert D'oh. I missed that. 200 or more would be fine. \$\endgroup\$ – Thomas O Feb 8 '11 at 20:31
  • \$\begingroup\$ Ruby won't even calculate 3**7625597484987 whereas Python does :) \$\endgroup\$ – gnibbler Feb 8 '11 at 22:36
  • \$\begingroup\$ @gnibbler, umm how? the result would have more than 3 trillion digits. \$\endgroup\$ – Dogbert Feb 9 '11 at 9:53
  • 1
    \$\begingroup\$ @Dogbert, given enough memory and time Python will go ahead an calculate it using it's longs. Ruby won't even do 3**5000000. seems to have some sort of limit in there \$\endgroup\$ – gnibbler Feb 9 '11 at 12:27

dc - 21 chars


This takes about a minute on my computer, and would take lots longer for values larger than 200. It doesn't output leading zeroes.

Here's a slightly longer but faster version (26 chars):

3[3rAz^|dz202>x]dsxxAz3-^%p # or an extra character for a few less iterations

Haskell, 99

Performance not stellar, but it manages to compute 500 digits in a minute on my decade-old hardware.

f a b|b==0=1|odd b=mod(a*f a(b-1))m|0<1=f(mod(a^2)m)$div b 2
main=print$iterate(f 3)3!!500

(btw, I'd love to hear about its performance on more modern hardware)

  • \$\begingroup\$ Takes about 19 seconds to run on my PC. On a side note, this doesn't print a leading 0 before the output. \$\endgroup\$ – Dogbert Feb 8 '11 at 21:29
  • \$\begingroup\$ Yup, it's buggy on all digit counts with leading zeros. Just compute for 501 ;-) Thanks for the benchmark. Did you run it interpreted or compiled? \$\endgroup\$ – J B Feb 8 '11 at 21:32
  • \$\begingroup\$ I compiled it with ghc -o g.exe g.hs. Not sure if that's the best way to compile. \$\endgroup\$ – Dogbert Feb 8 '11 at 21:34
  • \$\begingroup\$ I just ran ghc -O3 graham.hs The recommended badass options from the online doc seem to be -O2 -fvia-C. (and it looks like my GHC is a few releases behind already) \$\endgroup\$ – J B Feb 8 '11 at 21:41
  • \$\begingroup\$ It seems to be running at the same speed with both -O3 and -O2 -fvia-C, in around 18.3 seconds. \$\endgroup\$ – Dogbert Feb 8 '11 at 23:15

Python - 41 chars

499 digits

x=3;exec'x=pow(3,x,10**500);'*500;print x

500 digits

  • 1
    \$\begingroup\$ You are using the knowledge that the 500th digit from the back is a 0. It would give the wrong answer for, say, 200. \$\endgroup\$ – user475 Feb 9 '11 at 8:16
  • 1
    \$\begingroup\$ @Tim The problem asks for "200 digits or more". Just hardcode a count that works and be done with it. (or leave it as such: it prints 499 digits and that's good enough for the question as asked) \$\endgroup\$ – J B Feb 9 '11 at 8:51
  • \$\begingroup\$ @J B: Sure, I'd be satisfied with the 499 if the 0 was left out. Now, however, it assumes that a specific digit is 0. \$\endgroup\$ – user475 Feb 9 '11 at 9:01
  • \$\begingroup\$ @user475 - By the properties of power-towers, if you are calculating the last (d) digits, and the result is less than (d) digits, then the missing digits (on the left) must be "0's". So it's OK to add the missing "0" digit, but it should be done by examining the length of the result and adding the appropriate number of "0's". \$\endgroup\$ – Kevin Fegan Jul 14 '13 at 20:07

Python - 62 59 55 chars

for i in range(500):x=pow(3,x,10**500)

Takes around 12 seconds on my PC.

sh-3.1$ time python cg_graham.py

real    0m11.807s
user    0m0.000s
sys     0m0.015s
  • 3
    \$\begingroup\$ Native powmod is a killer :-) \$\endgroup\$ – J B Feb 8 '11 at 21:24
  • \$\begingroup\$ You can use 10**500 \$\endgroup\$ – gnibbler Feb 8 '11 at 22:50
  • \$\begingroup\$ @J B, that's the only reason I used Python for this entry :) \$\endgroup\$ – Dogbert Feb 8 '11 at 23:22
  • \$\begingroup\$ @gnibbler, updated, thanks! I'm new to Python :) \$\endgroup\$ – Dogbert Feb 8 '11 at 23:23

Axiom, 63 bytes

f()==(r:=3;d:=10^203;for i in 1..203 repeat r:=powmod(3,r,d);r)

ungolf and result

--This find the Graham's number follow the algo found in wiki
   r:=3; d:=10^203
   for i in 1..203 repeat r:=powmod(3,r,d)

(3) -> a:=f()::String
                                                             Type: String
(4) -> #a
   (4)  203
                                                    Type: PositiveInteger

#a=203 means the number len is >200 it menas too that it not has some 0 first...


Headsecks, 602 bytes

(h@HP0&Y+h8h (hx0RWc@4vYcx#@#PJ"B?[#CPx (h Lx$2(pl2YL;KD:T{9$2j<
 LSSh,ZT l2I<Pp,@4SX`,:xtc@T",($)<cKT\lbBAy44,dQl[yL"l+i,;9<*j0P
|)lD[+`\RBi!< LaD(LHPLyt{{@\iADRQdHTZQIT3[X`DB*`X$Cxh$*(T0$| ,[;
4RDK,A!<X \cqLZ" h,kHp|qLRQIDh,+StZbL+{(|jqqL;9L"(xd"<s$8\:x,CY\
z0T[,(XdcxlbaD*D;+tDj\JIi4k[)LPDLBzP@DSc$jL $s4BjQ19|;!)|9t;TaQA
dLzit[0@l2!)I$,0P@$y<L4pLPLStQT"!a| $+8(DZ`00t ,RtX4C@$yQ11|KI\"
`|2<k3|R(Dyi<)LshTrzQ$sp D+DRbH|Q$CqT0D;AA\jdXd"ppdK3LzZl#\Bl`@t

Prints the last 200 digits.

Please remove the newlines before running.

  • \$\begingroup\$ How are we supposed to run it? \$\endgroup\$ – caird coinheringaahing Jun 17 '17 at 3:12
  • \$\begingroup\$ Absolutely no idea (I just translated this from BF). But I searched "headsecks" on github and it looks like there are a few implementations (though the reference implementation link seems to be dead). \$\endgroup\$ – Esolanging Fruit Jun 17 '17 at 3:20

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.