# Integer solution to linear equation [duplicate]

I need to find a good configuration for my computational kernel, which forces me to find some integer solutions to the following simple equation:

$a \cdot x - b \cdot y = c$,

where $a$, $b$ and $c$ are constant positive integers, and $x$ and $y$ are my unknown, that I need to also be positive integers.

I thought I could solve this using the Diophantine solution, but I'm not sure how to proceed due to one of the addends to be negative.

What is the best way to find all solutions to such an equation?

## marked as duplicate by Dietrich Burde, Shailesh, choco_addicted, Daniel W. Farlow, StrantsMay 30 '16 at 3:44

• If you have issues with the negative coefficient, just substitute $z=-y$ and proceed with the usual algorithms for the solution of Diophantine equations. Then extract the solutions with negative $z$. – N74 May 29 '16 at 20:54

We see that the line of solutions given any $a, b, c$ is modelled by the linear equation $$y = \frac{ax-c}{b}$$ (whose equation can be obtained by performing elementary algebra)

All you need to do is solve this equation given your $a,b,c$ such that the solutions are integers.

In order for $y$ to be a positive integer, we require that $ax-c$ be an integer (which is true given our circumstances) and $b$ be an integer (also true) such that $(ax-c) \mod b = 0$ (their dividend is an integer).

Since $b$ is a positive integer, and since in order for $y$ to be a positive integer, $(ax - c) / b$ must be positive, then $ax-c$ must be positive so that $y$ is positive. This means we require

$$ax > c$$

and thus $$x > a/c$$

So, to calculate for $x$ given $a,b,c$, we test all positive integers which satisfy

$$(ax-c) \mod b = 0$$ $$x > a/c$$

And if these requirements are met, then $x$ is a solution and therefore you may plug in to obtain $y$.