solving large numbers by using modular arithmetic and exponentiation by squaring in python

main.py
def mod_exp(base, exp, mod):
    result = 1
    base = base % mod
    while exp > 0:
        if exp % 2 == 1:
            result = (result * base) % mod
        exp = exp >> 1
        base = (base * base) % mod
    return result

base = 123456789
exp = 987654321
mod = 10**9 + 7
result = mod_exp(base, exp, mod)
print(result)
326 chars
16 lines

gistlibby LogSnag