Comments on: Follow-up: Graham’s Number http://mathfactor.uark.edu/2007/04/follow-up-grahams-number/ The Math Factor Podcast Site Fri, 08 Aug 2014 12:52:06 +0000 hourly 1 https://wordpress.org/?v=4.9.25 By: nklein http://mathfactor.uark.edu/2007/04/follow-up-grahams-number/comment-page-1/#comment-51 Fri, 13 Apr 2007 14:51:10 +0000 http://mathfactor.uark.edu/2007/04/13/follow-up-grahams-number/#comment-51 Here are some snippets of Smalltalk code that one could add to the Integer class to compute the numbers in the Graham number sequence. And, since Smalltalk already uses arbitrary-precision integers, you should be down the road:

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.

]]>
By: rmjarvis http://mathfactor.uark.edu/2007/04/follow-up-grahams-number/comment-page-1/#comment-50 Fri, 13 Apr 2007 14:43:25 +0000 http://mathfactor.uark.edu/2007/04/13/follow-up-grahams-number/#comment-50 Here is a C progam that would print out Graham’s number if it could store infinitely large integers:

#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.)

]]>