Fast Power Algorithm
A fast and efficient method to calculate exponents
Search for a command to run...
A fast and efficient method to calculate exponents
No comments yet. Be the first to comment.
All related to problem solving: tips and tricks, algorithms, and theories...
While managing my kubernetes cluster, I needed to install several helm charts to do things like: Installing ingress controller in order to connect my cluster's ingress with my cloud-provider's load balancer Installing metrics-server to be able to ge...

On sharing data throughout big monolith app

A refactoring journey from imperative to declarative code

When we need to calculate the value of some number base raised to the power of another number exp we can do so naively using the following algorithm:
def power(base, exp):
result := 1
for i = 1 to exp:
result *= base
return result
The problem with this algorithm is that it is very slow, its complexity is O(exp) which is linear in the value of the exponent.
To solve this problem, there is a simple algorithm called Power By Squaring or just "Fast Power" algorithm.
It is built on the observation that we can manipulate the exponentiation into a sequence of squaring, here are some examples:
For this we can write a simple recursive algorithm:
def fast_power(base, exp):
// Base conditions
if exp == 1: return base
if exp == 0: return 1
// The recursive definitions
if exp % 2 == 0: return fast_power(base * base, exp / 2)
return base * fast_power(base, exp-1)
The sense of this algorithm is the following:
And of course we need the 2 base conditions to specify when we are going to end our loop.
Now, thanks to halfing the exponent on each step, the complexity of this algorithm is O(log(exp)). Which is much better than what we had before.
In problem solving we are often required to calculate a huge exponent and get the result module some constant. knowing that the modulus operator can be distributed on multiplication, i.e. (a * b) % m = ( (a % m) * (b % m)) % m, we can update the fast power algorithm to accomodate for that:
def fast_power(base, exp, mod):
// Base conditions
if exp == 1: return base
if exp == 0: return 1
// The recursive definitions
if exp % 2 == 0: return fast_power((base * base) % mod, exp / 2, mod)
return (base * fast_power(base, exp-1, mod)) % mod
In conclusion, the Fast Power algorithm is a fast and efficient way to calculate exponents with a complexity of O(log(exp)). It can also be modified to calculate the result modulo a constant if needed. You can now use this algorithm to improve the performance of your code when dealing with large exponents.
If you think this article was useful, leave me a reaction and/or a comment. If you think people in your circle can make use of this article feel free to share it.