Skip to content
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

Added code to calculate crt for non-coprime moduli in Integer #39716

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/sage/rings/integer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6965,8 +6965,6 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
Return the unique integer between `0` and `mn` that is congruent to
the integer modulo `m` and to `y` modulo `n`.

We assume that `m` and `n` are coprime.

EXAMPLES::

sage: n = 17
Expand All @@ -6976,6 +6974,16 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
17
sage: m%11
5

`crt` also works for some non-coprime moduli::

sage: 6.crt(0,10,4)
16
sage: 6.crt(0,10,10)
Traceback (most recent call last):
...
ValueError: no solution to crt problem since gcd(10,10) does not
divide 6-0
"""
cdef object g, s
cdef Integer _y, _m, _n
Expand All @@ -6984,7 +6992,9 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
_n = Integer(n)
g, s, _ = _m.xgcd(_n)
if not g.is_one():
raise ArithmeticError("CRT requires that gcd of moduli is 1.")
if (self % g) <> (_y % g):
raise ValueError("no solution to crt problem since gcd(%s,%s) does not divide %s-%s" % (_m, _n, self, _y))
return (self + g * Integer(0).crt((_y - self) // g, _m // g, _n // g)) % _n.lcm(_m)
# Now s*m + t*n = 1, so the answer is x + (y-x)*s*m, where x=self.
return (self + (_y - self) * s * _m) % (_m * _n)

Expand Down
Loading