computes base exponent mod time complexity olog n use similar to python inbuilt function pow if exponent 0 raise valueerrorexponent must be positive base mod result 1 while exponent 0 if the last bit is 1 add 2k if exponent 1 result result base mod exponent exponent 1 utilize modular multiplication properties to combine the computed mod c values base base base mod return result computes base exponent mod time complexity o log n use similar to python in built function pow if the last bit is 1 add 2 k utilize modular multiplication properties to combine the computed mod c values
def modular_exponential(base, exponent, mod):
    
    if exponent < 0:
        raise ValueError("Exponent must be positive.")
    base %= mod
    result = 1

    while exponent > 0:
        
        if exponent & 1:
            result = (result * base) % mod
        exponent = exponent >> 1
        
        base = (base * base) % mod

    return result