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.
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, is in lowest terms:
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.
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:
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.
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.
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.
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:
In the example above the "prime factor powers" 2 and 33 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 × 33 is the greatest common divisor of 1134 and 540!
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:
Examples
gcd(15,18) = 3 because:
1 | ||||||
15 | ) | 1 | 8 | |||
1 | 5 | 5 | ||||
3 | ) | 1 | 5 | |||
1 | 5 | |||||
0 |
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 |
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.
gcd(30,12) = 6 because:
2 | ||||||
12 | ) | 3 | 0 | |||
2 | 4 | 2 | ||||
6 | ) | 1 | 2 | |||
1 | 2 | |||||
0 |
Order the next fractions from least to greatest:
Answer:
The given sequence of fractions now written with a common denominator: Thus: