-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
math/extended_euclidean_algorithm cannot properly handle negative numbers #2630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hey, thank you for pointing this out. It also seems that the Euclidean algorithm for GCD is not able to handle negative numbers as well: $ python greatest_common_divisor.py
Enter two integers separated by comma (,): -3,9
greatest_common_divisor(-3, 9) = -3
By iterative gcd(-3, 9) = 3
$ python greatest_common_divisor.py
Enter two integers separated by comma (,): 3,-9
greatest_common_divisor(3, -9) = 3
By iterative gcd(3, -9) = -3
$ python greatest_common_divisor.py
Enter two integers separated by comma (,): -3,-9
greatest_common_divisor(-3, -9) = -3
By iterative gcd(-3, -9) = -3
$ python greatest_common_divisor.py
Enter two integers separated by comma (,): 3,9
greatest_common_divisor(3, 9) = 3
By iterative gcd(3, 9) = 3 Can you make two separate PRs to correct the algorithms? |
14 tasks
14 tasks
dhruvmanila
added a commit
that referenced
this issue
Oct 7, 2020
* add type hints to math/extended euclid * math/extended euclid - add doctest * math/extended euclid: remove manual doctest * change algorithm for negative numbers * improve naming of variables * Update extended_euclidean_algorithm.py Co-authored-by: Dhruv <[email protected]>
stokhos
pushed a commit
to stokhos/Python
that referenced
this issue
Jan 3, 2021
…rs (TheAlgorithms#2626) * add type hints to math/extended euclid * math/extended euclid - add doctest * math/extended euclid: remove manual doctest * change algorithm for negative numbers * improve naming of variables * Update extended_euclidean_algorithm.py Co-authored-by: Dhruv <[email protected]>
Panquesito7
pushed a commit
to Panquesito7/Python
that referenced
this issue
May 13, 2021
…rs (TheAlgorithms#2626) * add type hints to math/extended euclid * math/extended euclid - add doctest * math/extended euclid: remove manual doctest * change algorithm for negative numbers * improve naming of variables * Update extended_euclidean_algorithm.py Co-authored-by: Dhruv <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Think there is a mistake in the algorithm implemented.
By definition, gcd (a,b) = gcd (a,-b) = gcd (-a,b) = gcd (-a,-b)
So for a = 1, b = 4, the gcd is 1.
eqn1: 0(4) + 1(1) = 1 (correct)
eqn2: 0(4) + 1(-1) = -1 (sign incorrect)
eqn3: -1(1) + 1(-4) = -5 (sign and value incorrect)
eqn4: 1(-1) + 0(-4) = -1 (sign incorrect)
The text was updated successfully, but these errors were encountered: