• No results found

Number Theory

4.5 RELATIVE PRIMALITY

When gcd(m, n) =1, the integersmandnhave no prime factors in common and we say that they're relatively prime.

This concept is so important in practice, we ought to have a special notation for it; but alas, number theorists haven't agreed on a very good one yet. Therefore we cry: Hear us, O Mathematicians of the World! Let us not wait any longer! We can make many formulas clearer by adopting a new notation now! Let us agree to write `mn', and

Like perpendicular lines don't have a common direc- tion, perpendicular numbers don't have common factors.

to say \m is prime ton," ifmand n are relatively prime. In other words, let us declare that

A fraction m/n is in lowest terms if and only if m n. Since we reduce fractions to lowest terms by casting out the largest common factor of numerator and denominator, we suspect that, in general,

m/gcd(m, n) n/gcd(m, n); (4.27) and indeed this is true. It follows from a more general law, gcd(km, kn) = kgcd(m, n), proved in exercise 14.

Therelation has a simple formulation when we work with the prime- exponent representations of numbers, because of the gcd rule (4.14):

m n ⇐⇒ min(mp, np) = 0 for allp. (4.28)

Furthermore, sincemp andnpare nonnegative, we can rewrite this as The dot product is zero, like orthogonal vectors.

m n ⇐⇒ mpnp = 0 for allp. (4.29)

And now we can prove an important law by which we can split and combine tworelations with the same left-hand side:

k m and k n ⇐⇒ k mn . (4.30)

In view of (4.29), this law is another way of saying that kpmp = 0 and kpnp=0if and only ifkp(mp+np) =0, whenmpandnpare nonnegative.

There's a beautiful way to construct the set of all nonnegative fractions

