• Aucun résultat trouvé

LINEAR DIOPHANTINE EQUATIONS

Dans le document WITH JAVA APPLETS CRYPTOGRAPHY INTRODUCTION TO (Page 105-108)

Linear Diophantine Equations and Linear Congruences

4.1 LINEAR DIOPHANTINE EQUATIONS

Diophantine equations are special types of equations. What characterizes them is that their solutions must be integers. Consider the following equation:

12x+ 27y= 32.

This is called a linear diophantine equation in two variables. It is diophantine because we are only interested in integer solutions for the variables, and it is linear because the highest power of any variable in the equation is 1. The preceding equation has no integer solutions forxandy(try to find one, if you like), whereas the following equation

12x+ 27y= 30

has infinitely many integral solutions! One solution is x=⫺20,y= 10. (Try to find some more, or a formula which gives them all.) What distinguishes the first equation from the sec-ond? Proposition 16 will provide the answer to this; it will tell us which such equations have solutions, and which do not. The proof is constructive, in that it shows how to find the solutions when they exist.

PROPOSITION 16. Letaandbbe nonzero integers with d= (a,b). If d|c, the integer solutionsxandyof the equation ax+by=carex=x0+bn/d,y=y0⫺an/d, where x=x0, y=y0is a particular solution. If dⱉc, the equation has no integer solutions.

Proof. Supposexandyare integers such that ax+by=c. (*)

Then, since d|aandd|b, by proposition 2, dalso divides c. Thus, the contrapositive says ifddoes not divide cthen there are no integral solutions. So, suppose d|c. Proposition 12 demonstrates the existence of integers sandtsuch that

d=as+bt.

Sinced|c, there is an integer esuch that de=c. Multiply both sides of d=as+btbye to obtain

c=de= (as+bt)e=a(se) + b(te)

and we see then that x=x0=se,y=y0=teis a particular solution to (*). Now, let x=x0+ bn/d,y=y0⫺an/d, where nis any integer. Note that this is also a solution:

ax+by=ax0+abn/d+by0⫺ban/d=ax0+by0=c.

We must show that every solution of (*) must be of this form. Suppose xandyare inte-gers such that ax+by=c. Since ax0+by0=c, we subtract and rearrange terms to get

(ax+by)⫺(ax0+by0) = 0 a(x⫺x0) + b(y⫺y0) = 0

a(x⫺x0) = b(y0⫺y).

Divide both sides of the previous equation by dto get (a/d)(x⫺x0) = (b/d)(y0⫺y).

Proposition 7 tells us that (a/d,b/d) = 1, and we use proposition 13 to then show that (a/d)|(y0⫺y). Thus, we can write an/d=y0⫺yfor some integer n, and so y=y0⫺an/d. If we insert this value of yback into

a(x⫺x0) = b(y0⫺y)

we get a(x⫺x0) = b(an/d), and hence x=x0+bn/d, as desired. I

E

XAMPLE

.

The previous theorem allows us to find all solutions of the two equations presented at the beginning of this chapter. Consider again the equation 12x+ 27y= 32. The gcd of 12 and 27 is 3, which does not divide 32. Thus, this equation has no integer solutions. But the second equation, 12x+ 27y= 30, has infinitely many integer solutions since 3|30. We find all the solutions by first finding a particular solution. First, note that integers sandtexist which solve 12s+ 27t= (12, 27) = 3, and proposition 12 tells us how to compute them.

They are s=⫺2,t= 1. Thus, since 12(⫺2) + 27 ⭈1 = 3, we can multiply both sides of the equation by 10 to get 12(⫺20) + 27 ⭈10 = 30. So a particular solution to 12x+ 27y= 30 is x0=⫺20,y0= 10. Proposition 16 says all of the solutions to 12x+ 27y= 30 are then given byx=⫺20 + 27n/3 = ⫺20 + 9n, and y= 10 ⫺12n/3 = 10 ⫺4n,∀integersn.

E

XAMPLE

.

Diophantine equations have real-world applications as well. Suppose you are at the grocery store with 4 dollars and 27 cents. Apples sell for 35 cents, and oranges for 49 cents. What combination of apples and oranges (if any) will exhaust your money? (Assume there is no sales tax.) We wish to find all integer solutions to the equation 35x+ 49y= 427.

