# 3: Linear Diophantine Equations

3: Linear Diophantine Equations

## Linear Diophantine Equations in Three Variables

However now two equations depend on two variables ($k,l$) and one on one variable $l$. In the first solution one equation depends on two variables and two on one variable.

How can I come from a representation like the one from wolfram alpha for the general solution to one where all equations depend on one distinct variable except one equation.

Is there always such a representation? ## $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

## Python3

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.

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.

## How to solve Linear Diophantine equations in programming?

I have read about Linear Diophantine equations such as ax+by=c are called diophantine equations and give an integer solution only if gcd(a,b) divides c .

These equations are of great importance in programming contests. I was just searching the Internet, when I came across this problem. I think its a variation of diophantine equations.

I have two persons,Person X and Person Y both are standing in the middle of a rope. Person X can jump either A or B units to the left or right in one move. Person Y can jump either C or D units to the left or right in one move. Now, I'm given a number K and I have to find the no. of possible positions on the rope in the range [-K,K] such that both the persons can reach that position using their respective movies any number of times. (A,B,C,D and K are given in question).

I think the problem can be solved mathematically using diophantine equations.

I can form an equation for Person X like A x_1 + B y_1 = C_1 where C_1 belongs to [-K,K] and similarly for Person Y like C x_2 + D y_2 = C_2 where C_2 belongs to [-K,K] .

Now my search space reduces to just finding the number of possible values for which C_1 and C_2 are same. This will be my answer for this problem.

To find those values I'm just finding gcd(A,B) and gcd(C,D) and then taking the lcm of these two gcd's to get LCM(gcd(A,B),gcd(C,D)) and then simply calculating the number of points in the range [1,K] which are multiples of this lcm.

My final answer will be 2*no_of_multiples in [1,K] + 1 .

I tried using the same technique in my C++ code, but it's not working(Wrong Answer).

My question is: can anyone please tell me if I'm using diophantine equations correctly ?

If yes, can anyone tell me possible cases where my logic fails.

These are some of the test cases which were given on the site with problem statement.

A B C D K are given as input in same sequence and the corresponding output is given on next line :

This is the link to original problem. I have written the original question in simple language. You might find it difficult, but if you want you can check it:

Please give me some test cases so that I can figure out where am I doing wrong ?

## 3: Linear Diophantine Equations

I used your software to prepare for my algebra exam. I really like the step by step solution process and explanations.
J.R. Turnston, NY

Its a miracle! I looks like it's going to work. I get so close with solving almost all problems and this program has answered my prayers!
John Doer, TX

I do not have any issues. I just wanted to let you know that I am glad I purchased your product. I also appreciate the updates as they not only make for a better looking product, but things seem to be more user friendly now.
Daniel Cotton, NV

This algebra software provides my daughter with the ability to learn independently, offering facts and helpful hints before providing problems for her to solve. It's working out very well . . . I think the software lends itself nicely to helping students throughout the year by supplementing any materials they get in a regular classroom.
Christopher Bowman, TX

The most valuable algebra tutor I have ever come across. It caters not only the basic learners but also the students who are doing advanced algebra. The step-by-step solution to the problems accompanied by explanation of each step makes this software priceless mathematical tool.
Lisa Schuster, NY

## Linear diophantine equations calculator

Linear diophantine equation in two variables is equation of the form:

This calculator is based on the extended Euclidean algorithm written as a continued fraction. However, in some cases (for example, when the coefficient ) simpler methods are used. Also, the calculator does not consider the equations with at least one of the coefficients or equals to , since these cases lead to the ordinary linear equation.

If the coefficient is not divided by the , then the linear diophantine equation in two variables has no solutions. On the contrary, if is divided by , then the specified equation has infinitely many solutions in integers.

To solve a linear diophantine equation in two variables, one first need to find a particular solution and , and then find the general solution using the formulas:

Consider an example of solving a linear diophantine equation in two variables:

Coefficients of the equation: .

