# Fermat's Little Theorem and Euler's Theorem

I'm having trouble understanding clever applications of Fermat's Little Theorem and its generalization, Euler's Theorem. I already understand the derivation of both, but I can't think of ways to use them in problems that I know I must use them (i.e. the question topic is set).

Here are two questions I had trouble with trying to use FLT and ET:

1. Find the 5th digit from the rightmost end of the number $N = 5^{\displaystyle 5^{\displaystyle 5^{\displaystyle 5^{\displaystyle 5}}}}$.

2. Define the sequence of positive integers $a_n$ recursively by $a_1=7$ and $a_n=7^{a_{n-1}}$ for all $n\geq 2$. Determine the last two digits of $a_{2007}$.

I managed to solve the second one with bashing and discovering a cycle in powers of 7 mod 1000, and that may be the easier path for this particular question rather than using ET. However, applying ET to stacked exponents I believe is nevertheless an essential concept for solving more complex questions like number 1 that I wish to learn. It would be helpful if I could get hints on using ET in those two problems and a general ET approach to stacked exponents and its motivation.

• Hint: $5^{5^5}$ is really just $5^{(5^5)}$, so treat the $(5^5)$ as one unit, and then apply ET. – Kenny Lau Jun 28 '16 at 22:57
• Also, number 2 is also stacked exponents. – Kenny Lau Jun 28 '16 at 22:58
• Wait, what do you mean just apply ET? Can you be more specific? – Widow Maven Jun 28 '16 at 23:01
• How familiar are you with the Chinese remainder theorem? Because FLT and Euler's require that the base of the power and the modulus are coprime, and in problem 1 they are not. This is what the Chinese remainder theorem helps you deal with. It tells you that you can focus on $5^{5^{5^{5^5}}}$ modulo $2^{6}$ and $5^6$ separately. – Arthur Jun 28 '16 at 23:02
• – Martin Sleziak Jun 29 '16 at 4:53

For the first problem you will need a little bit of Chinese Remainder Theorem. You want to find the remainder of the stacked exponential modulo $10^5 = 2^5 \times 5^5$. Consider the two prime divisors separately.

As $\phi(2^5) = 16$ we have that if $r_1$ is the remainder of $5^{5^{5^{5}}}$ modulo $\phi(2^5) = 16$ then $5^{5^{5^{5^{5}}}} \equiv 5^{r_1} \pmod {2^5}$. Now we have to find $r_1$, which is a solution of $5^{5^{5^{5}}} \equiv r_1 \pmod {2^4}$. Repeat this algorithm few times and you will get rid of the exponents and you will find a value such that: $5^{5^{5^{5^{5}}}} \equiv r \pmod {2^5}$. Now use that $5^{5^{5^{5^{5}}}} \equiv 0 \pmod {5^5}$ and glue the two solutions with Chinese Remainder Theorem.

The second one can be solved using similar method, but this time you won't need the Chinese Remainder Theorem, as $(7,100) = 1$. Actually this is easier as $7^4 \equiv 1 \pmod {100}$.

• Thanks so much. I got this for the second problem though with ET instead of cycling in mod 100. I'll admit it's not the best way, but hey, at least I got it.! Valid XHTML. FYI, I actually got 43 mod 100 in the end (arithmetic error in the image). – Widow Maven Jun 29 '16 at 0:23

Walking through the first problem, we effectively need to find $a \equiv 5^{5^{5^{5^{5}}}} \bmod 10^5$. This splits easily into finding $a_1 \equiv a \bmod 2^5$ and $a_2 \equiv a \bmod 5^5$ which can then be re-united with the Chinese remainder theorem.

The order of $5 \bmod 32$ divides $\phi(32)=16$ (and actually we could say it divides $\lambda(32) = 8$ due to the Carmichael function). Because $16$ is a power of two (so the order of every number will also be a power of two) we can quickly square repeatedly: $(5\to25\equiv-7\to49 \equiv17\to 289\equiv 1)$ to find that the order of $5$ is in fact $8$. So we need to find:

$$b \equiv 5^{5^{5^{5}}} \bmod 8 \\ \text{which will give:}\qquad a_1\equiv 5^b \bmod 32$$

Next step; the order of $5 \bmod 8$ is easily seen to be $2$, and we can note that the remaining exponent is odd. Stepping back down the exponent ladder, $$b \equiv 5^\text{odd} \equiv 5 \bmod 8 \\ \text{and }\qquad a_1\equiv 5^5 \equiv 21 \bmod 32$$

Also we have $a_2\equiv a \equiv 0 \bmod 5^5$. As it happens $5^5=3125\equiv 21 \bmod 32$, so $a\equiv 3125 \bmod 10^5$ and the requested digit is $0$.

Note that this is true for $5$s in a tower of exponents of height $3$ or more; and this is inevitable behaviour, that the values in the exponent tower not very far up don't have any effect to the final modular result. In the second problem the intimidation factor of a tower of exponents $2007$ high is removed by this knowledge; only the bottom few make a difference.