Quite similar to the recursive algorithm that is used for solving the DSA problems such as installing Binary Tree Cameras, the implementation of the recursive solution for finding mod exponentiation is essentially the same!
 

So, what is exactly mod exponentiation? Well, mod exponents are the superscripts, subscripts and denominators of an arithmetic function that are commonly used for depicting multiple arithmetic values in the form of integers.
 

Mod exponentiation is a simple arithmetic expression used in public cryptosystems and other hardwares that require security.
 

So, are you interested to learn how mod exponentiation facilitates high levels of security in computer hardwares? Then this is just the right blog for you! 

What do you mean by exponentiation in computer programming?

Well, the process of exponentiation is essentially the subscript number written after performing multiplication on a given exponent repeatedly. The function is written something as follows: 


 

a to the power of b. 


 

Here, in the above expression, the alphabet "a" essentially represents the base of the character while "b" signifies the exponent.

This "a" character can be essentially an integer value where it can be either a positive or a negative integer.
 

For instance, if we are trying to multiply 4 ten times, then instead of writing the resulting value as 40, we will be implementing the exponentiation algorithm as:


 

4 to the power 10



 

Now, although it might seem like there would be a lengthy formula for exponentiation, the base and the exponent parts still remain the same in every representation as we have mentioned in the above formula.
 

With that said, in terms of modular exponentiation, there are essentially four different types. 
 

Check out the types of exponentiation performed in computer science in the section below.


 

What are the different types of exponents? 

Depending on the form of power held by the exponent, exponentiation can reflect different properties. 

There are essentially divided into the following categories:

 

Positive Exponent
 

The result of a positive exponent is caused by a positive integer that makes it behave simply. You can simplify the subset of a positive exponent by dividing the progression further.
 

For instance, check out the following example:

2 to the power 5 = 2 × 2 × 2 × 2 × 2 = 10


 

Negative Exponent

In order to obtain a negative exponent, we perform the opposite of the repeated multiplication process i.e repeated division.

Please note that once we have performed the repeated division algorithm, the exponent will be represented as a denominator in a fraction.


 

Zero Exponent
 

In the context of a zero exponent in modular exponentiation, the positive and the negative powers essentially cancel each other out.
 

Also, the ultimate multiplicative identity of the exponent reduces to 1. 

 

Rational Exponent
 

Finally, we have the rational exponents where the exponent is represented in the form of a fraction. Here, the numerator is essentially the power whereas, the denominator is the root of the fraction.
 

For instance, we could write 1612 as √16; 813

Did you know?
 

Modular exponentiation problems are otherwise known as the "Power of Modular Arithmetic" problems. 
 

Well, although these problems might be arithmetic, you can certainly implement some of the commonly used algorithms such as iterative solutions and the recursive methods for solving them in a computer program.

If you are interested in finding out how, then find your answers in the following section.

Finding Molecular Exponentiation Using Algorithms 

The field of computer science has certainly evolved to a great extent. 
 

Now, we can not only perform arithmetic progressions on a computer, but we can also generate answers for those using approaches and algorithms.
 

With that in mind, check out the following problem statement related to finding modular exponentiation in a program.
 

Problem Statement:
 

You have been given three different numbers, namely x, y and p. Your task is to compute the value for (x to the power of y) % p.
 

For instance, 
 

Input: y = 3, x = 2 and p = 5

Output: 3
 

Derivation: 2^3 % 5 = 8 % 5 = 3
 

We have discussed iterative and the recursive approaches below.
 

Method 1: Using the Iterative Approach 
 

Iterative algorithm essentially means performing a function repeatedly. In this approach, the instructions, steps are performed within a loop so that we can reach the desired results. 

Using the method of iteration is definitely one of the most detailed methods for solving the modular exponentiation problems.
 

This is because the power of the exponents can only be found if the given integer value is divided within the function multiple times.

Here's how the algorithm for the iterative function would look like.
 

Start by applying the logic, (x^y)%p in O(log y) */
 

Initialise your answer derived within the program
 

Keep going until the output reaches a zero
 

If y is found to be odd, you can multiply the given value of x with the results
 

Finally, change x into x^2
 

Time Complexity for this approach:

O(log2y)

 

Method 2: Using the Recursive Approach

Now, the recursive approach tends to solve the problem by focusing on the input values for the problem and finding its solution by carrying out smaller operations.
 

The recursive approach is also used for solving problems related to binary search tree cameras.

Here's how the algorithm for the recursive approach would work:
 

Start by iterating on the function for (x^y)%p in O(log y) */

Initialize your output and update the value of x if it is found to be equal to or more than p
 

If the value of y is found to be odd, you can multiply the value of x with the output

Initiate a divider code
 

Print the value of the code
 

Time Complexity for this approach:

O(Log y) 


 

Wrapping Up

The algorithms for performing mod exponentiation is definitely a time consuming and tricky process. 

Like other programming problems such as fixing Binary Tree Cameras in every node, it is not as easy to input a few functions and logic in the program to derive the exponent.
 

Hence, inorder to make it easier for our readers to comprehend this considerably difficult concept, this blog explains mod exponentiation in some of the simplest ways possible!


 

Ishita Juneja

4 Stories