pythonalgorithmsympydiophantine

I want sympy gcdex(ax + by = c This is a linear Diophantine equation.)


I want to cal

https://en.wikipedia.org/wiki/Diophantine_equation#Examples

ax + by = c This is a linear Diophantine equation.

i try

I think the way you substituted h is not good. How should I fix it?

x,y integer

(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

ref

I want function convert from xy to cells

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)

ref sage

Get all positive integral solutions for a linear equation


Solution

  • It looks like you are trying to get a specific solution to the equation that has a certain minimum value for x. If you solve the inequality for the parameter you can convert that to an interval on integers and just select the first such integer. A modified equation to give that value at n = 1 can also be given using a shift of origin.

    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