Sub topic 6
Prime factorization and the factor tree
This chapter assumes you can test divisibility by 2, 3, 5, and 9, that you know what a prime number is, and that you are comfortable with exponent notation such as 2^3. It builds the tool that later chapters use to count factors and to find the GCD and LCM.
The core idea
Every integer greater than 1 can be written as a product of primes, and the only freedom is the order of the factors. This is the Fundamental Theorem of Arithmetic. The result is called the prime factorization of the number.
For example, 360 = 2 × 2 × 2 × 3 × 3 × 5 = 2^3 × 3^2 × 5. No other collection of primes multiplies to 360.
Uniqueness is why the factorization is useful on the GRE. Two different-looking expressions for the same integer must contain the same primes with the same exponents. So you can compare, count, and combine integers by comparing their exponents.
Building a factor tree
A factor tree splits a number into two factors, then splits each composite factor again. You stop when every branch ends in a prime.
The diagram below shows one tree for 360.
Reading the leaves gives 2, 2, 3, 3, 2, 5. Collect equal primes into powers: 2^3 × 3^2 × 5.
You could start with 360 = 18 × 20 or 360 = 8 × 45 instead. The branches differ, but the leaves always collect to the same result.
A procedure that avoids mistakes
- Divide out the smallest prime that works (2, then 3, then 5, and so on) until it no longer divides evenly.
- Move to the next prime and repeat.
- Stop when the remaining number is 1, or when it is itself a prime.
- Check by multiplying the powers back together.
Take 1,764. It is even, so 1,764 = 2 × 882 = 2 × 2 × 441. Now 441 is odd. Its digits sum to 9, so it is divisible by 9: 441 = 9 × 49 = 3^2 × 7^2. Therefore 1,764 = 2^2 × 3^2 × 7^2.
Doing it in code
Trial division mirrors the procedure above. It tries each candidate divisor p in turn and records how many times it divides n.
def prime_factors(n):
factors = {}
p = 2
while p * p <= n:
while n % p == 0:
factors[p] = factors.get(p, 0) + 1
n //= p
p += 1
if n > 1:
factors[n] = factors.get(n, 0) + 1
return factors
print(prime_factors(360)) # {2: 3, 3: 2, 5: 1}The loop stops at p × p > n because a composite number always has a prime factor no larger than its square root. If anything greater than 1 is left after the loop, that leftover is a prime and gets recorded as a final factor.
Why you only test primes up to the square root
Suppose n = a × b with a ≤ b. Then a × a ≤ a × b = n, so a ≤ √n. This means that if n has any factor other than 1 and itself, it has one at most √n. For 97, you need only test 2, 3, 5, and 7, because 11^2 = 121 is already larger than 97. None of them divides 97, so 97 is prime.
This is the same shortcut you use by hand. To factor a number below 200, you never need a prime larger than 13, since 17^2 = 289.
What the factorization tells you
Once n is written as a product of prime powers, several questions become reading exercises.
| Question | How to answer it from the factorization |
|---|---|
| Is n divisible by d? | Every prime of d appears in n with at least the same exponent. |
| Is n a perfect square? | Every exponent is even. |
| What is the square root of a perfect square? | Halve every exponent. |
| What is n times m? | Add the exponents of matching primes. |
| What is n divided by m (when m divides n)? | Subtract the exponents of matching primes. |
Here is a divisibility check. Is 360 divisible by 24? Since 24 = 2^3 × 3, and 360 = 2^3 × 3^2 × 5 has at least 2^3 and at least 3^1, the answer is yes. Is 360 divisible by 16? Since 16 = 2^4 and 360 has only 2^3, the answer is no.
Here is a square check. 1,764 = 2^2 × 3^2 × 7^2 has all even exponents, so it is a perfect square, and its square root is 2 × 3 × 7 = 42.
print(42 ** 2) # 1764Worked GRE-style example
Question. If n is a positive integer and 2,520 × n is a perfect square, what is the smallest possible value of n?
Factor 2,520. It is divisible by 10 with 252 left over. Then 252 = 4 × 63 = 2^2 × 3^2 × 7. So 2,520 = 2 × 5 × 2^2 × 3^2 × 7 = 2^3 × 3^2 × 5 × 7.
The exponents are 3, 2, 1, and 1. A perfect square needs every exponent even. The primes 2, 5, and 7 have odd exponents, so n must supply one more of each. The smallest n is 2 × 5 × 7 = 70.
Check: 2,520 × 70 = 176,400 = 2^4 × 3^2 × 5^2 × 7^2, and every exponent is even. Its square root is 2^2 × 3 × 5 × 7 = 420.
The general method is to multiply by each prime that has an odd exponent, once. It never requires a larger multiplier.
Edge cases and common errors
- 1 has no prime factorization worth writing. The number 1 is not prime, and it is the empty product. Never list 1 as a prime factor.
- Counting a prime once. Writing 72 = 2 × 3 × 12 stops early. The 12 is still composite. Keep splitting until every leaf is prime: 72 = 2^3 × 3^2.
- Forgetting the leftover prime. After dividing out small primes, a large prime can remain, such as 2 × 3 × 59. If the remaining number has no prime factor up to its square root, it is prime.
- Treating a number as prime because it looks odd. Numbers like 51 (3 × 17), 91 (7 × 13), and 87 (3 × 29) are composite but are commonly mistaken for primes. Test 3, 7, and 11 before you decide.
- Negative integers. The factorization of a negative integer is usually written as −1 times the factorization of its absolute value. The primes themselves are positive.
- Adding instead of multiplying. The prime factors multiply to n. Their sum is not meaningful here.
Factorization of products and powers
Because exponents add under multiplication, you can factor pieces separately and combine the results.
To factor 12^2 × 15, write 12 = 2^2 × 3 and 15 = 3 × 5. Then 12^2 = 2^4 × 3^2, so 12^2 × 15 = 2^4 × 3^3 × 5. You never need to compute 2,160 first.
The same idea shows how powers behave. If n = 2^a × 3^b, then n^k = 2^(ak) × 3^(bk). Every exponent is multiplied by k. This is why cubing an integer produces exponents that are all multiples of 3, which is the test for a perfect cube.
from math import prod
def from_factors(factors):
return prod(p ** e for p, e in factors.items())
print(from_factors({2: 4, 3: 3, 5: 1})) # 2160
print(12 ** 2 * 15) # 2160Using it on quantitative comparison questions
Quantitative comparison questions often hide a factorization inside an expression. Suppose Quantity A is the number of distinct prime factors of 210 and Quantity B is the number of distinct prime factors of 360.
- 210 = 2 × 3 × 5 × 7, so it has 4 distinct primes.
- 360 = 2^3 × 3^2 × 5, so it has 3 distinct primes.
Quantity A is greater. Note that "distinct prime factors" counts each prime once, regardless of its exponent. A question that asks for "prime factors counted with multiplicity" would give 6 for 360.
Summary
- Every integer greater than 1 has exactly one prime factorization, apart from the order of factors.
- A factor tree and trial division both produce it. Test primes only up to the square root of the number.
- Multiplying integers adds exponents. Dividing subtracts them. Raising to a power multiplies them.
- A perfect square has all even exponents. A perfect cube has all exponents divisible by 3.
- The factorization is the starting point for counting divisors and for finding the GCD and LCM in the next chapters.
Exercise
Write the prime factorization of 1,260. Then find the smallest positive integer k such that 1,260 × k is a perfect square.
Show answer
Divide out primes: 1,260 = 2 × 630 = 2 × 2 × 315 = 2^2 × 315. Then 315 = 3 × 105 = 3 × 3 × 35 = 3^2 × 5 × 7. So 1,260 = 2^2 × 3^2 × 5 × 7. The primes 5 and 7 have odd exponents, so k = 5 × 7 = 35. Check: 1,260 × 35 = 44,100 = 2^2 × 3^2 × 5^2 × 7^2 = 210^2.
Exercise
Is 391 prime? Show the test you used.
Show answer
The square root of 391 is a little under 20 (since 20^2 = 400), so test the primes 2, 3, 5, 7, 11, 13, 17, and 19. It is odd, its digit sum 13 is not a multiple of 3, and it does not end in 0 or 5. Then 7 × 55 = 385 leaves remainder 6, 11 × 35 = 385 leaves remainder 6, and 13 × 30 = 390 leaves remainder 1. But 17 × 23 = 391. So 391 = 17 × 23 and it is composite.