My chief understanding of the exponential and the logarithm come from Spivak's wonderful book Calculus. He devotes a chapter to the definitions of both.
Think of exponentiation as some abstract operation $f_a$ ($a$ is just some index, but you'll see why it's there) that takes a natural number $n$ and spits out a new number $f_a(n)$. You should think of $f_a(n) = a^n$.
To match our usual notion of exponentiation, we want it to satisfy a few rules, most importantly $f_a(n+m) = f_a(n)f_a(m)$. Like how $a^{n+m} = a^na^m$.
Now, we can extend this operation to the negative integers using this rule: take $f_a(-n)$ to be $1/f_a(n)$. then $f_a(0) = f_a(n-n) = f_a(n)f_a(-n) = 1$, like how $a^0=1$.
Then we can extend the operation to the rational numbers, by taking $f_a(n/m) = \sqrt[m]{f_a(n)}$. Like how $a^{n/m} = \sqrt[m]{a^n}$.
Now, from here we can look to extend $f_a$ to the real numbers. This takes more work than what's happened up to now. The idea is that we want $f_a$ to satisfy the basic property of exponentiation: $f_a(x+y)=f_a(x)f_a(y)$. This way we know it agrees with usual exponentiation for natural numbers, integers, and rational numbers. But there are a million ways to extend $f_a$ while preserving this property, so how do we choose?
Answer: Require $f_a$ to be continuous.
This way, we also have a way to evaluate $f_a(x)$ for any real number $x$: take a sequence of rational numbers $x_n$ converging to $x$, then $f_a(x)$ is $\lim_{n\to\infty} f_a(x_n)$. This seems like a pretty reasonable property to require!
Now, actually constructing a function that does this is hard. It turns out it's easier to define its inverse function, the logarithm $\log(z)$, which is the area under the curve $y=1/x$ from $1$ to $z$ for $0<z<\infty$. Once you've defined the logarithm, you can define its inverse $\exp(z) = e^z$. You can then prove that it has all the properties of the exponential that we wanted, namely continuity and $\exp(x+y)=\exp(x)\exp(y)$. From here you can change the base of the exponential: $a^x = (e^{\log a})^x = e^{x\log a}$.
To conclude: the real exponential function $\exp$ is defined (in fact uniquely) to be a continuous function $\mathbb{R}\to\mathbb{R}$ satisfying the identity $\exp(x+y)=\exp(x)\exp(y)$ for all real $x$ and $y$. One way to interpret it for real numbers is as a limit of exponentiating by rational approximations. Its inverse, the logarithm, can similarly be justified.
Finally, de Moivre's formula $e^{ix} = \cos(x)+i\sin(x)$ is what happens when you take the Taylor series expansion of $e^x$ and formally use it as its definition in the complex plane. This is more removed from intuition; it's really a bit of formal mathematical symbol-pushing.
First of all, one can see that $a^n\bmod b$ is eventually periodic in $n$. Let $a^{n+b'}\equiv a^n$ for sufficiently large $n$. Then the problem boils down to finding $n\bmod b'$.
By similar arguments, one can furthermore show that $n^k\bmod b'$ is eventually periodic in $k$ for $k<b'$. By induction, this let's us push a $\bmod$ up the power tower, until eventually we reach $\bmod1$, which gives us $0$. At such a point, additional powers only contribute to reaching the point where we reach that "eventually periodic" step.
Conclusion:
$a^{P_n(k)}\bmod b$ is eventually constant in $k$. What you are observing is the special case when it starts being constant at $k=1$.
Additional note:
Euler's totient and Carmichael's functions gives a $\bmod$ that gets pushed up into the next power (although it may not be optimal), so repeatedly applying $\varphi$ or $\lambda$ to the initial $b$ gives you a maximum height to check. For example, when $b=7$ and with Euler's totient function, we have
$\varphi(7)=6$
$\varphi(6)=2$
$\varphi(2)=1$
which means we only have to manually check $k<3$. For $a=40$ and $n=3$, it happens to be the case, so it holds for all $k\ge1$.
Best Answer
Let's count how many modular multiplications we need to do to get to $x^{25}$ with the two methods. The trivial method would need 24 multiplications (I leave out the calculation of the residue modulo $N$ - that will always be there): $x^2=x*x$, $x^3=x^2*x$, $x^4=x^3*x$, $\ldots$, $x^{25}=x^{24}*x$. With repeated squaring there are several approaches. We might first compute $x^2=x*x$, $x^4=(x^2)*(x^2)$, $x^8=(x^4)*(x^4)$, $x^{16}=(x^8)*(x^8)$. That is 4 multiplications so far. The point of doing this to store these values somewhere. Then we can compute $x^{24}=(x^{16})*(x^8)$ and $x^{25}=(x^{24})*x$ with just two extra multiplications. A total of 6 multiplications in comparison to 24. In the worst case we need to do about $2*\log_2m$ multiplications to compute $x^m$.
If you don't have enough memory to store that look table of powers $x^{2^k},k=0,1,\ldots,[\log_2m]$, then you can get away with less by using the approach outlined in Bill Dubuque's answer. In addition to $x$ itself you only need to store one other intermediate value. Here $25=11001_2$, and for a hopefully obvious reason I will also write the exponents in binary in parentheses in what follows. First compute the square $$ x^2=(x^{10_2})=x*x. $$ We notice that our exponent $25=11001_2$ begin with two 1s, so next we multiply this result with $x$ and calculate $$ x^3=(x^{11_2})=(x^2)*x. $$ Notice that this calculation needed only the result of the previous multiplication and the input $x$. Let's square this $$ x^6=(x^{110_2})=(x^3)*(x^3). $$ Again we only needed the previous output $x^3$. Now we have the three leading bits from the exponent correctly in place, or because the third bit (starting with the most significant) of the exponent was 0, there is no need to multiply this by $x$. So we square again and get $$ x^{12}=(x^{1100_2})=(x^6)*(x^6). $$ Again we only needed the previous result ($x^6$). The fourth bit of the exponent was also a zero, so we can proceed with the final squaring $$ x^{24}=(x^{11000_2})=(x^{12})*(x^{12}). $$ The "previous value only" -comment applies again. The final bit of the exponent was a '1', so we need to fix it. The last multiplication is $$ x^{25}=(x^{11001_2})=(x^{24})*x. $$ To summarize: We square repeatedly. If the next bit of the exponent is a '1' we insert an extra multiplication with the original input.