mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-05 02:40:16 +00:00
725834b9bc
* Added binary exponentiaion with respect to modulo * Added miller rabin: the probabilistic primality test for large numbers * Removed unused import * Added test for miller_rabin * Add test to binary_exp_mod * Removed test parameter to make Travis CI happy * unittest.main() # doctest: +ELLIPSIS ... * Update binary_exp_mod.py * Update binary_exp_mod.py * Update miller_rabin.py * from .prime_check import prime_check Co-authored-by: Christian Clauss <cclauss@me.com>
29 lines
661 B
Python
29 lines
661 B
Python
def bin_exp_mod(a, n, b):
|
|
"""
|
|
>>> bin_exp_mod(3, 4, 5)
|
|
1
|
|
>>> bin_exp_mod(7, 13, 10)
|
|
7
|
|
"""
|
|
# mod b
|
|
assert not (b == 0), "This cannot accept modulo that is == 0"
|
|
if n == 0:
|
|
return 1
|
|
|
|
if n % 2 == 1:
|
|
return (bin_exp_mod(a, n - 1, b) * a) % b
|
|
|
|
r = bin_exp_mod(a, n / 2, b)
|
|
return (r * r) % b
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
BASE = int(input("Enter Base : ").strip())
|
|
POWER = int(input("Enter Power : ").strip())
|
|
MODULO = int(input("Enter Modulo : ").strip())
|
|
except ValueError:
|
|
print("Invalid literal for integer")
|
|
|
|
print(bin_exp_mod(BASE, POWER, MODULO))
|