我想要 sympy gcdex(ax + by = c 这是一个线性丢番图方程。)
I want sympy gcdex(ax + by = c This is a linear Diophantine equation.)
我要校准
https://en.wikipedia.org/wiki/Diophantine_equation#Examples
ax + by = c 这是丢番图线性方程
我试试
我觉得你替换h的方式不好。
我该如何解决?
x,y 整数
(1)(2)5^4x-2^4y=1
(3)5^5x-2^5y=1
(4)11^5x-2^5y=1
from sympy.solvers.inequalities import reduce_rational_inequalities
from sympy import *
import math
var('x y n')
def myEq(f,g,h,myMax):
myN = Symbol('myN', real=True)
myGcdex = gcdex(f, -g)
myX = g * n + myGcdex[2]
myY = solve((f * x - g * y - h).subs({x: myX}), y)[0]
myN = math.ceil(reduce_rational_inequalities([[myX.subs({n: myN}) >= myMax]], myN).lhs)
return myX.subs({n: myN}),myY.subs({n: myN})
print("#(1)",myEq ( 5**4,2**4,1, 0) )
print("#(2)",myEq ( 5**4,2**4,1, 10) )
print("#(3)",myEq ( 5**5,2**5,1,100), "????<correct>x=125,y=12207")
print("#(4)",myEq (11**5,2**5,1, 0)," ","????<correct>x= 19,y=95624")
#(1) (1, 39)
#(2) (17, 664)
#(3) (129, 100781/8) ????<correct>x=125,y=12207
#(4) (1, 80525/16) ????<correct>x= 19,y=95624
#
#〇(1)nxy 0 16*n + 1 625*n + 39 <correct>x = 16 n + 1 , y = 625 n + 39 , n=0
#〇(2)nxy 1 16*n + 1 625*n + 39
#×(3)nxy 4 32*n + 1 3125*n + 781/8 <correct>x = 32 n + 29 , y = 3125 n + 2832 , n=3
#×(4)nxy 0 32*n + 1 161051*n + 80525/16 <correct>x = 32 n + 19 , y = 161051 n + 95624, n=0
参考
https://mathworld.wolfram.com/Congruence.html
(20220407)
MY_diox( 5**4*x-2**4*y-1, 0) # (( 1, 39), (16*n - 15, 625*n - 586))
MY_diox( 5**4*x-2**4*y-1, 10) # (( 17, 664), (16*n + 1, 625*n + 39))
MY_diox( 5**5*x-2**5*y-1,100) # ((125, 12207), (32*n + 93, 3125*n + 9082))
MY_diox(11**5*x-2**5*y-1, 0) # (( 19, 95624), (32*n - 13, 161051*n - 65427))
(20220410)
参考圣人
您似乎在尝试对具有特定最小值 x 的方程求解。如果您解决了参数的不等式,您可以将其转换为整数区间,并且只是 select 第一个这样的整数。也可以使用原点偏移给出在 n = 1
处给出该值的修改方程式。
def diox(eq, x):
"""
>>> from sympy.abc import x, y
>>> eq = 5**5*x-2**5*y-1
>>> diox(eq, 30)
((61, 5957), (32*n + 29, 3125*n + 2832))
"""
s = diophantine(eq)
assert len(s) == 1
a, b = s.pop()
p = a.free_symbols
assert len(p) == 1
p = p.pop()
i = solve(a >= x).as_set().intersection(Integers)
if i.start.is_infinite:
i = i.sup
else:
i = i.start
t = Tuple(a, b).xreplace({p: i})
e = Tuple(a, b).xreplace({p: Symbol('n')+i-1})
return t, e
不出所料,以下是不可能的。
from sympy import *
from sympy.abc import x, y
from sympy.solvers.diophantine import diophantine
var('x a b c')
myFormula= a*x+b*y-c
def diox(eq, x):
.......................................
return t, e
print("#",diox(myFormula,0))
# NotImplementedError: No solver has been written for inhomogeneous_general_quadratic.
我要校准
https://en.wikipedia.org/wiki/Diophantine_equation#Examples
ax + by = c 这是丢番图线性方程
我试试
我觉得你替换h的方式不好。 我该如何解决?
x,y 整数
(1)(2)5^4x-2^4y=1
(3)5^5x-2^5y=1
(4)11^5x-2^5y=1
from sympy.solvers.inequalities import reduce_rational_inequalities
from sympy import *
import math
var('x y n')
def myEq(f,g,h,myMax):
myN = Symbol('myN', real=True)
myGcdex = gcdex(f, -g)
myX = g * n + myGcdex[2]
myY = solve((f * x - g * y - h).subs({x: myX}), y)[0]
myN = math.ceil(reduce_rational_inequalities([[myX.subs({n: myN}) >= myMax]], myN).lhs)
return myX.subs({n: myN}),myY.subs({n: myN})
print("#(1)",myEq ( 5**4,2**4,1, 0) )
print("#(2)",myEq ( 5**4,2**4,1, 10) )
print("#(3)",myEq ( 5**5,2**5,1,100), "????<correct>x=125,y=12207")
print("#(4)",myEq (11**5,2**5,1, 0)," ","????<correct>x= 19,y=95624")
#(1) (1, 39)
#(2) (17, 664)
#(3) (129, 100781/8) ????<correct>x=125,y=12207
#(4) (1, 80525/16) ????<correct>x= 19,y=95624
#
#〇(1)nxy 0 16*n + 1 625*n + 39 <correct>x = 16 n + 1 , y = 625 n + 39 , n=0
#〇(2)nxy 1 16*n + 1 625*n + 39
#×(3)nxy 4 32*n + 1 3125*n + 781/8 <correct>x = 32 n + 29 , y = 3125 n + 2832 , n=3
#×(4)nxy 0 32*n + 1 161051*n + 80525/16 <correct>x = 32 n + 19 , y = 161051 n + 95624, n=0
参考
https://mathworld.wolfram.com/Congruence.html
(20220407)
MY_diox( 5**4*x-2**4*y-1, 0) # (( 1, 39), (16*n - 15, 625*n - 586))
MY_diox( 5**4*x-2**4*y-1, 10) # (( 17, 664), (16*n + 1, 625*n + 39))
MY_diox( 5**5*x-2**5*y-1,100) # ((125, 12207), (32*n + 93, 3125*n + 9082))
MY_diox(11**5*x-2**5*y-1, 0) # (( 19, 95624), (32*n - 13, 161051*n - 65427))
(20220410)
参考圣人
您似乎在尝试对具有特定最小值 x 的方程求解。如果您解决了参数的不等式,您可以将其转换为整数区间,并且只是 select 第一个这样的整数。也可以使用原点偏移给出在 n = 1
处给出该值的修改方程式。
def diox(eq, x):
"""
>>> from sympy.abc import x, y
>>> eq = 5**5*x-2**5*y-1
>>> diox(eq, 30)
((61, 5957), (32*n + 29, 3125*n + 2832))
"""
s = diophantine(eq)
assert len(s) == 1
a, b = s.pop()
p = a.free_symbols
assert len(p) == 1
p = p.pop()
i = solve(a >= x).as_set().intersection(Integers)
if i.start.is_infinite:
i = i.sup
else:
i = i.start
t = Tuple(a, b).xreplace({p: i})
e = Tuple(a, b).xreplace({p: Symbol('n')+i-1})
return t, e
不出所料,以下是不可能的。
from sympy import *
from sympy.abc import x, y
from sympy.solvers.diophantine import diophantine
var('x a b c')
myFormula= a*x+b*y-c
def diox(eq, x):
.......................................
return t, e
print("#",diox(myFormula,0))
# NotImplementedError: No solver has been written for inhomogeneous_general_quadratic.