Skip to content

Commit 690e216

Browse files
committed
Adds fast expo in python
1 parent cc86336 commit 690e216

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def fast_mult_modulo(a, x, m):
2+
''' returns a^x % m '''
3+
ans = 1
4+
while x > 0:
5+
if x & 1:
6+
ans = (ans * a) % m
7+
a = (a * a) % m
8+
x //= 2
9+
return ans
10+
11+
12+
if __name__ == '__main__':
13+
# testing: should return 445
14+
print(fast_mult_modulo(1337, 65537, 2714))

0 commit comments

Comments
 (0)