gistlib
main.py# Modular arithmetic def mod(a, b, m): return (a % m + b % m) % m # Exponentiation by squaring algorithm def exp_by_squaring(base, power): if power < 0: return exp_by_squaring(1 / base, -power) if power == 0: return 1 if power % 2 == 0: return exp_by_squaring(base * base, power // 2) return base * exp_by_squaring(base * base, (power - 1) // 2) # Example usage a = 10 b = 7 m = 5 result_mod = mod(a, b, m) print("Modular arithmetic result:", result_mod) base = 2 power = 10 result_exp = exp_by_squaring(base, power) print("Exponentiation by squaring result:", result_exp) 620 chars26 lines
# Modular arithmetic def mod(a, b, m): return (a % m + b % m) % m # Exponentiation by squaring algorithm def exp_by_squaring(base, power): if power < 0: return exp_by_squaring(1 / base, -power) if power == 0: return 1 if power % 2 == 0: return exp_by_squaring(base * base, power // 2) return base * exp_by_squaring(base * base, (power - 1) // 2) # Example usage a = 10 b = 7 m = 5 result_mod = mod(a, b, m) print("Modular arithmetic result:", result_mod) base = 2 power = 10 result_exp = exp_by_squaring(base, power) print("Exponentiation by squaring result:", result_exp)
gistlibby LogSnag