m/n with m n, called the Stern{Brocot tree because it was discovered Interesting how mathematicians will say \discov- ered" when abso- lutely anyone else would have said \invented."

independently by Moriz Stern [339], a German mathematician, and Achille Brocot [40], a French clockmaker. The idea is to start with the two fractions

(0

1,10)and then to repeat the following operation as many times as desired:

Insert m+m0

n+n0 between two adjacent fractions

m n and

m0

n0 .

The new fraction(m+m0)/(n+n0)is called the mediant ofm/nandm0/n0.

For example, the rst step gives us one new entry between 0 1 and 10, 0

1, 11, 10;

and the next gives two more:

0

1, 12, 11, 21, 10.

The next gives four more,

0

and then we'll get 8, 16, and so on. The entire array can be regarded as an innite binary tree structure whose top levels look like this:

I guess 1/0 is innity, \in lowest

terms." 0 1 1 0 p p p p p p p p p p p p p p p p p p 1 p p p p p p p p p p p p p p p p p p 1 ©©©© © HH HHH 1 2 21 ¡¡ @@ ¡¡ @@ 1 3 23 32 31 ¢¢ AA ¢¢ AA ¢¢ AA ¢¢ AA 1 4 25 35 34 43 53 52 41 ¤ C ¤ C ¤C ¤ C ¤ C ¤ C ¤ C ¤ C 1 5 27 38 37 47 58 57 45 54 75 85 74 73 83 72 51 Each fraction ism+m0

n+n0, where mn is the nearest ancestor above and to the left,

and m0

n0 is the nearest ancestor above and to the right. (An \ancestor" is a

fraction that's reachable by following the branches upward.) Many patterns can be observed in this tree.

Why does this construction work? Why, for example, does each mediant fraction(m+m0)/(n+n0)turn out to be in lowest terms when it appears in

this tree? (Ifm,m0,n, andn0were all odd, we'd get even/even; somehow the

Conserve parody.

construction guarantees that fractions with odd numerators and denominators never appear next to each other.) And why do all possible fractionsm/noccur exactly once? Why can't a particular fraction occur twice, or not at all?

All of these questions have amazingly simple answers, based on the fol- lowing fundamental fact: If m/nand m0/n0 are consecutive fractions at any

stage of the construction, we have

m0nmn0 = 1 . (4.31)

This relation is true initially (1·1−0·0 = 1); and when we insert a new mediant(m+m0)/(n+n0), the new cases that need to be checked are

(m+m0)nm(n+n0) = 1;

m0(n+n0) − (m+m0)n0 = 1 .

Both of these equations are equivalent to the original condition (4.31) that they replace. Therefore (4.31) is invariant at all stages of the construction.

Furthermore, ifm/n < m0/n0 and if all values are nonnegative, it's easy

to verify that

A mediant fraction isn't halfway between its progenitors, but it does lie some- where in between. Therefore the construction preserves order, and we couldn't

possibly get the same fraction in two dierent places. True, but if you get a compound frac- ture you'd better go see a doctor.

One question still remains. Can any positive fraction a/b with a b

possibly be omitted? The answer is no, because we can conne the construc- tion to the immediate neighborhood ofa/b, and in this region the behavior is easy to analyze: Initially we have

m n = 01 < ¡a b ¢ < 1 0 = m 0 n0 ,

where we put parentheses around a

b to indicate that it's not really present

yet. Then if at some stage we have

m n < ¡a b ¢ < m0 n0 ,

the construction forms(m+m0)/(n+n0)and there are three cases. Either

(m+m0)/(n+n0) =a/band we win; or(m+m0)/(n+n0)< a/b and we

can set m ← m+m0, n n+n0; or (m+m0)/(n+n0) > a/b and we

can setm0 m+m0, n0n+n0. This process cannot go on indenitely,

because the conditions

a b−mn > 0 and m 0 n0 − ab > 0 imply that an−bm 1 and bm0an0 1; hence (m0+n0)(anbm) + (m+n)(bm0an0) m0+n0+m+n;

and this is the same asa+bm0+n0+m+nby (4.31). Eithermornor

m0 or n0 increases at each step, so we must win after at mosta+bsteps.

The Farey series of orderN, denoted by FN, is the set of all reduced

fractions between 0 and 1 whose denominators are N or less, arranged in increasing order. For example, ifN=6 we have

F6 = 01,16,51,14,13,25,12,35,23,34,45,56,11.

We can obtain FN in general by starting with F1= 01,11 and then inserting

mediants whenever it's possible to do so without getting a denominator that is too large. We don't miss any fractions in this way, because we know that the Stern{Brocot construction doesn't miss any, and because a mediant with denominator Nis never formed from a fraction whose denominator is> N. (In other words, FN denes a subtree of the Stern{Brocot tree, obtained by

pruning o unwanted branches.) It follows that m0nmn0 = 1 whenever

m/nandm0/n0 are consecutive elements of a Farey series.

This method of construction reveals that FN can be obtained in a simple

way from FN−1: We simply insert the fraction (m+m0)/N between con-

secutive fractionsm/n, m0/n0 of F

N−1 whose denominators sum toN. For

example, it's easy to obtain F7 from the elements of F6, by inserting 17, 27, . . ., 6

7 according to the stated rule:

F7 = 01,17,16,15,14,27,13,25,37,12,47,35,23,57,34,45,56,67,11.

WhenN is prime, N−1 new fractions will appear; but otherwise we'll have fewer than N−1, because this process generates only numerators that are relatively prime toN.

Long ago in (4.5) we proved | in dierent words | that whenevermn

and0 < mnwe can nd integersaandbsuch that

ma−nb = 1 . (4.32)

(Actually we saidm0m+n0n=gcd(m, n), but we can write1for gcd(m, n),

aform0, andb for−n0.) The Farey series gives us another proof of (4.32),

because we can letb/abe the fraction that precedesm/n in Fn. Thus (4.5)

is just (4.31) again. For example, one solution to3a−7b=1isa=5,b=2, since2

5 precedes 37 in F7. This construction implies that we can always nd a

solution to (4.32) with0b < a < n, if0 < mn. Similarly, if0n < m

andmn, we can solve (4.32) with 0 < abmby letting a/b be the fraction that follows n/min Fm.

Sequences of three consecutive terms in a Farey series have an amazing property that is proved in exercise 61. But we had better not discuss the Farey series any further, because the entire Stern{Brocot tree turns out to be

Farey 'nough.

even more interesting.

We can, in fact, regard the Stern{Brocot tree as a number system for representing rational numbers, because each positive, reduced fraction occurs exactly once. Let's use the lettersL and R to stand for going down to the left or right branch as we proceed from the root of the tree to a particular fraction; then a string of L's and R's uniquely identies a place in the tree. For example,LRRLmeans that we go left from 1

1 down to 12, then right to 23,

then right to 3

4, then left to 57. We can considerLRRLto be a representation

of 5

7. Every positive fraction gets represented in this way as a unique string

ofL's andR's.

Well, actually there's a slight problem: The fraction 1

1 corresponds to

the empty string, and we need a notation for that. Let's agree to call it I, because that looks something like1and it stands for \identity."

This representation raises two natural questions: (1) Given positive inte- gersmandnwithmn, what is the string of L's andR's that corresponds to m/n? (2) Given a string ofL's andR's, what fraction corresponds to it? Question 2 seems easier, so let's work on it rst. We dene

f(S) = fraction corresponding toS

whenS is a string ofL's andR's. For example, f(LRRL) = 5 7.

According to the construction, f(S) = (m+m0)/(n+n0) if m/n and

m0/n0 are the closest fractions preceding and followingSin the upper levels

of the tree. Initially m/n = 0/1 and m0/n0 = 1/0; then we successively

replace eitherm/norm0/n0 by the mediant(m+m0)/(n+n0)as we move

right or left in the tree, respectively.

How can we capture this behavior in mathematical formulas that are easy to deal with? A bit of experimentation suggests that the best way is to maintain a2×2matrix M(S) = µ n n0 m m0

that holds the four quantities involved in the ancestral fractions m/n and

m0/n0 enclosing f(S). We could put them's on top and then's on the bot-

tom, fractionwise; but this upside-down arrangement works out more nicely because we haveM(I) =¡1 00 1¢when the process starts, and¡1 0

0 1 ¢

is tradition- ally called the identity matrixI.

A step to the left replacesn0 byn+n0 andm0 bym+m0; hence

M(SL) = µ n n+n0 m m+m0 ¶ = µ n n0 m m0 ¶ µ 1 1 0 1 ¶ = M(S) µ 1 1 0 1 ¶ .

(This is a special case of the general rule

µ a b c d ¶ µ w x y z ¶ = µ aw+by ax+bz cw+dy cx+dz ¶

for multiplying2×2 matrices.) Similarly it turns out that If you're clueless about matrices, don't panic; this book uses them only here. M(SR) = µ n+n0 n0 m+m0 m0 ¶ = M(S) µ 1 0 1 1 ¶ .

Therefore if we deneLandR as2×2 matrices,

L = µ 1 1 0 1 ¶ , R = µ 1 0 1 1 ¶ , (4.33)

we get the simple formulaM(S) =S, by induction on the length ofS. Isn't that nice? (The lettersLandRserve dual roles, as matrices and as letters in the string representation.) For example,

M(LRRL) = LRRL = ¡1 10 1¢¡1 01 1¢¡1 01 1¢¡1 10 1¢ = ¡2 11 1¢¡1 11 2¢ = ¡3 42 3¢; the ancestral fractions that encloseLRRL = 5

7 are 23 and 34. And this con-

struction gives us the answer to Question 2:

f(S) = f µµ n n0 m m0 ¶¶ = m+m 0 n+n0 . (4.34)

How about Question 1? That's easy, now that we understand the fun- damental connection between tree nodes and2×2matrices. Given a pair of positive integersmandn, with mn, we can nd the position ofm/n in the Stern{Brocot tree by \binary search" as follows:

S := I;

while m/n6=f(S) do

if m/n < f(S) then¡output(L); S := SL¢

else ¡output(R); S := SR¢.

This outputs the desired string ofL's andR's.

There's also another way to do the same job, by changingmandninstead of maintaining the stateS. IfSis any2×2matrix, we have

f(RS) = f(S) +1

because RS is like S but with the top row added to the bottom row. (Let's look at it in slow motion:

S = µ n n0 m m0 ¶ ; RS = µ n n0 m+n m0+n0 ¶ ; hencef(S) = (m+m0)/(n+n0)andf(RS) =¡(m+n) + (m0+n0)¢/(n+n0).)

If we carry out the binary search algorithm on a fractionm/n with m > n, the rst output will beR; hence the subsequent behavior of the algorithm will havef(S)exactly1greater than if we had begun with(m−n)/n instead of

m/n. A similar property holds forL, and we have

m n = f(RS) ⇐⇒ m−n n = f(S), whenm > n; m n = f(LS) ⇐⇒ m n−m = f(S), whenm < n.

This means that we can transform the binary search algorithm to the following matrix-free procedure:

while m6=n do

if m < n then¡output(L); n := n−m¢

else ¡output(R); m := m−n¢.

For example, givenm/n=5/7, we have successively

m= 5 5 3 1 1

n= 7 2 2 2 1

output L R R L

in the simplied algorithm.

Irrational numbers don't appear in the Stern{Brocot tree, but all the rational numbers that are \close" to them do. For example, if we try the binary search algorithm with the numbere = 2.71828 . . ., instead of with a fractionm/n, we'll get an innite string ofL's andR's that begins

RRLRRLRLLLLRLRRRRRRLRLLLLLLLLRLR . . . .

We can consider this innite string to be the representation ofein the Stern{ Brocot number system, just as we can represent e as an innite decimal

2.718281828459. . . or as an innite binary fraction (10.101101111110 . . .)2.

Incidentally, it turns out thate's representation has a regular pattern in the Stern{Brocot system:

e = RL0RLR2LRL4RLR6LRL8RLR10LRL12RL . . .;

this is equivalent to a special case of something that Euler [105] discovered when he was 24 years old.

From this representation we can deduce that the fractions

1 1, R 2 1, R 3 1, L 5 2, R 8 3, R 11 4, L 19 7, R 30 11, L 49 18, L 68 25, L 87 32, L 106 39, R 193 71 , L 299 110, R 492 181, R 685 252, R 878 323, R . . .

are the simplest rational upper and lower approximations to e. For if m/n

does not appear in this list, then some fraction in this list whose numerator is mand whose denominator is nlies betweenm/nande. For example,

27

10 is not as simple an approximation as 197 = 2.714 . . ., which appears in

the list and is closer to e. We can see this because the Stern{Brocot tree not only includes all rationals, it includes them in order, and because all fractions with small numerator and denominator appear above all less simple ones. Thus, 27

e = RRLRRLR . . .. Excellent approximations can be found in this way. For example,1264

465 2.718280agrees witheto six decimal places; we obtained this

fraction from the rst 19 letters ofe's Stern{Brocot representation, and the accuracy is about what we would get with 19 bits ofe's binary representation. We can nd the innite representation of an irrational number α by a simple modication of the matrix-free binary search procedure:

ifα < 1 then¡output(L); α := α/(1−α)¢

else ¡output(R); α := α−1¢.

(These steps are to be repeated innitely many times, or until we get tired.) Ifαis rational, the innite representation obtained in this way is the same as before but withRL appended at the right ofα's (nite) representation. For

example, ifα=1, we get RLLL . . ., corresponding to the innite sequence of fractions 1

1, 21, 32, 34, 54,. . ., which approach1 in the limit. This situation is

exactly analogous to ordinary binary notation, if we think ofLas0andRas1: Just as every real numberx in [0 . . 1) has an innite binary representation

(.b1b2b3. . .)2 not ending with all 1's, every real number α in [0 . .∞) has

an innite Stern{Brocot representation B1B2B3. . . not ending with all R's.

Thus we have a one-to-one order-preserving correspondence between[0 . . 1)

and[0 . .∞)if we let0↔Land1↔R.

There's an intimate relationship between Euclid's algorithm and the Stern{Brocot representations of rationals. Given α = m/n, we get bm/nc R's, then ¥n/(mmodn)¦ L's, then ¥(mmodn)±¡nmod(mmodn)¢¦ R's, and so on. These numbersmmodn,nmod(mmodn),. . . are just the val- ues examined in Euclid's algorithm. (A little fudging is needed at the end to make sure that there aren't innitely many R's.) We will explore this relationship further in Chapter 6.