# Greatest common divisor and least common multiple

## Greatest common divisor (gcd)

The numerator and the denominator of a fraction can have the same prime factors in common.
When a fraction is reduced, *common factors* (aka *common divisors*) are canceled out.
This is what we learned in the last chapter.
The total factor canceled out when a fraction is reduced
to lowest terms is the *greatest common divisor (gcd)*
(aka *greatest common factor* or *highest common divisor/factor*).
Thus, the gcd of two (or more) given integers is the largest integer that
divides each of the given integers.

$$\frac{30}{45}=\frac{2\times \overline{)3}\times \overline{)5}}{3\times \overline{)3}\times \overline{)5}}=\frac{2}{3}$$

$$\mathrm{gcd}\left(\mathrm{30,45}\right)=3\times 5=15$$

The greatest factor that 30 and 45 have in common is 15. By dividing both the numerator and denominator by their gcd, a fraction can be reduced to lowest terms.

If the given integers do not share any prime factors, their gcd equals 1. The only integer factor they have in common is 1. The numerator and the denominator both divided by their gcd yields a fraction in its lowest terms (like 2/3 in the example above). If a fraction is in lowest terms, the gcd of the numerator and denominator is 1. For example, $\frac{6}{35}$ is in lowest terms:

$$\mathrm{gcd}\left(\mathrm{6,35}\right)=1$$

$$\frac{6}{35}=\frac{2\times 3}{5\times 7}=\frac{6}{35}$$

If one (or both) of the arguments is 1 or a prime number, the gcd is 1, unless one of the arguments is 0, because if one of the arguments is 0, the gcd is the other argument.

$$\mathrm{gcd}\left(\mathrm{13,0}\right)=13$$

### Euclidean algorithm

So far we (prime) factorized the integers to find the gcd. This is fundamentally correct, but not always most efficient. Factorizing larger numbers may require a fair amount of arithmetic. An efficient way to find the gcd of two integers is the famous Euclidean algorithm (c. 300 BCE).

The recipe is to divide the greatest of two integers by the smallest, divide the smallest by the remainder, divide the remainder by the new remainder and so on, until the new remainder is zero. The second last remainder is the gcd of the two initial integers. Next examples, using long division, will make things clear:

1 | 7 | |||||||||

54 | ) | 9 | 3 | 0 | ||||||

5 | 4 | ↓ | ||||||||

3 | 9 | 0 | ||||||||

3 | 7 | 8 | 4 | |||||||

1 | 2 | ) | 5 | 4 | ||||||

4 | 8 | 2 | ||||||||

6 | ) | 1 | 2 | |||||||

1 | 2 | |||||||||

0 |

930 | / | 54 | = | 17 | r | 12 |

54 | / | 12 | = | 4 | r | 6 |

12 | / | 6 | = | 2 | r | 0 |

1 | |||||||||

60 | ) | 8 | 4 | ||||||

6 | 0 | 2 | |||||||

2 | 4 | ) | 6 | 0 | |||||

4 | 8 | 2 | |||||||

1 | 2 | ) | 2 | 4 | |||||

2 | 4 | ||||||||

0 |

84 | / | 60 | = | 1 | r | 24 |

60 | / | 24 | = | 2 | r | 12 |

24 | / | 12 | = | 2 | r | 0 |

By dividing both the numerator and denominator by their gcd, a fraction can be reduced to lowest terms:

$$\frac{54}{930}=\frac{6\times 9}{6\times 155}=\frac{9}{155}$$

$$\frac{60}{84}=\frac{12\times 5}{12\times 7}=\frac{5}{7}$$

In JavaScript:

```
function gcd(a, b){
[b,a] = [a,b].sort(() => a - b);
let oldR = a,
newR = b,
t;
while (newR !== 0) { // while newRemainder ≠ 0
t = newR;
newR = oldR%newR; // newRemainder = oldRemainder mod newRemainder
oldR = t;
}
return oldR;
}
gcd(7,91); // returns: 7
```

gcd(`a`,`b`), 0 ≤ a,b < 100 000.

Appendix A provides a proof of why the Euclidean algorithm works.

## Least common multiple (lcm)

When fractions are added or subtracted, they need to have equal denominators. Fractions with *unequal* denominators
need to be converted to the same denominator first. In the last chapter this was accomplished by
applying rule 5. This always works, but is not always most efficient.

$$\begin{array}{l}\frac{1}{2}+\frac{5}{6}=\frac{6}{12}+\frac{10}{12}=\frac{16}{12}\\ \phantom{\frac{16}{12}}=\frac{4\times 4}{4\times 3}=\frac{4}{3}=1\u2064\frac{1}{3}\end{array}$$

In the above example, 12 is a multiple of both 2 and 6. The new denominator 12 is a *common multiple* of the initial denominators,
but *not* the *least common multiple (lcm)*
(aka *smallest common multiple* or *lowest common multiple*).
The lcm of 2 and 6 is 6.

$$\mathrm{lcm}\left(\mathrm{2,6}\right)=6$$

$$\frac{1}{2}+\frac{5}{6}=\frac{3}{6}+\frac{5}{6}=\frac{8}{6}=\frac{4}{3}$$

In the example above, a "reverse reduction" is applied to 1/2. Rule 3 is used to convert 1/2 to 3/6. To convert denominator 2 to the new common denominator 6, it needs to be multiplied by 3. In order to retain an equal fraction, also the numerator needs to be multiplied by 3.

$$\begin{array}{l}\frac{1}{2}+\frac{5}{6}=\frac{1\times 3}{2\times 3}+\frac{5}{2\times 3}\\ \phantom{\frac{1}{2}+\frac{5}{6}}=\frac{3}{6}+\frac{5}{6}\end{array}$$

