Sub topic 10
Euclid's Algorithm for Fast HCF
Prerequisites and purpose
Assume you can divide integers, find remainders, and understand the Highest Common Factor (HCF). The HCF of two numbers is the greatest positive integer that divides both numbers exactly.
Listing factors works for small numbers, but it becomes slow for large numbers. Euclid's algorithm finds the HCF through repeated division.
The key rule
For integers \(a\) and \(b\), where \(a > b > 0\):
\[ \operatorname{HCF}(a,b)=\operatorname{HCF}(b, a \bmod b) \]
Here, \(a \bmod b\) means the remainder when \(a\) is divided by \(b\).
The reason is that every common factor of \(a\) and \(b\) also divides \(a-bq\), where \(q\) is the quotient. The value \(a-bq\) is the remainder. Therefore, replacing \(a\) with the remainder does not change the common factors.
Worked example: HCF of 252 and 105
Apply the rule repeatedly:
| Division | Remainder |
|---|---|
| \(252=105\times2+42\) | 42 |
| \(105=42\times2+21\) | 21 |
| \(42=21\times2+0\) | 0 |
The last non-zero remainder is 21. Therefore:
\[ \operatorname{HCF}(252,105)=21 \]
The process stops when the remainder becomes zero. The divisor in that final division is the HCF.
Algorithm as a flow
Python implementation
The following function uses the same process. The assignment a, b = b, a % b updates both values at once.
def gcd(a, b):
while b:
a, b = b, a % b
return a
print(gcd(252, 105)) # 21Although programming libraries often call this value gcd, it is the same as the HCF.
Common errors and CAT shortcuts
- Do not subtract the smaller number repeatedly when division gives a faster remainder.
- Do not stop at the first remainder. Continue until the remainder is zero.
- Do not choose the final zero as the answer.
- If one number divides the other exactly, the smaller number is immediately the HCF. For example, HCF(84, 12) is 12.
- Reduce large numbers early. The remainder is usually much smaller than the original dividend.
For three numbers, calculate the HCF in stages:
\[ \operatorname{HCF}(a,b,c)=\operatorname{HCF}(\operatorname{HCF}(a,b),c) \]
Exercise
Find the HCF of 867 and 255 using Euclid's algorithm.
Show answer
\(867=255\times3+102\), \(255=102\times2+51\), and \(102=51\times2+0\). The HCF is 51.