Instead of recursion, you can use an iterative approach. Start with and process each bit of from right to left.
If the current bit is 1, multiply by the current power of . Then square and move to the next bit.
This works because any exponent can be written in binary. For example, , so .