Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import copy
- def ExtendedEuclideanAlgorithm(A, B, showSteps=True):
- if B > A: A, B, = B, A
- initialA, initialB = copy.deepcopy(A), copy.deepcopy(B)
- T1, T2 = 0, 1
- if showSteps: print("Q | A | B | R | T1 | T2 | T")
- while B != 0:
- Q = A//B
- R = A%B
- T = T1 - T2*Q
- if showSteps: print(f"{Q} | {A} | {B} | {R} | {T1} | {T2} | {T}")
- A = copy.deepcopy(B)
- B = copy.deepcopy(R)
- T1 = copy.deepcopy(T2)
- T2 = copy.deepcopy(T)
- if T1 == 1:
- print(f"There is no Multiplicative Inverse.")
- return None
- else:
- print(f"Multiplicative Inverse: {T1}")
- print(f"{initialB}*{T1} MOD {initialA} = {(initialB*T1) % initialA}")
- return T1
Add Comment
Please, Sign In to add comment