First, compute (35, 49) = 7, and note that 427 = 61 ⭈7, so 7|427. Thus, the equation has infi-nitely many solutions. We find a particular solution by first solving 35s+ 49t= 7, and get s= 3, t=⫺2. Multiply both sides of 35 ⭈3 + 49(⫺2) = 7 by 61 to get 35 ⭈183 + 49(⫺122)

= 427, and get a particular solution to our equation; that is, x0= 183, and y0=⫺122. The

general solutions to the equation are then x= 183 + 7n,y=⫺122⫺5n∀integersn. Since we obviously cannot buy a negative number of apples or oranges, we need to find which of these solutions are nonnegative. Thus we find that x= 183 + 7n≥0, or n≥⫺183/7 =

⫺26 1/7, and y=⫺122⫺5n≥0, or n≤⫺122/5 = ⫺24 2/5. Thus, ncan only attain the val-ues⫺26, and ⫺25. Each of these values of nproduces a satisfactory solution to our prob-lem, which can be seen in the Table 4.1.

Note that with diophantine equations, it is only necessary to solve the equation ax+by

=cwherea,b, and care all positive, for if any of these are negative, the solution is still eas-ily obtained by inverting some of the signs. For example, suppose x=x⬘andy=y⬘is a solution to ax+by=c, where a,b, and care all positive. Then we have all of the following in case any one of the constants changes sign.

x=⫺x⬘,y=⫺y⬘is a solution to ax+by=⫺c, x=⫺x⬘,y=y⬘is a solution to ⫺ax+by=c, and x=x⬘,y=⫺y⬘is a solution to ax⫺by=c

One can easily solve for the other cases when 2 or all 3 of the constants change sign.

More cases to consider are when one of a,b, or cis 0. For these cases we have

• x=b,y=⫺ais a solution to ax+by= 0

• x= 0, y=c/bis a solution to 0x+by=c(providedb|c)

• x=c/a,y= 0 is a solution to ax+ 0y=c(provideda|c)

Java Algorithm. We now write for the BigIntegerMath class a method to solve linear diophantine equations ax+by=c. Because of the previous discussion, we will allow only equations where aandbare positive, and cis nonnegative. You may wish to rewrite the method to solve equations when any of a,b, or care negative, or zero. The method will accept the coefficients aandb, and the constant cas BigIntegers. If aorbare not positive, or if c is negative, it will throw an IllegalArgumentException. It will then compute d= (a,b), and ifdⱉc, it will again throw an IllegalArgumentException. Otherwise, it will compute a par-ticular solution x=x⬘,y=y⬘to the equation, and return it in an array of BigIntegers. The element at index 1 will be x⬘, and y⬘will be at index 2. For convenience, the gcd of aandb will be returned at index 0. This is useful if we want to display the general solution.

public static BigInteger[] solveLinearDiophantine (BigInteger a, BigInteger b,

4.1 Linear Diophantine Equations 91

Table 4.1

n Number of apples Number of oranges Total spent (in cents)

– 2 6 – 2 5

1 8 3 + 7 ( – 2 6 ) = 1 1 8 3 + 7 ( – 2 5 ) = 8

– 1 2 2 – 5 ( – 2 6 ) = 8 – 1 2 2 – 5 ( – 2 5 ) = 3

3 5 • 1 + 4 9 • 8 = 4 2 7 3 5 • 8 + 4 9 • 3 = 4 2 7

FIGURE 4.1

BigInteger c) throws IllegalArgumentException {

if (a.compareTo(ZERO)<=0||b.compareTo(ZERO)<=0||c.compareTo(ZERO)<0) throw new IllegalArgumentException

(“All constants must be positive in linear diophantine equation.”);

BigInteger[] euclidAnswers=euclid(a,b);

if (c.mod(euclidAnswers[0]).compareTo(ZERO)!=0) throw new IllegalArgumentException

(“No solution since “+euclidAnswers[0]+” does not divide “+c+”.”);

BigInteger[] answer=new BigInteger[3];

BigInteger q=c.divide(euclidAnswers[0]);

answer[0]=euclidAnswers[0];

answer[1]=q.multiply(euclidAnswers[1]);

answer[2]=q.multiply(euclidAnswers[2]);

return answer;

}

There is an applet called TestDiophantineApplet on the book’s website you can use to solve linear diophantine equations. A screen shot of the applet solving a sample equation is shown in Figure 4.1.

Dans le document WITH JAVA APPLETS CRYPTOGRAPHY INTRODUCTION TO (Page 105-108)