You can use prime factorization to find the lcm. First prime factorize each given integer and write products of equal factors as powers, then the lcm will be the product of the highest power of each prime number in the total group of prime factor powers:

$$\begin{array}{rl}\mathrm{factor}\left(540\right)\text{:}& {2}^{2}\times {3}^{3}\times {5}^{\phantom{1}}\\ \mathrm{factor}\left(1134\right)\text{:}& {2}^{\phantom{1}}\times {3}^{4}\times {7}^{\phantom{1}}\\ \mathrm{lcm}\left(1134,540\right)\text{:}& {2}^{2}\times {3}^{4}\times {5}^{\phantom{1}}\times {7}^{\phantom{1}}\\ \text{common multiple:}& {2}^{2}\times {3}^{4}\times {5}^{\phantom{1}}\times {7}^{2}\\ \text{common multiple:}& {2}^{3}\times {3}^{6}\times {5}^{2}\times {7}^{3}\times 13\\ \text{common multiple:}& \text{etc.}\end{array}$$

In the example above the "prime factor powers" 2 and 3^{3} are left over,
that is, they are not part of the lcm,
since they are not the highest power of a prime number in the total group of prime factor powers.
However, 2 × 3^{3} is the greatest common divisor of 1134 and 540!

$$\frac{540}{1134}=\frac{2\times {3}^{3}}{2\times {3}^{3}}\times \frac{2\times 5}{3\times 7}$$

$$\mathrm{gcd}\left(1134,540\right)=2\times {3}^{3}$$

This means that multiplying the lcm and gcd yields a product of all prime factor powers, which is the same as multiplying the non-factorized initial integers. This gives us an efficient method to find the lcm through the gcd:

$$\mathrm{lcm}\left(a,b\right)=\frac{a\times b}{\mathrm{gcd}\left(a,b\right)}$$

### Examples

$$\frac{2}{15}-\frac{1}{18}=\mathrm{\dots}$$

gcd(15,18) = 3 because:

1 | ||||||

15 | ) | 1 | 8 | |||

1 | 5 | 5 | ||||

3 | ) | 1 | 5 | |||

1 | 5 | |||||

0 |

$$\begin{array}{l}\mathrm{lcm}\left(15,18\right)=\frac{15\times 18}{3}\\ \phantom{\mathrm{lcm}\left(15,18\right)}=5\times 18=15\times 6=90\end{array}$$

$$\frac{2}{15}-\frac{1}{18}=\frac{2\times 6}{15\times 6}-\frac{5}{5\times 18}=\frac{7}{90}$$

$$\frac{2}{102}+\frac{2}{78}=\mathrm{\dots}$$

gcd(102,78) = 6 because:

1 | ||||||||||

78 | ) | 1 | 0 | 2 | ||||||

7 | 8 | 3 | ||||||||

2 | 4 | ) | 7 | 8 | ||||||

7 | 2 | 4 | ||||||||

6 | ) | 2 | 4 | |||||||

2 | 4 | |||||||||

0 |

$$\begin{array}{l}\mathrm{lcm}\left(102,78\right)=\frac{102\times 78}{6}\\ \phantom{\mathrm{lcm}\left(102,78\right)}=17\times 78=102\times 13\end{array}$$

$$\begin{array}{l}\frac{2}{102}+\frac{2}{78}=\frac{2\times 13}{102\times 13}+\frac{2\times 17}{78\times 17}\\ \phantom{\frac{2}{102}+\frac{2}{78}}=\frac{60}{78\times 17}\\ \phantom{\frac{2}{102}+\frac{2}{78}}=\frac{2\times 2\times 3\times 5}{2\times 3\times 13\times 17}\\ \phantom{\frac{2}{102}+\frac{2}{78}}=\frac{2\times 5}{13\times 17}=\frac{10}{221}\end{array}$$

The answer 60/1326 is reduced to 10/221 by using factorization of the numerator and denominator. Now try to reduce 60/1326 by using the gcd.

$$\frac{1}{6}+\frac{1}{10}+\frac{1}{12}=\mathrm{\dots}$$

$$\begin{array}{l}\mathrm{lcm}\left(6,10\right)=\frac{6\times 10}{2}\\ \phantom{\mathrm{lcm}\left(6,10\right)}=3\times 10=6\times 5=30\end{array}$$

$$\frac{1}{6}+\frac{1}{10}=\frac{5}{5\times 6}+\frac{3}{3\times 10}=\frac{8}{30}$$

$$\frac{1}{6}+\frac{1}{10}+\frac{1}{12}=\frac{8}{30}+\frac{1}{12}$$

gcd(30,12) = 6 because:

2 | ||||||

12 | ) | 3 | 0 | |||

2 | 4 | 2 | ||||

6 | ) | 1 | 2 | |||

1 | 2 | |||||

0 |

$$\begin{array}{l}\mathrm{lcm}\left(30,12\right)=\frac{30\times 12}{6}\\ \phantom{\mathrm{lcm}\left(30,12\right)}=30\times 2=5\times 12=60\end{array}$$

$$\begin{array}{l}\frac{8}{30}+\frac{1}{12}=\frac{8\times 2}{30\times 2}+\frac{5}{5\times 12}\\ \phantom{\frac{8}{30}+\frac{1}{12}}=\frac{21}{30\times 2}=\frac{7}{10\times 2}\end{array}$$

$$\frac{1}{6}+\frac{1}{10}+\frac{1}{12}=\frac{7}{20}$$