Exponent Evaluation
Updated Feb 03, 20254 min read
Exponent evaluation is the most important operation. It has applications in cryptography and encryption methods, The exponent tells us how many times to multiply the base by itself. Raising 𝑎 to the power of 𝑛 is expressed as multiplication by 𝑎 done 𝑛-1 times:𝑎𝑛=𝑎∙𝑎∙…∙𝑎.However, this method is not practical for large 𝑎 or 𝑛.Therefore we will apply the binary exponentiation method .The idea of binary exponentiations, to split the exponent using the binary representation and then do multiplication work of.
There are two approach to tackle Exponent Evaluation.
- Brute force / Naive method
- Binary Exponenetiation
Lets Take an example 411 to solve from both approach.
Brute force method : 411 will take 11 iteration to get final answer and their tome complexity is O(n).
Binary Exponentiation: Binary exponentiation (also known as exponentiation by squaring) is based on the observation that any exponent nnn can be expressed in binary form. If we write nnn as:
n=bk2k+bk−12k−1+⋯+b121+b020,
where each bib_ibi is either 0 or 1, then the power ana^nan can be rewritten as:
an=abk2k⋅abk−12k−1⋯ab121⋅ab020.
Using the fact that a2ia^{2^i}a2i can be computed by squaring aaa repeatedly, we can compute ana^nan much more efficiently.
For Example 411 can be written 48.42.41 in base of 2 and it will take only 3 Iteration to solve same problem.
Time complexity here is O(logn).
The Algorithms for Binary Exponentiation
There are two common approaches to binary exponentiation: the Right-to-Left method and the Left-to-Right method. Both achieve logarithmic time complexity, but they process the exponent's binary digits in different orders.
1. Right-to-Left Binary Exponentiation
This method processes the bits of the exponent from least significant to most significant. The algorithm repeatedly checks if the current bit is set (i.e., the exponent is odd), multiplies the result if needed, squares the base, and then shifts the exponent to the right.
Pseudocode:
function rightToLeftPow(a, n):
result = 1
while n > 0:
if (n mod 2 == 1):
result = result * a
a = a * a // Square the base
n = floor(n / 2) // Shift the exponent right by one bit
return result
Explanation:
- Initialize: Set
result = 1. - Loop: While the exponent nnn is greater than 0:
(i). if n is Odd multiply result by the current base a.
(ii). Square the base
(iii). Divide n by 2 - Return the final result holds the value an.
2. Left-to-Right Binary Exponentiation:
In this approach, you process the bits of the exponent from the most significant bit (MSB) to the least significant bit (LSB). This version typically requires obtaining the binary representation of nnn and then iterating over its bits.
Pseudocode:
function leftToRightPow(a, n):
result = 1
bits = binaryRepresentation(n) // e.g., if n = 13, bits = [1, 1, 0, 1]
for each bit in bits: // Process bits from MSB to LSB
result = result * result // Square the current result
if bit == 1:
result = result * a // Multiply by a if the bit is 1
return result
Explanation:
- Convert Exponent to Binary: Obtain the binary representation of n as an array of bits, with the most significant bit first.
- Initialize: Set result = 1.
- Loop: For each bit in the binary representation:
- Square result.
- If the current bit is 1, multiply result by a
- Return: The final result gives an
