Skip to content

Commit 467360f

Browse files
authored
Merge pull request #50 from AdityaJ7/modular-expo
Modular exponentiation with Python
2 parents 8a78ca2 + 69a1793 commit 467360f

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed

Modular Exponentiation/REAMDE.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## Modular Exponentiation
2+
3+
Modular exponentiation is a very important topic in Discrete Mathematics as well as in Cryptography.
4+
It's main application is in public-key cryptography.
5+
It is an effective way of calculating modulus values of extremely large exponents values like 2^500 with ease.
6+
You can refer to this amazing video to understand Modular exponentiation better:-
7+
[Modular Exponentiation](https://www.youtube.com/watch?v=EHUgNLN8F1Y)
8+
9+
I have used teo different approaches to solve this problem:
10+
11+
1. iterative
12+
2. recursive
13+
14+
The time complexity in case of iterative approach is O(log(power)) which is awesome.
15+
16+
The time complexity in case of recursive approach is O(power) which is very nice as well.
17+
18+
## How to use these two programs?
19+
20+
1. For iterative approach type:
21+
22+
python iterative.py
23+
24+
Here is a sample use case:
25+
26+
<p align = "center">
27+
<img src="iterative.PNG" alt="iter">
28+
</p>
29+
30+
2. For recursive approach type:
31+
32+
python recursive.py
33+
34+
Here is a sample use case:
35+
36+
<p align = "center">
37+
<img src="recursive.PNG" alt="rec">
38+
</p>

Modular Exponentiation/iterative.PNG

12.6 KB
Loading

Modular Exponentiation/iterative.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
def modular_exponentiation(base, power, modulus_to):
2+
"""This function uses the iterative approach to
3+
perform modular exponentiation.
4+
5+
Args:
6+
base (int): The number used for the base value of the expression.
7+
power (int): The number choosed for power value of the expression.
8+
modulus_to (int): The number used to take modulus of the expression.
9+
10+
Returns:
11+
result(int): The evaluated value of modular exponentiaion.
12+
"""
13+
result = 1 # Intial modular exponentiation result
14+
base %= modulus_to # Performing modulus on the base and assigning it back to the base.
15+
16+
while (power > 0): # Looping over untill the value of power is more than 0.
17+
18+
# To perform operation accordingly for odd and even powers.
19+
if ((power & 1) == 1): # To extract the lowest bit.
20+
result = (result * base) % modulus_to # For even power values we perform this.
21+
22+
power = power//2 # For odd value of power.
23+
base = (base ** 2) % modulus_to # Repeatitive squaring of the base and performing modulus operation
24+
25+
return result
26+
27+
28+
if __name__ == "__main__":
29+
base = int(input("Enter the base number: "))
30+
power = int(input("Enter the power number: "))
31+
modulus_to = int(
32+
input("Enter the number which is used to perform modulus operation: "))
33+
print(f"The result is {modular_exponentiation(base, power, modulus_to)}")

Modular Exponentiation/recursive.PNG

12.8 KB
Loading

Modular Exponentiation/recursive.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
def modular_exponentiation(base, power, modulus_to):
2+
"""This function uses the recursive approach to
3+
perform modular exponentiation.
4+
5+
Args:
6+
base (int): The number used for the base value of the expression.
7+
power (int): The number choosed for power value of the expression.
8+
modulus_to (int): The number used to take modulus of the expression.
9+
10+
Returns:
11+
result(int): The evaluated value of modular exponentiaion.
12+
"""
13+
14+
# Base Cases for recursion
15+
if (base == 0): # This reduces computation if base is 0.
16+
return 0
17+
if (power == 0): # This reduces computation if power is 0.
18+
return 1
19+
if (modulus_to == 1): # This reduces computation if modulus_to is 1.
20+
return 0
21+
22+
# Recursive cases for recursion
23+
result = 0
24+
if (power % 2 == 0): # To check if the power is even
25+
result = modular_exponentiation(base, power / 2, modulus_to) # Recursive call if power/2
26+
result = (result * result) % modulus_to # Storing the squared result value's mod back into result
27+
28+
else: # For odd power values
29+
result = base % modulus_to # Mod of the base value
30+
# In the below line the function has been called recursively and successive mods have been performed
31+
# with reducing values of power by a factor of 1
32+
result = (result * modular_exponentiation(base, power - 1,
33+
modulus_to) % modulus_to) % modulus_to
34+
return ((result + modulus_to) % modulus_to)
35+
36+
37+
if __name__ == "__main__":
38+
base = int(input("Enter the base number: "))
39+
power = int(input("Enter the power number: "))
40+
modulus_to = int(
41+
input("Enter the number which is used to perform modulus operation: "))
42+
print(f"The result is {modular_exponentiation(base, power, modulus_to)}")

0 commit comments

Comments
 (0)