Javatpoint Logo
Javatpoint Logo

C++ Program to Implement Modular Exponentiation Algorithm

Modular exponentiation is a base fundamental algoritm in number theory and cryptography that works to ef?ciently f?nd the remainder of an integer raised to a power then divided by another integer. This algorithm proves to be quite productive in cases where working with large numbers is involved - such as in cryptographic applications, RSA encryption being but one example.

The algorithm takes advantage of the fact that (a.b) mod n is equivalent to (a mod n⋅b mod n) mod n, thus, during the exponentiation process, we can perform the modular reduction at each step in order to avoid huge intermediate results.

Mathematical Concepts

Modular Arithmetic

Modular arithmetic is a subdiscipline of number theory devoted to arithmetic operations performed on integers subject to modulus restriction. Regarding modular ex??nenti?tion, while c?n?erned with the modulo operation, not?ted as a mod n, wh??h provides the remainder when ? is divided by n.

Key properties of modular arithmetic:

Addition: (a+b) mod n=((a mod n)+(b mod n)) mod n.

Multiplication: (a.b) mod n=(((a mod n).(b.mod n)) mod n) mod n.

Exponentiation

The modular exponential problem deals with efficiently deriving a^b mod n given integers a,b, and n.

Approach-1: Basic Iterative Approach for Modular Exponentiation

A fundamental iterative algorithm for modular exponential is the basic iterative approach. It has a loop that iterativel? squares the base and reduce the result modulo n at e??? ste?. The merits of the smethod does lie in its simplicity and intelligibility, but above all, this method is an ide?l choi?e f?r ?oncepts related to the educ?ti?nal field a?nd ?s a basis for understanding m?re advanced algorithms.

Program:

Output:

Enter the base: 4
Enter the exponent: 3
Enter the modulus: 2
Result: 4^3 mod 2 = 0

Explanation:

The algorithm starts with setting the end result to 1 because any range raised to the energy of 0 is 1. After that it goes right into a loop that iterates through the bits of the exponent. This loop keeps until the exponent becomes 0.

Inside the loop:

  • Checking the Exponent Bit:

It che?ks the f?llowing situation: if (exponent % 2 ==1), to ensure the current little bit of the exponent is set (1). If the bit is set, it approach that the corresponding energy of two is one of the very last result and the base multiplied with the contemporary end result.

  • Squaring the Base:

To take care of the following bit within the exponent, the base is squared. This is a critical optimization, as it enables the algor?thm to calculate powers of two successfully.

  • Right Shifting the Exponent:

In the next round of the loop, the exponent shifts right, and this action is equivalent to dividing by 2.

This procedure continues until the exponent is equal to zero. Ultimately, the function returns the computed value.

The user is prompted to enter the base, response, and modulus in the main function. These values are then utilized to execute modular expression with the power function, and the outcome is shown.

Example Usage:

The use of the example in the main function demonstrates easy use as well as practicality of the modular exponential algorithm. However, it should be noted that this simple iterative method is not complicated at all, yet a more optimized approach in form of the binary exponention is mostly used instead for greater efficiency especially in big number applications.

Complexity Analysis:

Lastly, the C++ code given as an input for the codification of the simple iterative method using modular exponenaliation is time and space complex, which represents a valuable criterion to evaluate its computational efficiency and scalability.

Time Complexity:

The number of bit in the binary representation of the exponent can be determined as the determining factor of time complexity of modular exponeniatation algorithm based on the number of iterations of loop. Let us denote the number of bits in the exponent by k.

Loop Iterations:

The while loop in the power function iterate once through each bit of the exponent. The expiration is right-shifted (divided by D) with each iteration until it reaches zero so the loop is worked for O (k) times.

Operations Inside the Loop:

The loop is performed simple arithmetical operations, for example, modular multiplication and division by 2 in each iteration. These opérations never stop in each iteration.

This means that the overall order of time complexity for the elementary iterative scheme usage for modular exponentiation is O(k), in which k stands for bit of the exponent. This complexity proves to be a highly effective one that not only lets the algorithm deal with exponents of significant size, but also uses reasonable amount of resources needed to complete the task.

Space Complexity:

Space complexity is an assessment of the memory that an algorithm needs to run it, including auxiliary space and input space.

Auxiliary Space:

The algorithm requires constant auxiliary space, independent of the size of the input. Result, base, exponent, and modulus are the only variables th?t consume space, with each one requiring a constant amount of space for storage.

Input Space:

The input space is determined by the size of the input values: base, exponent, and modulus. As these values are entered by the user and depend n?t on the size of the input.

Therefore, the t?t?l sp??e cr?m?lexity of the base iterative approach fo? the moduler ex??nenti?ti?n is O(1), showing th?t the algorithm u?es ?onst?nt amount of mem?ry.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA