2.5: Linear Diophantine Equations

In this section, we discuss equations in two variables called diophantine equations. The goal of this section is to present the set of points that determine the solution to this kind of equations. Geometrically speaking, the diophantine equation represent the equation of a straight line. We need to find the points whose coordinates are integers and through which the straight line passes.

A linear equation of the form (ax+by=c) where (a,b) and (c) are integers is known as a linear diophantine equation.

Note that a solution to the linear diophantine equation ((x_0,y_0)) requires (x_0) and (y_0) to be integers. The following theorem describes the case in which the diophantine equation has a solution and what are the solutions of such equations.

The equation (ax+by=c) has integer solutions if and only if (dmid c) where (d=(a,b)). If the equation has one solution (x=x_0), (y=y_0), then there are infinitely many solutions and the solutions are given by [x=x_0+(b/d)t y=y_0-(a/d)t] where (t) is an arbitrary integer.

Suppose that the equation (ax+by=c) has integer solution (x) and (y). Thus since (dmid a) and (dmid b), then [dmid (ax+by)=c.] Now we have to prove that if (dmid c), then the equation has integral solution. Assume that (dmid c). By theorem 9, there exist integers (m) and (n) such that [d=am+bn.] And also there exists integer (k) such that [c=dk] Now since (c=ax+by), we have [c=dk=(ma+nb)k=a(km)+b(nk).] Hence a solution for the equation (ax+by=c) is [x_0=km mbox{and} y_0=kn.] What is left to prove is that we have infinitely many solutions. Let [x=x_0+(b/d)t mbox{and} y=y_0-(a/d)t.] We have to prove now that (x) and (y) are solutions for all integers (t). Notice that [ax+by=a(x_0+(b/d)t)+b(y_0-(a/d)t)=ax_0+by_0=c.] We now show that every solution for the equation (ax+by=c) is of the form [x=x_0+(b/d)t mbox{and} y=y_0-(a/d)t.] Notice that since (ax_0+by_0=c), we have [a(x-x_0)+b(y-y_0)=0.] Hence [a(x-x_0)=b(y-y_0).] Dividing both sides by (d), we get [a/d(x-x_0)=b/d(y-y_0).] Notice that ((a/d,b/d)=1) and thus we get by Lemma 4 that (a/dmid y-y_0). As a result, there exists an integer (t) such that (y=y_0-(a/d)t). Now substituting (y-y_0) in the equation [a(x-x_0)=b(y-y_0).] We get [x=x_0+(b/d)t.]

The equation (3x+6y=7) has no integer solution because ((3,6)=3) does not divide (7).

There are infinitely many integer solutions for the equation (4x+6y=8) because ((4,6)=2 mid 8). We use the Euclidean algorithm to determine (m) and (n) where (4m+6n=2). It turns out that (4(-1)+6(1)=2). And also (8=2.4). Thus (x_0=4.(-1)=-4) and (y_0=4.1=4) is a particular solution. The solutions are given by [x=-4+3t y=4-2t] for all integers (t).


  1. Either find all solutions or prove that there are no solutions for the diophantine equation (21x+7y=147.)
  2. Either find all solutions or prove that there are no solutions for the diophantine equation (2x+13y=31.)
  3. Either find all solutions or prove that there are no solutions for the diophantine equation (2x+14y=17.)
  4. A grocer orders apples and bananas at a total cost of $8.4. If the apples cost 25 cents each and the bananas 5 cents each, how many of each type of fruit did he order.

Linear recurrence solution to Diophantine equation

I have a Diophantine equation of the form: $ax^2 + bx + c = y^2, quad x, y in mathbb$ Is it true that there will always be a linear recurrence formula that generates all the solutions for $x$, of the form: $x_n = alpha_1x_ + alpha_2x_ + alpha_3,$ for some constants $alpha_1, alpha_2, alpha_3 in mathbb$ (where plugging integers into $n$ generates the next $x$ that solves the Diophantine equation)?

If so, is it possible to prove this?

And if so, is it possible to show how these constants $alpha_1, alpha_2, alpha_3$ are found?

To find one solution of the Diophantine equation with 2 unknowns, you can use the Extended Euclidean algorithm. First, assume that $a$ and $b$ are non-negative. When we apply Extended Euclidean algorithm for $a$ and $b$, we can find their greatest common divisor $g$ and 2 numbers $x_g$ and $y_g$ such that:

If $c$ is divisible by $g = gcd(a, b)$, then the given Diophantine equation has a solution, otherwise it does not have any solution. The proof is straight-forward: a linear combination of two numbers is divisible by their common divisor.

Now supposed that $c$ is divisible by $g$, then we have:

$a cdot x_g cdot frac + b cdot y_g cdot frac = c$

Therefore one of the solutions of the Diophantine equation is:

$x_0 = x_g cdot frac,$ $y_0 = y_g cdot frac.$

The above idea still works when $a$ or $b$ or both of them are negative. We only need to change the sign of $x_0$ and $y_0$ when necessary.

Finally, we can implement this idea as follows (note that this code does not consider the case $a = b = 0$):

Euclidean Algorithm and Linear Diophantine Equations

In this article, we will be talking about the greatest common divisor (gcd) and algorithms to calculate it. The Euclidean algorithm (or Euclid’s algorithm) is one of the most used and most common mathematical algorithms, and despite its heavy applications, it’s surprisingly easy to understand and implement.

In the simplest form the gcd of two numbers a, b is the largest integer k that divides both a and b without leaving any remainder. We will denote it as gcd(a, b), which is the standard representation. When one of the numbers in the pair is 0, by definition, the gcd is the second number. When both the numbers are 0, the greatest common divisor is undefined but is assumed to be 0.

The trivial algorithm to find the gcd of two numbers will be to loop from 1 to min (a, b) and check for each number if it divides both a and b. The range of gcd (a, b) is [ 1 , min(a, b)] because a number cannot divide another number smaller than it. For the same reason, we loop from 1 to min (a, b) .

The pseudocode for the above idea would be:

Another way to find the gcd would be to run the loop from i = a to i = 1 and return gcd at the first occurrence of ‘i’ such that it divides both a and b. In both cases, the time complexity in the worst case for this algorithm remains O (n) . The Euclidean algorithm, on the other hand, gives us a way to compute the greatest common divisor of two numbers in O( log ( min ( a ,b))) . This article will introduce the Euclidean algorithm to find the gcd and its applications in competitive programming.


You can observe that the gcd of two numbers does not change if the larger number is replaced by the difference between the two numbers. By doing this repeatedly, in the end, one of the elements will become 0 and the gcd becomes the second element (which is non-zero). With each run one of the numbers decreases, which makes sure the algorithm will terminate eventually.


Let a be pk and b be qk where k is the gcd of a and b and without any loss of generality assume a <= b, i.e., p <= q.
Now the difference b-a = (q-p)*k where q - p >= 0
It can see that the gcd of a and b-a is also k.
On repeating this process further, in the end, we get 0, and the other number is k.

Pseudocode for the above idea:

This can be represented mathematically as,


The same algorithm can be improved a little, replacing the larger number with b%a instead of b-a. b%a means b modular a or the remainder when b gets divided by a. The proof of this change is in Equation 1.
Let the gcd of a and b be k. Therefore k|a and k|b where p|q means p divides q.
Now by definition, b%a = b - ⌊b/a⌋*a
Both b and ⌊b/a⌋*a are divisible by k hence b - ⌊b/a⌋*a is also divisible by k, which makes b%a | k. After this point, we can follow what we did in Equation 1, replacing each subtraction with a mod.

Taking the same example of 14 and 26

14 26 replace 26 with 26%14
14 12 replace 14 with 14%12
2 12 replace 12 with 12%2
2 0

As soon as one of the numbers turns 0, exit, gcd is the second number.
Here is some pseudocode for the above algorithm:

The complexity of this algorithm is log2min (a, b) . In reality, the number of the function call is not more than five times the digits in base 10 of the smaller number. We can see the power of logarithm here: in the trivial algorithm defined at the start of this article, the number of times the code runs for reasonably large numbers, assume in the order 10^9, would be

10^9, but using Euclid’s algorithm, we can compute the greatest common divisor in not more than thirty steps.


