Skip to content

Commit 8a7dc8e

Browse files
authored
Merge pull request #18398 from abhinav28071999/doc
fixed is_nthpow_residue
2 parents 6ec4afb + 3c097bc commit 8a7dc8e

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

sympy/ntheory/residue_ntheory.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -628,18 +628,17 @@ def is_nthpow_residue(a, n, m):
628628
.. [1] P. Hackman "Elementary Number Theory" (2009), page 76
629629
630630
"""
631+
a = a % m
631632
a, n, m = as_int(a), as_int(n), as_int(m)
632633
if m <= 0:
633634
raise ValueError('m must be > 0')
634635
if n < 0:
635636
raise ValueError('n must be >= 0')
636-
if a < 0:
637-
raise ValueError('a must be >= 0')
638637
if n == 0:
639638
if m == 1:
640639
return False
641640
return a == 1
642-
if a % m == 0:
641+
if a == 0:
643642
return True
644643
if n == 1:
645644
return True
@@ -835,7 +834,9 @@ def nthroot_mod(a, n, p, all_roots=False):
835834
23
836835
"""
837836
from sympy.core.numbers import igcdex
837+
a = a % p
838838
a, n, p = as_int(a), as_int(n), as_int(p)
839+
839840
if n == 2:
840841
return sqrt_mod(a, p, all_roots)
841842
# see Hackman "Elementary Number Theory" (2009), page 76
@@ -1487,10 +1488,9 @@ def _val_poly(root, coefficients, p):
14871488

14881489

14891490
def _valid_expr(expr):
1490-
"""This function is used by `polynomial_congruence`.
1491-
If `expr` is a univariate polynomial will integer
1492-
coefficients the it returns its coefficients,
1493-
otherwise it raises Valuerror
1491+
"""
1492+
return coefficients of expr if it is a univariate polynomial
1493+
with integer coefficients else raise a ValueError.
14941494
"""
14951495

14961496
from sympy import Poly
@@ -1526,13 +1526,14 @@ def polynomial_congruence(expr, m):
15261526
[3257]
15271527
"""
15281528
coefficients = _valid_expr(expr)
1529+
coefficients = [num % m for num in coefficients]
15291530
rank = len(coefficients)
15301531
if rank == 3:
1531-
return quadratic_congruence(coefficients[0], coefficients[1],
1532-
coefficients[2], m)
1532+
return quadratic_congruence(*coefficients, m)
15331533
if rank == 2:
1534-
return quadratic_congruence(0, coefficients[0], coefficients[1],
1535-
m)
1534+
return quadratic_congruence(0, *coefficients, m)
1535+
if coefficients[0] == 1 and 1 + coefficients[-1] == sum(coefficients):
1536+
return nthroot_mod(-coefficients[-1], rank - 1, m, True)
15361537
if isprime(m):
15371538
return _polynomial_congruence_prime(coefficients, m)
15381539
return _help(m,

sympy/ntheory/tests/test_residue.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections import defaultdict
2-
from sympy import S, Symbol, Tuple
2+
from sympy import S, Symbol, Tuple, Dummy
33

44
from sympy.ntheory import n_order, is_primitive_root, is_quad_residue, \
55
legendre_symbol, jacobi_symbol, totient, primerange, sqrt_mod, \
@@ -130,7 +130,7 @@ def test_residue():
130130
#issue 10816
131131
assert is_nthpow_residue(1, 0, 1) is False
132132
assert is_nthpow_residue(1, 0, 2) is True
133-
assert is_nthpow_residue(3, 0, 2) is False
133+
assert is_nthpow_residue(3, 0, 2) is True
134134
assert is_nthpow_residue(0, 1, 8) is True
135135
assert is_nthpow_residue(2, 3, 2) is True
136136
assert is_nthpow_residue(2, 3, 9) is False
@@ -165,6 +165,8 @@ def test_residue():
165165
assert is_nthpow_residue(31, 4, 41)
166166
assert not is_nthpow_residue(2, 2, 5)
167167
assert is_nthpow_residue(8547, 12, 10007)
168+
assert is_nthpow_residue(Dummy(even=True) + 3, 3, 2) == True
169+
assert nthroot_mod(Dummy(odd=True), 3, 2) == 1
168170

169171
assert nthroot_mod(29, 31, 74) == [45]
170172
assert nthroot_mod(1801, 11, 2663) == 44
@@ -274,6 +276,7 @@ def test_residue():
274276
assert polynomial_congruence(10*x**2 + 14*x + 20, 2) == [0, 1]
275277
assert polynomial_congruence(x**3 + 3, 16) == [5]
276278
assert polynomial_congruence(65*x**2 + 121*x + 72, 277) == [249, 252]
279+
assert polynomial_congruence(x**4 - 4, 27) == [5, 22]
277280
assert polynomial_congruence(35*x**3 - 6*x**2 - 567*x + 2308, 148225) == [86957,
278281
111157, 122531, 146731]
279282
assert polynomial_congruence(x**16 - 9, 36) == [3, 9, 15, 21, 27, 33]

0 commit comments

Comments
 (0)