forked from keon/algorithms
-
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.
Add chinese remainder theorem (keon#759)
* feat: Add basic ch. remainder theorem algorithm * feat: Add all n coprime check Co-authored-by: Lazar Cerovic<[email protected]> * Add gcd function * Add list length > 0 check * doc: Improve function documentation * feat: add all divisors need to be > 1 * test: Add test cases for crt solver * fix: make check_coprime private * fix: Change to python3.7 type hints * refactor: Move ch. remainder theorem tests to test_maths * Add link in README * Remove unnecessary whitespace and add newline at end of file * docs: Fix README alphabetic order
- Loading branch information
1 parent
6adcb36
commit 8f6e5f0
Showing
3 changed files
with
79 additions
and
1 deletion.
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
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,46 @@ | ||
from algorithms.maths.gcd import gcd | ||
from typing import List | ||
|
||
def solve_chinese_remainder(num : List[int], rem : List[int]): | ||
""" | ||
Computes the smallest x that satisfies the chinese remainder theorem | ||
for a system of equations. | ||
The system of equations has the form: | ||
x % num[0] = rem[0] | ||
x % num[1] = rem[1] | ||
... | ||
x % num[k - 1] = rem[k - 1] | ||
Where k is the number of elements in num and rem, k > 0. | ||
All numbers in num needs to be pariwise coprime otherwise an exception is raised | ||
returns x: the smallest value for x that satisfies the system of equations | ||
""" | ||
if not len(num) == len(rem): | ||
raise Exception("num and rem should have equal length") | ||
if not len(num) > 0: | ||
raise Exception("Lists num and rem need to contain at least one element") | ||
for n in num: | ||
if not n > 1: | ||
raise Exception("All numbers in num needs to be > 1") | ||
if not _check_coprime(num): | ||
raise Exception("All pairs of numbers in num are not coprime") | ||
k = len(num) | ||
x = 1 | ||
while True: | ||
i = 0 | ||
while i < k: | ||
if x % num[i] != rem[i]: | ||
break | ||
i += 1 | ||
if i == k: | ||
return x | ||
else: | ||
x += 1 | ||
|
||
def _check_coprime(l : List[int]): | ||
for i in range(len(l)): | ||
for j in range(len(l)): | ||
if i == j: | ||
continue | ||
if gcd(l[i], l[j]) != 1: | ||
return False | ||
return True |
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