Skip to content

Implementation of Chinese Remainder Theorem with Extended Euclidean Algorithm #5872

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

Conversation

mn121mn121
Copy link

This pull request implements the Chinese Remainder Theorem (CRT) in Java, providing an efficient solution to find an integer
𝑥 that satisfies multiple simultaneous congruences. The implementation uses the Extended Euclidean Algorithm to compute modular inverses, significantly improving performance for larger inputs.

Problem Statement: Given several pairwise coprime moduli (n1, n2, ..., nk) and integers reaminders (a1, a2, ..., ak), the goal is to find an integer x such that:

x ≡ a1 (mod n1)
x ≡ a2 (mod n2)
...
x ≡ ak (mod nk)

Implementation Details:
The code computes the product of all moduli.
For each modulus, it calculates the corresponding Ni and its modular inverse using the Extended Euclidean Algorithm.
It sums the contributions from each equation to find the result.

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

This commit implements the Chinese Remainder Theorem (CRT) algorithm in Java and adds detailed comments to explain the functionality of each part of the code. It includes functions for Greatest Common Divisor (GCD) calculation, modular inverse computation, and the main CRT logic.
@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 0% with 34 lines in your changes missing coverage. Please review.

Project coverage is 66.60%. Comparing base (dfff8d9) to head (4f213a7).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...m/thealgorithms/maths/ChineseRemainderTheorem.java 0.00% 34 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #5872      +/-   ##
============================================
- Coverage     66.74%   66.60%   -0.14%     
  Complexity     4489     4489              
============================================
  Files           610      611       +1     
  Lines         16929    16963      +34     
  Branches       3267     3272       +5     
============================================
  Hits          11299    11299              
- Misses         5182     5216      +34     
  Partials        448      448              

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

@siriak
Copy link
Member

siriak commented Oct 26, 2024

It's already present here

public final class ChineseRemainderTheorem {

@siriak siriak closed this Oct 26, 2024
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