Since is divided by , this equation has solutions in integers.

Next, find some specific (particular) solution and of the original equation. To do this, one first need to find a particular solution and of the auxiliary equation with coefficient :

and then multiply the obtained particular solution and of the auxiliary equation by and get a particular solution and of the original equation:

To find a particular solution of the auxiliary equation, we use continued fractions. To do this, we compose a fraction, which is the ratio of the coefficients and , а знаменателем коэффициент .

Transform this fraction into a continuous fraction:

In the resulting continued fraction, discard the last fraction :

The resulting fraction is the ratio of particular solutions and chosen with the correct sign:

By substituting four values in an auxiliary equation, determine that a particular solution is:

Now, to find a particular solution and of the original equation, we multiply the obtained particular solution and of the auxiliary equation by :

Finally, find the final answer using formulas for the general solution:

Our online calculator is able to solve any linear diophantine equation with two unknowns with step by step solution. To get started, one need to input equation and set the variables to find.

## Question : 10.11 Program 3: Linear Diophantine Equations Students, This content is controlled by your instructor, and is not zyBook content. Direct questions or concerns about this content to your instructor. If you have any technical issues with the zyLab submission system, use the "Trouble with lab?" button at the bottom. In this PROGRAM, you will be solving linear

This content is controlled by your instructor, and is not zyBook content. Direct questions or concerns about this content to your instructor. If you have any technical issues with the zyLab submission system, use the "Trouble with lab?" button at the bottom.

In this PROGRAM, you will be solving linear Diophantine equations recursively.

A linear Diophantine equation is an equation in the form:

where a, b, and c are all integers and the solutions will also be integers.

See the following entry in Wikipedia: Linear Diophantine equations.

You will be solving this using the recursive version of the Extended Euclidean algorithm for finding the integers x and y in Bezout's identity:

You will need a Greatest Common Divisor helper function gcd(int x, int y):

A recursive algorithm for GCD:

the GCD of x and y is y if x mod y is 0
otherwise the GCD is the GCD of y and the remainder of x/y

Required Recursive Function

If gcd(a,b) does not divide c, then there is no solution.

If b divides a (the remainder of a / b is 0), then you can just divide by b to get the solution: x = 0, y = c / b.

Otherwise (b does not divide a), through a substitution method, we can come up with a simpler version of the original problem and solve the simpler problem using recursion.

where q is (a / b) (using integer division) and r is the remainder (a % b).

Substituting (bq + r) in for a now:

and now we have the equation in the same form, only with smaller coefficients:

Finally, you recursively call your function on this simpler version of the original problem. Don't forget that this recursive call will actually solve for u and vin this case, so you still have to solve for x and y to get the solution to the original problem:

## A three variable linear diophantine promise problem

Given $a,b,c,sinBbb N$ such that $(a,b,c)=1$ with promise that we have at most one triple $x,y,zinBbb N$ such that $ax+by+cz=s$, what is a good algorithm that runs in $O(log(abcs))$ time to find such a triple or return empty?

I am trying to use FrobeniusSolve as recommended in the comments in this accepted answer here Frobenius number for three numbers. However it seems pretty slow (that is does not seem logarithmic in $abcs$).

What is the complexity of this method that is implemented in $mathsf$? Is there an $O(log(abcs))$ time complexity method or something close to it?

## Linear Diophantine Equations in Two Variables Interact on desktop, mobile and cloud with the free Wolfram Player or other Wolfram Language products.

A linear Diophantine equation in two variables has the form , with , , and integers, where solutions are sought in integers. The corresponding homogeneous equation is , and it always has infinitely many solutions , where is an integer. If is a solution of the nonhomogeneous equation, all of its solutions are of the form . Suppose and are positive and relative prime. Then the distance between two consecutive solutions is , so the equation always has a solution in non-negative integers if .

Contributed by: Izidor Hafner  (January 2014)
Code from: Emmanuel Garces Medina
Open content licensed under CC BY-NC-SA