Last few digits of $n^{n^{n^{\cdot^{\cdot^{\cdot^n}}}}}$
I want to compute last few digts (as much as possible ) of the following number $$ N:=n^{n^{n^{\cdot^{\cdot^{\cdot^n}}}}}\!\!\!\hspace{5 mm}\mbox{ if there are $k$ many $n$'s in the expression and $n\in\mathbb{N}$ }$$ I have seen many particular cases of this problem. I think for odd $n$ the units digit is $n^3\mbox{ mod } 10 $ and for even $n$ the units digit is 6, for all $k\geq 3$ . How much can we say about the other digits ?
Solution 1:
Taking $n=7$ and looking for the last three digits for an example, note that $7^m \pmod {1000}$ is periodic with period $20$. You can check this easily with a spreadsheet. So now, we only need the tower above the first $7$ to $\pmod {20}$. That has period $4$, so we only need the tower above the first two $7$'s $\pmod 4$. That has period $2$, and the stack above the bottom three $7$'s is always odd. So a tower of $k\ 7$'s has last three digits the same as $7^{7^7}$ for $ k \ge 4$. The upper $7$ is $3 \pmod 4$, so $7^7 \equiv 3 \pmod {20}$, so $7^{7^7} \equiv 343 \pmod {1000}$, so any taller tower ends in $343$
Solution 2:
Here's a little bit of computational knowledge...
If we want the first $d$ digits, we can calculate the result by modular arithmetic. In other words, modulo $10^d=2^d5^d$.
The more time-consuming portion is calculating the result modulo $5^m$. We can note that $$k^m \mod n \equiv k^{m \mod \phi(n)} \mod n$$ where $\phi(n)$ is Euler's Totient function.
We can apply this function recursively, i.e. $$m^m \mod n \equiv m^{m \mod \phi(n)} \mod n$$ $$m^{m^m} \mod n \equiv m^{\left(m^m \mod \phi(\phi(n))\right) \mod \phi(n)} \mod n$$ $$\dots$$ where $n=5^d$. Therefore, the most extensive operation is exponentiation modulo $n$. This can be done in $O(\log(n))$ operations via exponentiation by squaring or binary exponentiation. This operation is done at most $n$ times, so we get a conservative bound of $O(n \log(n))$ or, really, $O(5^d \log(5^d))$ operations.