The extended Euclidean algorithm states that for any two positive integers a and b, there always is m and n such that it is possible to represent the gcd of a and b as a * m + b * n.

a * m + b * n = gcd (a, b) for some integer m and n, they can be negative or zero.

While the Euclidean algorithm finds the gcd of two numbers, the extended algorithm also allows us to represent this gcd in terms of these two numbers. The importance of this result is seen more in the next topic, linear diophantine equations.

In this original Euclidean theorem, the operations end when one of the numbers is 0 and the other is g. For these parameters, we can easily find the coefficients m and n, which is

0 * 0 + g* 1 = g … Equation 2

Where 0 is m and 1 is n, instead of 0, m can be taken as an integer, as the equations remain true it is interesting to see that on changing the values here, we get different final values of m and n. For example if a = 3 and b = 5 then
3 *( 2 ) + 5 *( -1 ) = 1 but also,
3 *( -3 ) + 5 *( 2 ) = 1 . So, changing the value of m and n in the base case gives us different m and n for the original equation.

By reversing the steps in Euclid’s theorem, we can find the coefficients m and n. All we need to do is to figure out how the value of m, n changes from (b%a, a) to (a, b).

Let us assume we know some x0 and y0 such that

Therefore m = x0 - ⌊b/a⌋y0
n = y0

Pseudocode for the above result looks like:


We have three integers, a, b, and c, such that a * x + b * y = c , we are required to find a solution to this linear equation. Linear equations in two variables are known as linear diophantine equations, and the extended Euclidean algorithm helps in solving these problems.

Let us look at the extended Euclidean algorithm equation again, denote g as the gcd(a, b), and x_g, y_g be integers for which:

ax_g + by_g = g is true
axg*( c/g ) + byg*( c/g ) = c is also true

Therefore the x we require is equal to x_g*( c/g ) and y = y_g*(c/g) . If c is divisible by gcd (a, b) , then the linear equation has one or more solutions otherwise, it does not. It is easy to conclude that a linear combination of any integer should be divisible by that number.
Therefore one of the solutions to the above linear diophantine equation is:

Pseudocode for the above is, using the gcd function we calculated earlier:


Kudos! You completed it. You can now solve any problem that uses Euclidean’s algorithm or is about finding solutions to linear equations. In competitive programming, most of the time, the problems do not come with the straight task of computing the gcd of two numbers. Still, often it is a subtask to solving some DP or greedy or rather any mathematical problems. Diophantine equations are rarely anything but advantageous when there are problems like "In how many ways can <x> and <y> combine with <conditions> and their sum is <z> . " It’s always important to keep the mathematical theorem and algorithms handy when you want to increase ratings.

$ax+by+cz = t$

Proof (“$Rightarrow$”): Proceeds analogously to the $ax+by = t$ case before.

Proof (“$Leftarrow$”): We will generalize the proof from the $ax+by = t$ case before.

$gcd(a,b,c) mathrelvert t Rightarrow gcd(a, gcd(b, c)) mathrelvert t Rightarrow ax + gcd(b,c)w = t$ has an integer solution $(x_0, w_0)$.

Let $gcd(b,c) = d$. We know $exists (y_0, z_0)$ such that: egin b y_0 + c z_0 = d end Replacing in the previous equation, we have: egin ax_0 + gcd(b,c)w_0 &= tLeftrightarrow ax_0 + dw_0 &= tLeftrightarrow ax_0 + (b y_0 + c z_0) w_0 &= tLeftrightarrow ax_0 + b w_0 y_0 + c w_0 z_0 &= t end Thus, an integer solution is $(x_0, y_0 w_0, z_0 w_0)$.

Theorem (from Sec 6.2 in 2 ): In this case, if $(x_0, y_0, z_0)$ is one integer solution, then all integer solutions are of the form: egin x &= x_0 + m b / gcd(a,b) - ell c / gcd(a,c)
y &= y_0 + k c / gcd(b,c) - m a / gcd(a, b)
z &= z_0 + ell a / gcd(a, c) - k b / gcd(b, c) end Here, $m,ell,k$ are integers and at least one of $a,b$ or $c$ are $ e 0$.

$ax+by+cz = 0$

