Skip to content

Fixed gcd_extended and mod_inverse for modular exponential #750

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

Merged
merged 1 commit into from
Jun 15, 2024
Merged

Fixed gcd_extended and mod_inverse for modular exponential #750

merged 1 commit into from
Jun 15, 2024

Conversation

Cheshulko
Copy link
Contributor

@Cheshulko Cheshulko commented Jun 15, 2024

Pull Request Template

Description

Fixed the implementation of gcd_extended and mod_inverse.
Reference: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein - Introduction to algorithms, 3rd edition. Page 937. The extended form of Euclid’s algorithm

Type of change

Current implementation is wrong. Examples:

mod_inverse(7, 13)
Current: 0
Correct: 2

mod_inverse(5, 31)
Current: 0
Correct: 25

mod_inverse(10, 11)
Current: 0
Correct: 10

The test

assert_eq!(
    modular_exponential(123, -45, 67),
    mod_inverse(123, 67).pow(45) % 67
); // Inverse of 123 mod 67 is calculated via the function

passed because mod_inverse(123, 67) is 0 in current implementation. Correct value is 6 and it overflows i64
6.pow(45) = 1,03945637534×10³⁵.

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I ran bellow commands using the latest version of rust nightly.
  • I ran cargo clippy --all -- -D warnings just before my last commit and fixed any issue that was found.
  • I ran cargo fmt just before my last commit.
  • I ran cargo test just before my last commit and all tests passed.
  • I checked COUNTRIBUTING.md and my code follows its guidelines.

@Cheshulko Cheshulko requested review from imp2002 and vil02 as code owners June 15, 2024 13:16
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.01%. Comparing base (864ef42) to head (e0483a9).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #750   +/-   ##
=======================================
  Coverage   95.00%   95.01%           
=======================================
  Files         303      303           
  Lines       22522    22529    +7     
=======================================
+ Hits        21398    21405    +7     
  Misses       1124     1124           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@vil02 vil02 changed the title Fixed 'gcd_extended' and 'mod_inverse' for modular exponential Fixed gcd_extended and mod_inverse for modular exponential Jun 15, 2024
@vil02 vil02 merged commit 57b2c5f into TheAlgorithms:master Jun 15, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants