graham

self < 1

ifTrue: [ self error: ‘Only supports positive graham indexes.’ ].

self = 1

ifTrue: [ ^ 3 arrow: 3 arrowCount: 4. ].

^ 3 arrow: 3 arrowCount: ( ( self-1 ) graham ).

and then the arrow notation method:

arrow: power arrowCount: count

power < 0

ifTrue: [ self error: ‘Negative power is a problem.’. ].

count < 1

ifTrue: [ self error: ‘Must have at least one arrow.’. ].

power = 0

ifTrue: [ ^ 1. ].

count = 1

ifTrue: [ ^ self * ( self arrow: (power-1) arrowCount: count ). ].

^ self arrow: ( self arrow: (power-1) arrowCount: count ) arrowCount: (count-1).

So, to get the first graham number, one would do: `1 graham`

. However, you best be able to interrupt it, because even the first number in the series is immense.

Anyhow, that’s 16 lines of code… 6 of which are error checking.

#include

int f(int a, int b, int c)

{

int i,x;

if(c==1) for(x=1;b>0;–b) x *= a;

else {

x = f(a,b,–c);

for(i=c;i>0;–i) x = f(a,x,c);

}

return x;

}

int g(int n)

{

return n==1 ? f(3,3,4) : f(3,3,g(n-1));

}

int main()

{

return printf(“%d\n”,g(64));

}

Basically, g(n) is the nth number in Graham’s sequence, for which g(64) is Graham’s number. f(a,b,c) is a^^^…^^^b where there are c ^’s in there.

It’s probably a bit longer than the Fortran version, since C doesn’t have any native integer exponent function, so I had to write my own (the c==1 case at the start of the f function.)

]]>