Skip to content

Commit 0330d88

Browse files
Added Modular Exponential
1 parent a03b2ea commit 0330d88

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Maths/ModularExponential.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def modularExponential(base, power, mod):
2+
if power < 0:
3+
return -1
4+
base %= mod
5+
result = 1
6+
7+
while power > 0:
8+
if power & 1:
9+
result = (result * base) % mod
10+
power = power >> 1
11+
base = (base * base) % mod
12+
return result
13+
14+
15+
def main():
16+
print(modularExponential(3, 200, 13))
17+
18+
19+
if __name__ == '__main__':
20+
main()

0 commit comments

Comments
 (0)