In this case, all integer solutions are of the form: egin x &= m b / gcd(a,b) - ell c / gcd(a,c)
y &= k c / gcd(b,c) - m a / gcd(a, b)
z &= ell a / gcd(a, c) - k b / gcd(b, c) end Here, $m,ell,k$ are arbitrary integers.

An example

In a Catalano-Fiore vector commitment (VC) 3 of size $n=3$, collision resistance is implied by the fact that the following equation with $(ell+1)$-bit primes $e_1,e_2,e_3$ does not have any $ell$-bit integer solutions:

[e_2 e_3 v_1 + e_1 e_3 v_2 + e_1 e_2 v_3 = 0]

The only integer solutions given by the formula above are at least $ell+1$ bit wide: egin %x &= frac\
% &= frac = (m-ell)e_1
x &= m (e_1 e_3) / gcd(e_2 e_3, e_1 e_3) - ell (e_1 e_2) / gcd(e_2 e_3, e_1 e_2) =
&= m (e_1 e_3) / e_3 - ell (e_1 e_2) / e_2 = m’ e_1 ( ext m’=m-ell)
y &= k’ e_2
z &= ell’ e_3 end


How does this work?
Let GCD of ‘a’ and ‘b’ be ‘g’. g divides a and b. This implies g also divides (ax + by) (if x and y are integers). This implies gcd also divides ‘c’ using the relation that ax + by = c. Refer this wiki link for more details.
This article is contributed by Ashutosh Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to [email protected] See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Attention reader! Don&rsquot stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.

Euler&aposs Method for Solving Linear Diophantine Equations

Interact on desktop, mobile and cloud with the free Wolfram Player or other Wolfram Language products.

A linear Diophantine equation in two variables is an equation of the form , where , , and are integers and solutions are sought in integers. This Demonstration shows Euler&aposs method for solving such an equation.

When using the second method dividing by means finding an integer such that , while in the first it is the integer part of .

As an example, consider the linear Diophantine equation rewrite it as . Introduce the variable and simplify to get a new equation, . Solving for gives , and finally . Assigning integer values to gives all possible solutions to the original equation.

If we rewrite the equation as one more step is needed.

In this example, we can write for integers and (i.e. ), so the process terminates on the first iteration. If there had been a fraction, another variable would be introduced to produce a new equation between the last two variables and . After a finite number of iterations, the process must end because the coefficients are decreasing positive integers. Assigning integer values to the last variable and substituting back up the chain gives all solutions to the original equation in and .

Contributed by: Izidor Hafner  (January 2014)
Open content licensed under CC BY-NC-SA

Solving linear Diophantine equation

where and c are all integers and are integer variables.

Case 1: n = 2

If the linear Diophantine equations has only two variables (n = 2), extended Euclid’s algorithm can be used to find the solutions. If solvable, we will have to introduce one parameter. The general form of the equation when n = 2 can be expressed as ax + by = c. This is solvable if and only if gcd(a, b) divides c. i.e gcd(c, gcd(a, b)) = gcd(a, b). Here gcd stands for greatest common divisor. Let x0 and y0 be a solution, then it can be easily noticed that, x0 + bt and y0 -at are also solutions. We can use this fact to find all the solutions when an initial solution has been found.

We start by dividing both sides of the equation by gcd(a, b). If gcd(a, b) does not divide c, then there are no solutions. gcd(4, 6) = 2 and 2 divides 10 so this is solvable and dividing by 2 yields, 5x + 7y = 5. Now we should apply extended Euclid’s algorithm. What the algorithm does is finding three values x0, y0 and d such that ax0 + by0 = d. Here d = gcd(a, b). Since we divided the equation by gcd(a, b) at the very beginning, d will always be 1 in our case. Below is a simplified version of the algorithm used in the solver.

x0, y0, d = extended_euclid(b, a%b)

extended_euclid(5, 7) returns (3, -2 , 1), i.e 5*3 – 7*2 = 1. Following is the procedure if you were work this out manually.

Now starting from the line before the last one we can start going back, 1 = 5 – 2*2 = 5 – 2*(7 – 5*1) = 3*5 – 2*7 which gives the desired result. Multiplying 5*3 +7*(-2) = 1 by 5 we get 5*15 +7*(-10) = 5 which implies that 15 and -10 are solutions of the initial equation. So all the solutions can be expressed as, x = 15+7t, y = -10-5t where t is an integer.

