Sub topic 8
Highest Common Factor
This chapter assumes you can factorize a number into primes and use exponent notation. It also assumes you know how to count factors from the exponent form, which earlier chapters covered. Here we focus on the Highest Common Factor (HCF), also called the Greatest Common Divisor (GCD). We cover how to compute it, how to recognize it in word problems, and where it goes wrong.
What the HCF is
The HCF of two or more positive integers is the largest integer that divides every one of them with no remainder. A common factor is any number that divides all of them. The HCF is the biggest of those.
For 36 and 60:
- Factors of 36: 1, 2, 3, 4, 6, 9, 12, 18, 36
- Factors of 60: 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60
- Common factors: 1, 2, 3, 4, 6, 12
- HCF: 12
Every common factor of the numbers is also a factor of the HCF. Here 1, 2, 3, 4 and 6 all divide 12. This property is why the HCF is the natural answer to "what is the largest size that fits every quantity exactly?"
Two numbers are coprime when their HCF is 1. They need not be prime themselves. For example, 8 and 15 are coprime.
Method 1: Listing factors
Listing works for small numbers, as the example above shows. It becomes slow and error-prone once the numbers pass about 100, and it is rarely the right choice in an exam.
Method 2: Prime factorization
Write each number as a product of prime powers. The HCF takes each prime that appears in all the numbers, raised to the smallest exponent among them.
Take 360, 756 and 1260.
- 360 = 2^3 x 3^2 x 5
- 756 = 2^2 x 3^3 x 7
- 1260 = 2^2 x 3^2 x 5 x 7
Check each prime:
| Prime | Exponents in 360, 756, 1260 | Smallest | Included? |
|---|---|---|---|
| 2 | 3, 2, 2 | 2 | Yes |
| 3 | 2, 3, 2 | 2 | Yes |
| 5 | 1, 0, 1 | 0 | No |
| 7 | 0, 1, 1 | 0 | No |
A prime missing from any one number has exponent 0 there, so the minimum is 0 and the prime drops out. So HCF = 2^2 x 3^2 = 36.
The rule to remember is "common primes, lowest powers". The companion rule for the LCM is "all primes, highest powers", and mixing the two up is a frequent slip.
Method 3: Euclid's algorithm
Prime factorization is slow when the numbers are large or have big prime factors. Euclid's algorithm avoids factorizing. It rests on this fact: the HCF of a and b equals the HCF of b and the remainder of a divided by b.
To find HCF(252, 105):
- 252 = 2 x 105 + 42, so HCF(252, 105) = HCF(105, 42)
- 105 = 2 x 42 + 21, so HCF(105, 42) = HCF(42, 21)
- 42 = 2 x 21 + 0, so the remainder is zero
The last nonzero remainder, 21, is the HCF.
def gcd(a, b):
while b:
a, b = b, a % b
return a
print(gcd(252, 105)) # 21For three numbers, apply the HCF in steps: HCF(a, b, c) = HCF(HCF(a, b), c). The next chapters treat Euclid's algorithm in more depth. For now, use it whenever the numbers are large and the factorization is not obvious.
Useful shortcuts
- If one number divides all the others, it is the HCF. For 6, 18 and 42, the HCF is 6.
- If a and b are consecutive integers, their HCF is 1.
- HCF(a, b) also divides a + b and a - b. So HCF(a, b) = HCF(a, a - b) for a > b. This is what makes Euclid's algorithm work.
- To find the HCF of numbers with a common factor, pull the factor out. HCF(120, 180, 300) = 60 x HCF(2, 3, 5) = 60.
- HCF(ka, kb) = k x HCF(a, b).
Recognizing HCF in word problems
Look for these signals in a question:
- "Largest" size, length, or number of items that must fit exactly.
- Splitting into the greatest number of equal groups with nothing left over.
- "Greatest number that divides ... exactly."
Typical cases:
- Cutting ropes of 84 m, 126 m and 210 m into pieces of equal length, as long as possible. The answer is HCF(84, 126, 210) = 42 m.
- Tiling a 540 cm by 360 cm floor with the largest square tiles. The side is HCF(540, 360) = 180 cm, so 3 x 2 = 6 tiles.
- Packing 48 pens and 72 pencils into identical kits with nothing left over. The greatest number of kits is HCF(48, 72) = 24.
Compare the last case with a "least" quantity. If a question asks for the smallest length that all of several things can measure exactly, that is an LCM problem, covered in the next chapter. The test is direction. HCF breaks things down into the largest equal piece. LCM builds up to the smallest common total.
Remainder problems with HCF
Suppose a divisor d leaves remainder r when dividing a number N. Then d divides N - r. So when two numbers leave the same remainder r, the largest divisor is the HCF of the numbers each reduced by r.
Find the largest number that divides 245 and 1029, leaving remainder 5 each time. Subtract the remainder: 240 and 1024. HCF(240, 1024) = 16.
from math import gcd
print(gcd(245 - 5, 1029 - 5)) # 16Notice the divisor must exceed the remainder. Here 16 > 5, so the answer is valid. If a computed HCF is not greater than the remainder, that divisor cannot produce that remainder, and the problem has no valid answer.
When the remainders differ, say r1 and r2, subtract each from its own number: HCF(a - r1, b - r2). Each reduced number must be exactly divisible by d.
A related trick applies when the question says "leaves the same remainder" without giving its value. Then d divides the difference of the numbers. For 245 and 1029, d divides 1029 - 245 = 784. The largest such d is the HCF of the pairwise differences. With three numbers a, b, c, take HCF(b - a, c - b).
Common errors
- Using maximum exponents. The HCF uses the minimum exponent of each common prime. The maximum belongs to the LCM.
- Keeping primes that are not shared. A prime absent from even one number is dropped.
- Forgetting to subtract the remainder. Taking HCF(245, 1029) directly gives 7, which is the wrong answer to the remainder question.
- Confusing HCF with LCM in word problems. Ask whether the answer should be a divisor (HCF) or a multiple (LCM) of the given numbers.
- Stopping Euclid's algorithm early. The answer is the last nonzero remainder, not the first remainder you compute.
- Treating a coprime pair as prime numbers. 9 and 16 are coprime, though neither is prime.
HCF and number of common factors
The common factors of a set of numbers are exactly the factors of their HCF. So the count of common factors equals the number of factors of the HCF. For 360 and 756, HCF = 2^2 x 3^2 = 36, and 36 has (2+1)(2+1) = 9 factors. So the two numbers share 9 common factors.
This turns a listing job into a short calculation, and CAT likes to ask it.
from math import gcd
def factorize(n):
f = {}
p = 2
while p * p <= n:
while n % p == 0:
f[p] = f.get(p, 0) + 1
n //= p
p += 1
if n > 1:
f[n] = f.get(n, 0) + 1
return f
h = gcd(360, 756)
count = 1
for e in factorize(h).values():
count *= e + 1
print(h, count) # 36 9Summary of the method
- Small numbers: list factors.
- Visible prime structure: take common primes at the lowest powers.
- Large or awkward numbers: use Euclid's algorithm.
- Remainder questions: subtract the remainder first, or use differences when it is unstated.
- Cutting and packing questions: the HCF is the largest equal piece.
Exercise
Find the greatest number that divides 1251, 9377 and 15628 and leaves remainders 1, 2 and 3 respectively.
Show answer
Subtract each remainder: 1250, 9375 and 15625. Factorize: 1250 = 2 x 5^4, 9375 = 3 x 5^5, 15625 = 5^6. The only common prime is 5, with minimum exponent 4. So the HCF is 5^4 = 625. Since 625 is greater than every remainder, the answer is 625.
Exercise
A hall is 825 cm long and 675 cm wide. It is to be paved with identical square tiles, as large as possible, with no cutting. What is the side of each tile, and how many tiles are needed?
Show answer
The side is HCF(825, 675). Using Euclid's algorithm: 825 = 1 x 675 + 150, 675 = 4 x 150 + 75, 150 = 2 x 75 + 0. So the side is 75 cm. The hall holds 825 / 75 = 11 tiles along the length and 675 / 75 = 9 along the width, so 11 x 9 = 99 tiles.
Exercise
How many common factors do 504 and 756 have?
Show answer
504 = 2^3 x 3^2 x 7 and 756 = 2^2 x 3^3 x 7. The HCF takes the lowest powers: 2^2 x 3^2 x 7 = 252. The number of factors of 252 is (2+1)(2+1)(1+1) = 18, so 504 and 756 have 18 common factors.