Skip to content

Added doctests in modular_exponential.py #1775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 20, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions maths/modular_exponential.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,6 +25,7 @@ def modular_exponential(base, power, mod):
result = (result * base) % mod
power = power >> 1
base = (base * base) % mod

return result


Expand All @@ -22,4 +35,8 @@ def main():


if __name__ == "__main__":
import doctest

doctest.testmod()

main()