Added doctests in modular_exponential.py (#1775)

* added doctests in modular_exponential.py

* added doctests in modular_exponential.py

* added URL link
This commit is contained in:
Iqrar Agalosi Nureyza 2020-02-20 18:29:01 +07:00 committed by GitHub
parent d2f7982a4e
commit 6b3bbc70a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,8 +1,20 @@
"""Modular Exponential."""
"""
Modular Exponential.
Modular exponentiation is a type of exponentiation performed over a modulus.
For more explanation, please check https://en.wikipedia.org/wiki/Modular_exponentiation
"""
"""Calculate Modular Exponential."""
def modular_exponential(base : int, power : int, mod : int):
"""
>>> modular_exponential(5, 0, 10)
1
>>> modular_exponential(2, 8, 7)
4
>>> modular_exponential(3, -2, 9)
-1
"""
def modular_exponential(base, power, mod):
"""Calculate Modular Exponential."""
if power < 0:
return -1
base %= mod
@ -13,6 +25,7 @@ def modular_exponential(base, power, mod):
result = (result * base) % mod
power = power >> 1
base = (base * base) % mod
return result
@ -22,4 +35,8 @@ def main():
if __name__ == "__main__":
import doctest
doctest.testmod()
main()