Skip to content

Commit

Permalink
Chinese remaindering also works for m1=1 or m2=1
Browse files Browse the repository at this point in the history
  • Loading branch information
fagu committed Oct 21, 2023
1 parent 4293d5c commit aa90012
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 15 deletions.
6 changes: 2 additions & 4 deletions doc/source/fmpz.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1283,8 +1283,7 @@ The ``fmpz_multi_CRT`` class is similar to ``fmpz_multi_CRT_ui`` except that it
congruent to `r_1` modulo `m_1` and `r_2` modulo `m_2`,
where `M = m_1 \times m_2`. The result `x` is stored in ``out``.

It is assumed that `m_1` and `m_2` are positive integers greater
than `1` and coprime.
It is assumed that `m_1` and `m_2` are positive coprime integers.

If sign = 0, it is assumed that `0 \le r_1 < m_1` and `0 \le r_2 < m_2`.
Otherwise, it is assumed that `-m_1 \le r_1 < m_1` and `0 \le r_2 < m_2`.
Expand All @@ -1296,8 +1295,7 @@ The ``fmpz_multi_CRT`` class is similar to ``fmpz_multi_CRT_ui`` except that it
congruent to `r_1` modulo `m_1` and `r_2` modulo `m_2`,
where `M = m_1 \times m_2`.

It is assumed that `m_1` and `m_2` are positive integers greater
than `1` and coprime.
It is assumed that `m_1` and `m_2` are positive coprime integers.

If sign = 0, it is assumed that `0 \le r_1 < m_1` and `0 \le r_2 < m_2`.
Otherwise, it is assumed that `-m_1 \le r_1 < m_1` and `0 \le r_2 < m_2`.
Expand Down
4 changes: 1 addition & 3 deletions src/fmpz/CRT.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ void fmpz_CRT(fmpz_t out, const fmpz_t r1, const fmpz_t m1,
fmpz_init(c);

fmpz_mod(c, m1, m2);
fmpz_invmod(c, c, m2);

if (fmpz_is_zero(c))
if (!fmpz_invmod(c, c, m2))
{
flint_printf("Exception (fmpz_CRT). m1 not invertible modulo m2.\n");
flint_abort();
Expand Down
6 changes: 0 additions & 6 deletions src/fmpz/CRT_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,6 @@ void fmpz_CRT_ui(fmpz_t out, const fmpz_t r1, const fmpz_t m1,
c = fmpz_fdiv_ui(m1, m2);
c = n_invmod(c, m2);

if (c == 0)
{
flint_printf("Exception (fmpz_CRT_ui). m1 not invertible modulo m2.\n");
flint_abort();
}

fmpz_init(m1m2);
fmpz_mul_ui(m1m2, m1, m2);

Expand Down
2 changes: 0 additions & 2 deletions src/ulong_extras/test/t-CRT.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ int main(void)
fmpz_randtest_not_zero(m1, state, b1);
fmpz_randtest_not_zero(m2, state, b2);
fmpz_abs(m1, m1);
fmpz_add_ui(m1, m1, 1);
fmpz_abs(m2, m2);
fmpz_add_ui(m2, m2, 1);
fmpz_mul(m1m2, m1, m2);

fmpz_gcd(r, m1, m2);
Expand Down

0 comments on commit aa90012

Please sign in to comment.