It looks like an implementation of the "Square And Multiply" algorithm, which is based on the bitwise representation of the exponent. That algorithm is a very fast way to compute powers and you would not simply come up with that solution by thinking; you would probably already know that algorithm from a book when you use it.
I did not learn about it in CS, but in my current master studies (Applied IT Security). It is detailed on Wikipedia , however here is the short and easy explanation from my book for the vanilla algorithm:
The algorithm is based on scanning the bit of the exponent from the left (the most significant bit) to the right (the least significant bit). In every iteration, i.e., for every exponent bit, the current result is squared. If and only if the currently scanned exponent bit has the value 1, a multiplication of the current result by x is executed following the squaring.
(Understanding Cryptography by Christof Paar, Jan Pelzl, ISBN: 978-3-642-04100-6, Springer)
In your case, the algorithm walks from the right to the left and does a "Multiply and Square" operation. That means that, for example, if you want to compute x^26 (with 0d26 = 0b11010), all you have to do is check the rightmost exponent bit and if the bit is 0 (even numbers), you only do a square operation of the base, else, if the bit is 1 (uneven numbers; in code: if n & 1:) you additionally do a multiplication with the result before the squaring. In order to walk left, the above algorithm shifts all bits to the right (n = n >> 1). Then it can start anew, until there are no more bits left to shift, in which case n contains the value 0 and the loop ends. Using x^26, that would result in
| Step | Current exponent bit | Computation | Current x | Result |
| #0 | 0 | SQ = x * x | x^2 | 1 |
| #1 | 1 | MUL + SQ = x^2 * x^2 | x^4 | x^2 |
| #3 | 0 | SQ = x^4 * x^4 | x^8 | x^2 |
| #4 | 1 | MUL + SQ = x^8 * x^8 | x^16 | x^10 |
| #5 | 1 | MUL + SQ = x^16 * x^16 | x^32 | x^26 |
The vanilla algorithm, just as a comparison, would work like that:
| Step | Current exponent bit | Computation | Current power |
| #0 | 1 | INITIAL | x^1 = x^0b1 |
| #1 | 1 | S&M = x^2 * x | x^3 = x^0b11 |
| #3 | 0 | SQ = (x^3)^2 | x^6 = x^0b110 |
| #4 | 1 | S&M = (x^6)^2 * x | x^13 = x^0b1101 |
| #5 | 0 | SQ = (x^13)^2 | x^26 = x^0b11010 |
If you compare the exponent bits of the current power you calculate (right-most column) and the current exponent bit (second column from the left), you can see that you slowly build the correct exponent bit by bit, just by using the multiplication operation of your CPU (square is also just a multiplication with the number itself). So, calculating a very large power of a number results in only a few multiplications, which a CPU can very quickly calculate.
I think, the vanilla algorithm is easier to understand and I wonder why they did not introduce and use it in that book. It's something I would have expected from such a book.
"Square and Multiply" is used in current crypto-systems, for example for RSA encryption, which can be used for HTTPS.