File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed
Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ # @Author: S. Sharma <silentcat>
2+ # @Date: 2019-02-25T12:08:53-06:00
3+ # @Email: silentcat@protonmail.com
4+ # @Last modified by: silentcat
5+ # @Last modified time: 2019-02-26T07:07:38-06:00
6+
7+ import sys
8+
9+ # Finds 2 numbers a and b such that it satisfies
10+ # the equation am + bn = gcd(m, n) (a.k.a Bezout's Identity)
11+ def extended_euclidean_algorithm (m , n ):
12+ a = 0 ; aprime = 1 ; b = 1 ; bprime = 0
13+ q = 0 ; r = 0
14+ if m > n :
15+ c = m ; d = n
16+ else :
17+ c = n ; d = m
18+
19+ while True :
20+ q = int (c / d )
21+ r = c % d
22+ if r == 0 :
23+ break
24+ c = d
25+ d = r
26+
27+ t = aprime
28+ aprime = a
29+ a = t - q * a
30+
31+ t = bprime
32+ bprime = b
33+ b = t - q * b
34+
35+ pair = None
36+ if m > n :
37+ pair = (a ,b )
38+ else :
39+ pair = (b ,a )
40+ return pair
41+
42+ def main ():
43+ if len (sys .argv ) < 3 :
44+ print ('2 integer arguments required' )
45+ exit (1 )
46+ m = int (sys .argv [1 ])
47+ n = int (sys .argv [2 ])
48+ print (extended_euclidean_algorithm (m , n ))
49+
50+ if __name__ == '__main__' :
51+ main ()
You can’t perform that action at this time.
0 commit comments