Sub topic 10
Euclid's Algorithm for Fast HCF
This chapter assumes you can do division with remainder, know what the HCF (highest common factor) is, and can read basic exponent notation. You have already seen how to find an HCF by prime factorization. Here you learn a faster method that works even when the numbers are too large or awkward to factorize by hand.
Why a second HCF method?
Prime factorization is reliable, but it needs you to spot the primes. Factorizing 1517 or 4181 by hand costs time you do not have in the exam. Euclid's algorithm finds the HCF using only division with remainder. It never needs a factor.
The method rests on one fact:
- If a = b × q + r, then HCF(a, b) = HCF(b, r).
Why the rule is true
Let d be any common divisor of a and b. Since r = a − b × q, d also divides r. So d is a common divisor of b and r.
Now go the other way. Let d be any common divisor of b and r. Since a = b × q + r, d also divides a. So d is a common divisor of a and b.
The two pairs (a, b) and (b, r) therefore have exactly the same common divisors. They have the same greatest one too.
The procedure
- Divide the larger number by the smaller. Note the remainder.
- Replace the pair (larger, smaller) with (smaller, remainder).
- Repeat until the remainder is 0.
- The last non-zero remainder, which is the last divisor used, is the HCF.
The process always ends. Each remainder is smaller than the divisor, so the remainders strictly decrease and must reach 0.
Worked example 1: HCF of 252 and 105
| Step | Division | Remainder |
|---|---|---|
| 1 | 252 = 105 × 2 + 42 | 42 |
| 2 | 105 = 42 × 2 + 21 | 21 |
| 3 | 42 = 21 × 2 + 0 | 0 |
The last non-zero remainder is 21, so HCF(252, 105) = 21.
Check by factorization: 252 = 2² × 3² × 7 and 105 = 3 × 5 × 7. The common part is 3 × 7 = 21. The two methods agree.
Worked example 2: numbers that resist factorizing
Find HCF(1517, 4181).
| Step | Division | Remainder |
|---|---|---|
| 1 | 4181 = 1517 × 2 + 1147 | 1147 |
| 2 | 1517 = 1147 × 1 + 370 | 370 |
| 3 | 1147 = 370 × 3 + 37 | 37 |
| 4 | 370 = 37 × 10 + 0 | 0 |
The HCF is 37. Spotting that both numbers share the factor 37 (1517 = 37 × 41 and 4181 = 37 × 113) by inspection would be slow. Four divisions did it.
The algorithm in code
The loop below mirrors the table. It also derives the LCM from the relation HCF × LCM = a × b.
def gcd(a, b):
while b:
a, b = b, a % b
return a
def lcm(a, b):
return a // gcd(a, b) * b
print(gcd(252, 105), lcm(252, 105)) # 21 1260Dividing before multiplying (a // gcd(a, b) * b) keeps intermediate numbers small. In exam work, do the same by hand: divide one number by the HCF first, then multiply.
Extending to three or more numbers
The HCF is associative. Find the HCF of the first two numbers, then find the HCF of that result with the third number, and so on.
Example: HCF(84, 126, 210).
- HCF(84, 126): 126 = 84 × 1 + 42, then 84 = 42 × 2 + 0. The result is 42.
- HCF(42, 210): 210 = 42 × 5 + 0. The result is 42.
So HCF(84, 126, 210) = 42.
A useful shortcut: start with the two smallest or most similar numbers, since the running HCF can only shrink.
Applying it to remainder problems
Euclid's algorithm is the engine behind the standard "same remainder" question. If a divisor leaves remainder r in both numbers, it divides each number minus r. The largest such divisor is the HCF of the reduced numbers.
Find the largest number that divides 245 and 1029 leaving remainder 5 each time.
- Subtract the remainder: 245 − 5 = 240 and 1029 − 5 = 1024.
- Run Euclid: 1024 = 240 × 4 + 64, then 240 = 64 × 3 + 48, then 64 = 48 × 1 + 16, then 48 = 16 × 3 + 0.
- The HCF is 16.
from math import gcd
# Largest number that divides 245 and 1029 leaving remainder 5 in each case
print(gcd(245 - 5, 1029 - 5)) # 16The divisor must also be larger than the remainder 5. Since 16 > 5, the answer is valid. Always run this check.
The difference shortcut
Because HCF(a, b) = HCF(b, a − b), you can subtract instead of dividing. This helps when the numbers are close together.
Find HCF(1001, 1043). The difference is 42, so the HCF divides 42. Test the divisors of 42 against 1001: 1001 = 7 × 11 × 13, so the candidates that divide it are 1, 7 and 14 is not (14 does not divide 1001). Checking 7: 1043 = 7 × 149, so 7 divides both. Checking 21 and 42: 1001 is not divisible by 3, so neither works. The HCF is 7.
This also shows a general principle. The HCF of two numbers always divides their difference. That narrows the search to the factors of a small number.
Edge cases and common errors
- Reporting the last remainder as 0. The answer is the last non-zero remainder, which is the divisor in the final step.
- Starting with the smaller number first. The algorithm still works. If a < b, the first step gives a mod b = a, so the pair simply swaps. You lose one step but not correctness.
- Forgetting the remainder condition. In remainder problems, the divisor must exceed the remainder. If the HCF is not larger than r, no valid divisor exists.
- Dividing wrongly under pressure. One arithmetic slip in an early step corrupts everything after it. Verify the final HCF by dividing both original numbers by it. Both divisions must be exact.
- Using HCF × LCM = a × b for three numbers. That relation holds only for two numbers. Do not extend it.
- Coprime results. If the last non-zero remainder is 1, the numbers are coprime (they share no factor except 1). This is a valid answer, not a mistake.
How fast is it?
Euclid's algorithm is fast because remainders shrink quickly. Within every two steps, the remainder falls to less than half of the earlier value. So the number of steps grows with the number of digits, not with the size of the numbers. For CAT, this means that even 5-digit pairs usually finish in under eight divisions.
Choosing between methods
| Situation | Better method |
|---|---|
| Small numbers with obvious factors | Prime factorization |
| Large numbers or hidden factors | Euclid's algorithm |
| You need the LCM too | Euclid for the HCF, then LCM = a × b ÷ HCF |
| Need the HCF to be a factor of a known small number | Difference shortcut |
| Many numbers with powers of primes | Prime factorization, taking minimum exponents |
Exercise
Use Euclid's algorithm to find HCF(493, 799), and then find LCM(493, 799).
Show answer
799 = 493 × 1 + 306. 493 = 306 × 1 + 187. 306 = 187 × 1 + 119. 187 = 119 × 1 + 68. 119 = 68 × 1 + 51. 68 = 51 × 1 + 17. 51 = 17 × 3 + 0. The HCF is 17. The LCM is 493 × 799 ÷ 17 = 29 × 799 = 23171.
Exercise
Find the largest number that divides 398, 436 and 542 leaving remainders 7, 11 and 15 respectively.
Show answer
Subtract each remainder: 398 − 7 = 391, 436 − 11 = 425, 542 − 15 = 527. HCF(391, 425): 425 = 391 × 1 + 34, 391 = 34 × 11 + 17, 34 = 17 × 2 + 0, so it is 17. HCF(17, 527): 527 = 17 × 31, so it is 17. The answer is 17, which exceeds the largest remainder 15, so it is valid.
Exercise
Two numbers have HCF 12 and their sum is 96. They are not equal. How many such pairs of numbers exist, ignoring order?
Show answer
Write the numbers as 12x and 12y with x and y coprime and x + y = 8. The coprime pairs with x < y are (1, 7) and (3, 5). The pairs (2, 6) and (4, 4) are not coprime. So there are 2 pairs: (12, 84) and (36, 60).