For the following question, all what is needed to know about Graham's number is that it is a power tower with many many many $3's$
Consider the following pseudocode :
input n
Start with $s=1$ and $p=7$ (the last digit of $3^3$)
Repeat
$s=s+1$
$p=3^p$ modulo $10^s$
Until $s=n$
output p
Questions :
- Does this algorithm return the $n$ last digits of Graham's number ?
- If I take another base and $p$ happens to get smaller than $s$. Do I have to add $\lambda(10^s)$ to $p$ ?
- Can I calculate the $n$ last digits of Graham's number (or another tetrated number) easier ?
Yes, this algorithm will return the last $n$ digits of Graham's number, and as far as I know it is the simplest way of doing so.
As for when $p$ gets smaller than $s$, you don't have to worry if the base of your power tower is relatively prime to 10. However, if your base does contain factors of 2 or 5, then you could theoretically have a problem if $p < s$; say if the base were 2, then if $p < s$ the next number $2^p$ would not be divisible by $2^s$ as it should be. However, it seems to me that this will never happen; if your base is divisible by 2, then each calculation of $p$ will result in a number divisible by $2^s$, and therefore will not be less than $s$. Similarly for when the base is divisible by 5. So even in such cases, you don't need to worry.