-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
added chinese_remainder_theorem #1232
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
Closed
OddExtension5
wants to merge
1
commit into
TheAlgorithms:master
from
OddExtension5:chinese_remainder_theorem
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
data_structures/hashing/number_theory/chinese_remainder_theorem.py
This file contains hidden or 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,44 @@ | ||
# Chinese Remainder Theorem: | ||
|
||
# If GCD(a,b) = 1, then for any remainder ra modulo a and any remainder rb modulo b there exists integer n, | ||
# such that n = ra (mod a) and n = ra(mod b). If n1 and n2 are two such integers, then n1=n2(mod ab) | ||
|
||
# Algorithm : | ||
|
||
# 1. Use extended euclid algorithm to find x,y such that a*x + b*y = 1 | ||
# 2. Take n = ra*by + rb*ax | ||
|
||
|
||
# Extended Euclid | ||
def ExtendedEuclid(a, b): | ||
if b == 0: | ||
return (1, 0) | ||
(x, y) = ExtendedEuclid(b, a % b) | ||
k = a // b | ||
return (y, x - k * y) | ||
|
||
|
||
# Uses ExtendedEuclid to find inverses | ||
def ChineseRemainderTheorem(n1, r1, n2, r2): | ||
(x, y) = ExtendedEuclid(n1, n2) | ||
m = n1 * n2 | ||
n = r2 * x * n1 + r1 * y * n2 | ||
return ((n % m + m) % m) | ||
|
||
|
||
# ----------SAME SOLUTION USING InvertModulo instead ExtendedEuclid---------------- | ||
|
||
# This function find the inverses of a i.e., a^(-1) | ||
def InvertModulo(a, n): | ||
(b, x) = ExtendedEuclid(a, n) | ||
if b < 0: | ||
b = (b % n + n) % n | ||
return b | ||
|
||
|
||
# Same a above using InvertingModulo | ||
def ChineseRemainderTheorem2(n1, r1, n2, r2): | ||
x, y = InvertModulo(n1, n2), InvertModulo(n2, n1) | ||
m = n1 * n2 | ||
n = r2 * x * n1 + r1 * y * n2 | ||
return (n % m + m) % m |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://pep8.org/#function-names In Python function_names should be lower_case and ClassNames should be CamelCase. This goes for all the functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, add some doctests https://docs.python.org/3/library/doctest.html
This goes for all the functions unless adding doctests makes no sense.