Python program for Modular Exponentiation

Modular exponentiation calculates (baseexponent) % modulo efficiently. This operation is fundamental in cryptography and number theory, involving three integers: base, exponent, and modulo.

Syntax

# Basic syntax
result = (base ** exponent) % modulo

# Using pow() function (recommended)
result = pow(base, exponent, modulo)

Basic Example

Let's calculate (25) % 5 step by step ?

base = 2
exponent = 5
modulo = 5

# Step by step calculation
power_result = base ** exponent
print(f"{base}^{exponent} = {power_result}")

final_result = power_result % modulo
print(f"{power_result} % {modulo} = {final_result}")
2^5 = 32
32 % 5 = 2

Using pow() Function

Python's pow() function performs modular exponentiation efficiently in O(log n) time ?

base = 2
exponent = 5
modulo = 5

result = pow(base, exponent, modulo)
print(f"({base}^{exponent}) % {modulo} = {result}")

# Compare with direct calculation for large numbers
base = 3
exponent = 100
modulo = 7

result = pow(base, exponent, modulo)
print(f"({base}^{exponent}) % {modulo} = {result}")
(2^5) % 5 = 2
(3^100) % 7 = 4

Recursive Implementation

Here's how to implement modular exponentiation recursively using the divide-and-conquer approach ?

def mod_exp_recursive(base, exponent, modulo):
    # Base case: any number to power 0 is 1
    if exponent == 0:
        return 1
    
    # Base case: number to power 1
    if exponent == 1:
        return base % modulo
    
    # Recursive case: divide exponent by 2
    half_power = mod_exp_recursive(base, exponent // 2, modulo)
    
    if exponent % 2 == 0:
        # Even exponent
        return (half_power * half_power) % modulo
    else:
        # Odd exponent
        return (base * half_power * half_power) % modulo

# Test the function
base = 2
exponent = 10
modulo = 1000

result = mod_exp_recursive(base, exponent, modulo)
print(f"Recursive: ({base}^{exponent}) % {modulo} = {result}")

# Compare with pow()
result_pow = pow(base, exponent, modulo)
print(f"Using pow(): ({base}^{exponent}) % {modulo} = {result_pow}")
Recursive: (2^10) % 1000 = 24
Using pow(): (2^10) % 1000 = 24

Iterative Implementation

An iterative approach that's memory-efficient for large exponents ?

def mod_exp_iterative(base, exponent, modulo):
    result = 1
    base = base % modulo
    
    while exponent > 0:
        # If exponent is odd, multiply base with result
        if exponent % 2 == 1:
            result = (result * base) % modulo
        
        # Divide exponent by 2
        exponent = exponent // 2
        # Square the base
        base = (base * base) % modulo
    
    return result

# Test with different values
test_cases = [(3, 4, 5), (7, 10, 13), (2, 20, 17)]

for base, exp, mod in test_cases:
    result = mod_exp_iterative(base, exp, mod)
    print(f"({base}^{exp}) % {mod} = {result}")
(3^4) % 5 = 1
(7^10) % 13 = 4
(2^20) % 17 = 16

Comparison

Method Time Complexity Space Complexity Best For
pow() O(log n) O(1) Production code
Recursive O(log n) O(log n) Understanding algorithm
Iterative O(log n) O(1) Memory-constrained systems
Direct calculation O(n) O(1) Small numbers only

Conclusion

For modular exponentiation, use Python's built-in pow(base, exponent, modulo) function for optimal performance. The recursive and iterative implementations help understand the underlying algorithm used in cryptographic applications.

Updated on: 2026-03-24T21:03:43+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements