Published on

Linear Diophantine Equation

Linear Diophantine Equation

A linear Diophantine equation is an equation of the form

ax+by=cax + by = c

where aa, bb, and cc are given integers, and we seek integer solutions x,y∈Zx, y \in \mathbb{Z}.


When Does a Solution Exist?

Let d=gcd⁡(a,b)d = \gcd(a, b). The equation ax+by=cax + by = c has an integer solution if and only if

d∣cd \mid c

that is, dd divides cc exactly.

This follows from Bézout's identity, which guarantees integers x0,y0x_0, y_0 such that

ax0+by0=da x_0 + b y_0 = d

If d∣cd \mid c, scaling both sides by c/dc/d gives a solution to the original equation.


Finding a Particular Solution

The extended Euclidean algorithm computes gcd⁡(a,b)\gcd(a, b) while also producing x0,y0x_0, y_0 satisfying ax0+by0=da x_0 + b y_0 = d. Multiplying through by c/dc/d gives one particular solution:

x1=x0⋅cd,y1=y0⋅cdx_1 = x_0 \cdot \frac{c}{d}, \qquad y_1 = y_0 \cdot \frac{c}{d}
def extended_gcd(a, b):
    if b == 0:
        return a, 1, 0  # base case: gcd(a, 0) = a, with a*1 + 0*0 = a
    d, x1, y1 = extended_gcd(b, a % b)
    # back-substitute the smaller pair's coefficients to get x, y for (a, b)
    x, y = y1, x1 - (a // b) * y1
    return d, x, y

def solve_linear_diophantine(a, b, c):
    d, x0, y0 = extended_gcd(a, b)
    if c % d != 0:
        return None  # d does not divide c, so no integer solution exists
    scale = c // d
    # a*x0 + b*y0 = d, so scaling both sides by c/d gives a*x + b*y = c
    return x0 * scale, y0 * scale

print(solve_linear_diophantine(6, 15, 9))  # e.g. (-6, 3)

The General Solution

Once one particular solution (x1,y1)(x_1, y_1) is known, every integer solution has the form

x=x1+t⋅bd,y=y1−t⋅ad,t∈Zx = x_1 + t \cdot \frac{b}{d}, \qquad y = y_1 - t \cdot \frac{a}{d}, \qquad t \in \mathbb{Z}

This describes an infinite family of solutions spaced along a line with integer step size (bd,−ad)\left(\frac{b}{d}, -\frac{a}{d}\right).


Worked Example

Solve 6x+15y=96x + 15y = 9.

  • d=gcd⁡(6,15)=3d = \gcd(6, 15) = 3, and 3∣93 \mid 9, so solutions exist.
  • Extended Euclidean algorithm gives 6(−2)+15(1)=36(-2) + 15(1) = 3, so x0=−2, y0=1x_0 = -2,\ y_0 = 1.
  • Scale by c/d=9/3=3c/d = 9/3 = 3: particular solution x1=−6, y1=3x_1 = -6,\ y_1 = 3.
  • General solution: x=−6+5t,y=3−2t,t∈Zx = -6 + 5t, \qquad y = 3 - 2t, \qquad t \in \mathbb{Z}

Check t=1t = 1: x=−1,y=1  ⟹  6(−1)+15(1)=9x = -1, y = 1 \implies 6(-1) + 15(1) = 9. ✅


Why It Matters

Linear Diophantine equations underpin many areas of number theory and computer science:

  • Modular arithmetic — solving ax≡c(modb)ax \equiv c \pmod{b} reduces to a linear Diophantine equation.
  • Cryptography — computing modular inverses (e.g., in RSA) uses the extended Euclidean algorithm.
  • Scheduling / tiling problems — finding integer combinations of two step sizes that reach a target value.

Reference