- Published on
Linear Diophantine Equation
Linear Diophantine Equation
A linear Diophantine equation is an equation of the form
where , , and are given integers, and we seek integer solutions .
When Does a Solution Exist?
Let . The equation has an integer solution if and only if
that is, divides exactly.
This follows from Bézout's identity, which guarantees integers such that
If , scaling both sides by gives a solution to the original equation.
Finding a Particular Solution
The extended Euclidean algorithm computes while also producing satisfying . Multiplying through by gives one particular solution:
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 is known, every integer solution has the form
This describes an infinite family of solutions spaced along a line with integer step size .
Worked Example
Solve .
- , and , so solutions exist.
- Extended Euclidean algorithm gives , so .
- Scale by : particular solution .
- General solution:
Check : . ✅
Why It Matters
Linear Diophantine equations underpin many areas of number theory and computer science:
- Modular arithmetic — solving 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.