Case 2: n > 2

If n > 2, we can express two of the variables using the rest of the variables and a parameter. Below is an example for this case.

Solution of 2x + 3y + 4z = 5

Let’s set x = x and assume y = ax + bm + c and z = dx + em + f where a, b, c, d, e, f are constants to be found. m is the introduced parameter. Plug in these in original equation,

2x + 3(ax + bm + c) + 4(dx + em + f) = 5

By comparing the two sides we get the set of equations, 3c + 4f = 5, 2 + 3a + 4d = 0 and 3b + 4e = 0 all of which are two variable linear DEs. If these can be solved, these will yield infinite number of solutions and any pair of values will be acceptable for (a, d), (b, e) and (c, f). In this example all three equations are solvable and (a, d) = (2, -2) , (b, e) = (4, -3) and (e, f) = (3, -1) would do the trick. Then the general solution is, x = x, y = 2x + 4m + 3 and z = -2x – 3m – 1

In the solver module when more than two variables are involved it is rearranged leaving two variable in one side and all the other variables and constant term in the other side. For example 2x + 3y + 4z = 5 is assumed to be in the form 2x + 3y = 5 – 4z. Then it finds a separate solution for each of the two equations 2x + 3y = 5 –>(1) and 2x + 3y = -4z –>(2). First equation returns a parametric solution as discussed in case 1 and the solutions for second one can be found by finding a basic solution(not parametric) for 2x + 3y = -4 and multiplying it by z. Adding the two solutions for (1) and (2) yields the solution of the original equation.

Linear Diophantine Equations

A Diophantine problem is one in which the solutions are required to be integers. Abusing terminology, I'll refer to Diophantine equations, meaning equations which are to be solved over the integers.

For example, the equation has many solutions over the reals. Here's a solution:

However, this equation has no nonzero integer solutions. This is a special case of Fermat's Last Theorem.

On the other hand, the following equation has infinitely many integer solutions:

and are examples of solutions.

In this section, I'll look at equations like the last one. They're called linear Diophantine equations.

Theorem. Let . Consider the Diophantine equation

(a) If , there are no solutions.

(b) If , there are infinitely many solutions of the form

Here is a particular solution, and .

If you've had a course in differential equations, you may have seen something like this. and give a general solution to the homogeneous equation

is a particular solution to . Their sum gives a general solution to the given (nonhomogeneous) equation.

Before I give the proof, I'll give some examples, and also discuss the three variable equation .

Example. Solve .

Since , there are infinitely many solutions. Divide the equation by 3 to get

By inspection, and is a particular solution. Hence, the general solution is

For example, setting produces the solution , .

In general, you may not be able to see a particular solution by inspection. In that case, you can use the Extended Euclidean algorithm to generate one. We'll see how to do this in examples that follow.

Example. Solve .

Since , the equation has no solutions.

Example. Find all the solutions to the following Diophantine equation for which x and y are both positive.

, so there are solutions.

It is too hard to guess a particular solution, so I'll use the Extended Euclidean algorithm:

Matching this with the given equation , I see that is a particular solution. The general solution is

I want solutions for which x and y are both positive. So

The integers which satisfy both of these inequalities are . Here are the values of x and y:

The solutions are , , and .

The requirement that the solutions be positive can come up in real-world problems.

Example. Phoebe buys large shirts for $18 each and small shirts for $11 each. The shirts cost a total of $1188. What is the smallest total number of shirts she could have bought?

Let x be the number of large shirts and let y be the number of small shirts. Then

Since , there are solutions.

I'll use the Extended Euclidean algorithm to get a particular solution:

and is a particular solution. The general solution is

Since the number of shirts can't be negative, I have and .

Thus, .

The total number of shirts is

For , this is smallest for , which gives

She bought 66 large shirts, no small shirts, and a total of 66 shirts.

Consider a 3-variable equation

The equation has solutions if and only if . If it has solutions, there will be infinitely many, determined by two integer parameters.

