def fast_pow(x, n):
accum = 1
p2 = x
while n:
if n % 2:
accum *= p2
p2 = p2 * p2
n //= 2
return accum
It's fast exponentiation in Python. If you want, check it in Rust. It works because squaring gives us powers of two in x's exponent. On the first iteration , on the second , then , then etc. Then we use accum *= p2 in order to collect together powers back to initial value of . In the code on GitHub there are some boring details you should keep in mind in an interview. Negative exponents, x equal to zero, blah blah blah. I hate this stuff.
Stop. I promised you fast Fibonacci. But we talked about quick power, which is totally unrelated. Or is it?