Skip to content

Instantly share code, notes, and snippets.

@mayormaier
Created March 24, 2022 03:16
Show Gist options
  • Save mayormaier/68ccfe07d5388a0be3eedeb85d8392ae to your computer and use it in GitHub Desktop.
Save mayormaier/68ccfe07d5388a0be3eedeb85d8392ae to your computer and use it in GitHub Desktop.
Uses the extended euclidian algorithm to find the multiplicative inverse of two numbers, a and b.
# If a and b are positive integers, then there are always integers m and n so that the GCD of a and b equals m·a+n·b.
# i.e., Since the GCD of 210 and 45 is 15, we should be able to write 15 as a sum of multiples of 210 and 45.
# Finding multiplicative inverses mod p
a = 3276
b = 29
print("Finding Multiplicative Inverse of", a, "and", b)
if a < b:
temp = a
a = b
b = temp
t1 = 0
t2 = 1
t = 0
found = False
while not found:
if b == 0:
print("Multiplicative Inverse:", t1)
found = True
break
q = a // b #floor division (aka integer division)
r = a % b
t = t1 - (t2 * q)
a = b
b = r
t1 = t2
t2 = t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment