EXTREMEXPLOIT

Extended Euclidean Algorithm

Feb 20th, 2022 (edited)
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. import copy
  2.  
  3. def ExtendedEuclideanAlgorithm(A, B, showSteps=True):
  4.     if B > A: A, B, = B, A
  5.     initialA, initialB = copy.deepcopy(A), copy.deepcopy(B)
  6.     T1, T2 = 0, 1
  7.     if showSteps: print("Q | A | B | R | T1 | T2 | T")
  8.     while B != 0:
  9.         Q = A//B
  10.         R = A%B
  11.         T = T1 - T2*Q
  12.         if showSteps: print(f"{Q} | {A} | {B} | {R} | {T1} | {T2} | {T}")
  13.         A = copy.deepcopy(B)
  14.         B = copy.deepcopy(R)
  15.         T1 = copy.deepcopy(T2)
  16.         T2 = copy.deepcopy(T)
  17.     if T1 == 1:
  18.         print(f"There is no Multiplicative Inverse.")
  19.         return None
  20.     else:
  21.         print(f"Multiplicative Inverse: {T1}")
  22.         print(f"{initialB}*{T1} MOD {initialA} = {(initialB*T1) % initialA}")
  23.         return T1
Add Comment
Please, Sign In to add comment