Skip to content

Commit 72e62fb

Browse files
Merge pull request sympy#26780 from zylipku/fix-cornacchia-miss
Fix a bug with diophantine() when cornacchia() is not called properly
2 parents 16362cc + 875e72d commit 72e62fb

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

.mailmap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,8 @@ Zeel Shah <kshah215@gmail.com>
15501550
Zhenxu Zhu <xzdlj@outlook.com> xzdlj <xzdlj@outlook.com>
15511551
Zhi-Qiang Zhou <zzq_890709@hotmail.com> zhouzq-thu <zzq_890709@hotmail.com>
15521552
Zhongshi <zj495@nyu.edu>
1553+
Zhuoyuan Li <zy.li@stu.pku.edu.cn> zylipku <123136644+zylipku@users.noreply.github.com>
1554+
Zhuoyuan Li <zy.li@stu.pku.edu.cn> zylipku <zy.li@stu.pku.edu.cn>
15531555
Zlatan Vasović <zlatanvasovic@gmail.com>
15541556
Zoufiné Lauer-Baré <raszoufine@gmail.com> Zoufiné Lauer-Baré <82505312+zolabar@users.noreply.github.com>
15551557
Zoufiné Lauer-Baré <raszoufine@gmail.com> zolabar <raszoufine@gmail.com>

sympy/solvers/diophantine/diophantine.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2174,6 +2174,25 @@ def cornacchia(a:int, b:int, m:int) -> set[tuple[int, int]]:
21742174
"""
21752175
# Assume gcd(a, b) = gcd(a, m) = 1 and a, b > 0 but no error checking
21762176
sols = set()
2177+
2178+
if a + b > m:
2179+
# xy = 0 must hold if there exists a solution
2180+
if a == 1:
2181+
# y = 0
2182+
s, _exact = iroot(m // a, 2)
2183+
if _exact:
2184+
sols.add((int(s), 0))
2185+
if a == b:
2186+
# only keep one solution
2187+
return sols
2188+
if m % b == 0:
2189+
# x = 0
2190+
s, _exact = iroot(m // b, 2)
2191+
if _exact:
2192+
sols.add((0, int(s)))
2193+
return sols
2194+
2195+
# the original cornacchia
21772196
for t in sqrt_mod_iter(-b*invert(a, m), m):
21782197
if t < m // 2:
21792198
continue

sympy/solvers/diophantine/tests/test_diophantine.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,3 +1049,11 @@ def test_quadratic_parameter_passing():
10491049
# test that parameters are passed all the way to the final solution
10501050
assert solution == {(t, 11*t), (t, -22*t)}
10511051
assert solution(0, 0) == {(0, 0)}
1052+
1053+
def test_issue_18628():
1054+
eq1 = x**2 - 15*x + y**2 - 8*y
1055+
sol = diophantine(eq1)
1056+
assert sol == {(15, 0), (15, 8), (-1, 4), (0, 0), (0, 8), (16, 4)}
1057+
eq2 = 2*x**2 - 9*x + 4*y**2 - 8*y + 14
1058+
sol = diophantine(eq2)
1059+
assert sol == {(2, 1)}

0 commit comments

Comments
 (0)