You can solve a 3-variable equation by reducing it to a 2-variable equation. Group the first two terms and factor out the greatest common divisor of their coefficients. Introduce a new variable, defining it to be what is left after the greatest common divisor is factored out. The new equation is a 2-variable Diophantine equation, which you can solve using the method described earlier.

Example. Find the general solution to the following Diophantine equation.

Let .

and is a particular solution. So

and is a particular solution. The general solution is

A general linear Diophantine equation has the form

There are solutions if . If there is a solution, it will in general have parameters --- exactly as you'd expect from linear algebra.

Here's the proof of the theorem for the two-variable case.

Proof. (two variable case) Consider the linear Diophantine equation

Case 1. Suppose . If x and y are solutions to the equation, then

This contradiction shows that there cannot be a solution.

Case 2. Suppose . Write for . There are integers m and n such that

Hence, , , is a solution.

Suppose , , is a particular solution. Then

This proves that , is a solution for every .

Finally, I want to show that every solution has this form. Suppose then that is a solution. Then and imply

Now divides the left side, so it divides the right side. However, . Therefore,

Substitute back into the last x-y equation above:


In mathematics, an equation is a statement that asserts the equality of two expressions, which are connected by the equals sign " cite_ref-:0_2-0" >[2] [3] [4] The word equation and its cognates in other languages may have subtly different meanings for example, in French an équation is defined as containing one or more variables, while in English, any equality is an equation. [5]

Solving an equation containing variables consists of determining which values of the variables make the equality true. The variables for which the equation has to be solved are also called unknowns, and the values of the unknowns that satisfy the equality are called solutions of the equation. There are two kinds of equations: identities and conditional equations. An identity is true for all values of the variables. A conditional equation is only true for particular values of the variables. [6] [7]

An equation is written as two expressions, connected by an equals sign (" cite_ref-:1_3-1" >[3] The expressions on the two sides of the equals sign are called the "left-hand side" and "right-hand side" of the equation. Very often the right-hand side of an equation is assumed to be zero. Assuming this does not reduce the generality, as this can be realized by subtracting the right-hand side from both sides.

The most common type of equation is a polynomial equation (commonly called also an algebraic equation) in which the two sides are polynomials. The sides of a polynomial equation contain one or more terms. For example, the equation

has left-hand side A x 2 + B x + C − y +Bx+C-y> , which has four terms, and right-hand side 0 , consisting of just one term. The names of the variables suggest that x and y are unknowns, and that A , B , and C are parameters, but this is normally fixed by the context (in some contexts, y may be a parameter, or A , B , and C may be ordinary variables).

An equation is analogous to a scale into which weights are placed. When equal weights of something (e.g., grain) are placed into the two pans, the two weights cause the scale to be in balance and are said to be equal. If a quantity of grain is removed from one pan of the balance, an equal amount of grain must be removed from the other pan to keep the scale in balance. More generally, an equation remains in balance if the same operation is performed on its both sides.

In Cartesian geometry, equations are used to describe geometric figures. As the equations that are considered, such as implicit equations or parametric equations, have infinitely many solutions, the objective is now different: instead of giving the solutions explicitly or counting them, which is impossible, one uses equations for studying properties of figures. This is the starting idea of algebraic geometry, an important area of mathematics.

Algebra studies two main families of equations: polynomial equations and, among them, the special case of linear equations. When there is only one variable, polynomial equations have the form P(x) = 0, where P is a polynomial, and linear equations have the form ax + b = 0, where a and b are parameters. To solve equations from either family, one uses algorithmic or geometric techniques that originate from linear algebra or mathematical analysis. Algebra also studies Diophantine equations where the coefficients and solutions are integers. The techniques used are different and come from number theory. These equations are difficult in general one often searches just to find the existence or absence of a solution, and, if they exist, to count the number of solutions.

Differential equations are equations that involve one or more functions and their derivatives. They are solved by finding an expression for the function that does not involve derivatives. Differential equations are used to model processes that involve the rates of change of the variable, and are used in areas such as physics, chemistry, biology, and economics.

The "Robert Recorde, who considered that nothing could be more equal than parallel straight lines with the same length. [1]

Watch the video: Συνήθεις Διαφορικές Εξισώσεις: Ομογενείς Α Τάξης (November 2021).