Home » What is modular exponentiation method?

What is modular exponentiation method?

Coding Problem

by ishitajuneja

Exponents are one of the most basic operations taught in elementary schooling. However, they are widely used in various technical operations worldwide in different forms. One of the applications of the exponents is modular exponentiation is optimal binary search tree execution. 

The property of exponentiation is asked in different forms by industry experts. You can use this operation directly in a code or make a part of code (while dealing with huge databases) smaller through exponentiation. 

The codes can directly ask you to calculate or solve a particular equation with exponential powers or apply the concept to different data structures. This operation can be used for optimal binary search tree solutions while applying theories on huge databases. 

In this article, we will discuss what modular exponentiation is and how you can utilize modular exponentiation property to resolve several problem statements. Let’s begin with what modular exponentiation means. 


What is modular exponentiation? 

Modular exponentiation is one of the most common properties used while dealing with public keys in computer science. 

The modular exponentiation is simply the reminder you will get when a certain integer is raised to a fixed power and afterward divided by another integer. 

The operations applied in this problem are power and division. There are three integers in this equation. 

Also, modular exponentiation is only applied with modulus which means that it is applied on positive integers. Two integers in the equation are kept positive at all times. However, the exponential power can be inserted in a negative integer form. 

The equation of this problem is formed as



B is the base integer-always positive

E is the exponential power- negative or positive

And n is the dividing integer- always positive

The equation remains the same however, the questions can add the “%” symbol to showcase the dividing operations. Hence, the equation in such cases become


Where the variables hold the same significance as the above-stated explanation. 

The value of C or the modular exponentiation always lies between 0 to the integer M. Thus, the results will always be positive in all circumstances. 

To establish a better understanding of the concept, let’s look at some working examples.


Working examples

Given the problem statement, you get three numbers where you will have to evaluate the equation (XY) %p.

Let’s find the solution to this problem by inputting small integers first. Let X=2, Y=3, and p=5.

Can you form an equation with these terms? The equation will be

2^3/5. This will yield you 2 to the power 3 divided by 5 i.e. 8/5=3. The remainder of this equation will be 3 hence the value of modular exponentiation will be the same as the final reminder. 

Thus, In this case, the modular exponentiation value will be 3.

Let’s take another example with x=10, y=4, and p=3.

In this case, the equation will become 10^4/3. Hence, it will further be evaluated to become 10000/3 which will leave a reminder of 1. Hence, the final output, in this case, will be 1.

As you can notice, the solution of this equation lies between 0 and 5&3 respectively. 

The next segment covers the different methods or approaches to solving the modular exponentiation problem for optimal binary search trees


Methods to resolve modular exponentiation problem

The two methods using which you can easily print the modular exponentiation results are iteration or recursion. 

Let’s discuss the iteration method in detail.


Iteration method

In this method, the approach utilized is that the power will be evaluated under the modulus value if the number is large. 

The mod function will be applied on every integer before evaluation which ensures that the result values are accurate and short. 

The iterative approach simply calculates the x^y expression first and then takes the accurate results to divide with mod M. 

Let’s consider the following steps to understand the steps of this method:

  • Take the input of X as a positive integer
  • Also, take Y as a positive or negative integer 
  • Finally, calculate the value of x^y and return this value to a variable in the next step
  • Afterward, simply divide this variable with M. M can be taken as input or inserted 

At last, return the value of this function and print the same. 

Let’s understand the second method i.e. the recursion method of printing the modular exponentiation results. 


Recursion method

The recursion method is based on different properties, these properties include:

  • If B is odd, the expression can be resolved by calculating (p*(q^(q-1))%r)
  • If B is even, the equation will be ((p^q/2)*(p^q/2))%r

These properties are easily used to find and print the modular exponentiation

In the recursion approach, the main function exponentMod is called repeatedly unless the whole equation is resolved. 

The following steps show how the results can be printed with the help of the recursion approach:

  • First, declare a function that takes integer A, integer B, and integer C as arguments. 
  • Check the base classes if it is zero or not. In case B is found 0, the function will return 1. However, if A is found to be 0, the function will return 0
  • In case B is even, y= (y*y) %c is evaluated to resolve this problem and the value is allocated to the variable
  • However, if B is found to be odd, the function will be called again
  • At the return value, simply return the ((y+c) %c) and print the value

The final value will depict the modular exponentiation results accurately. 

All you need in this approach is to check whether B is odd or even. 


Winding up

Modular exponentiation is one of the basic concepts you need while dealing with keys in crypto data. It can also be efficiently used for optimal binary search tree execution. With the help of the methods listed in this article, you can print accurate results for this problem statement easily. 

You may also like

Leave a Comment