forked from zergtant/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added extended euclidean algorithm (TheAlgorithms#720)
* Added extended euclidean algorithm * Fixed extended euclidean algorithm
- Loading branch information
1 parent
9a44eb4
commit 2bbf8cd
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# @Author: S. Sharma <silentcat> | ||
# @Date: 2019-02-25T12:08:53-06:00 | ||
# @Email: [email protected] | ||
# @Last modified by: silentcat | ||
# @Last modified time: 2019-02-26T07:07:38-06:00 | ||
|
||
import sys | ||
|
||
# Finds 2 numbers a and b such that it satisfies | ||
# the equation am + bn = gcd(m, n) (a.k.a Bezout's Identity) | ||
def extended_euclidean_algorithm(m, n): | ||
a = 0; aprime = 1; b = 1; bprime = 0 | ||
q = 0; r = 0 | ||
if m > n: | ||
c = m; d = n | ||
else: | ||
c = n; d = m | ||
|
||
while True: | ||
q = int(c / d) | ||
r = c % d | ||
if r == 0: | ||
break | ||
c = d | ||
d = r | ||
|
||
t = aprime | ||
aprime = a | ||
a = t - q*a | ||
|
||
t = bprime | ||
bprime = b | ||
b = t - q*b | ||
|
||
pair = None | ||
if m > n: | ||
pair = (a,b) | ||
else: | ||
pair = (b,a) | ||
return pair | ||
|
||
def main(): | ||
if len(sys.argv) < 3: | ||
print('2 integer arguments required') | ||
exit(1) | ||
m = int(sys.argv[1]) | ||
n = int(sys.argv[2]) | ||
print(extended_euclidean_algorithm(m, n)) | ||
|
||
if __name__ == '__main__': | ||
main() |