Simplifying Radicals
A radical is in simplest form when no perfect square hides inside it. and are the same number, but is easier to compare, combine and reason about. This lesson shows how to pull square factors out, how to add and multiply radicals, and how to clear a root from a denominator.
Pull out perfect squares
The product rule lets you split off the largest perfect-square factor:
If you don't spot the largest square at once, use the prime factorization and pair up equal primes. Each pair leaves the root as a single factor:
The pairing method is easy to turn into code:
def simplify_sqrt(n):
outside, inside = 1, 1
p = 2
while n > 1:
count = 0
while n % p == 0:
n //= p
count += 1
outside *= p ** (count // 2) # each pair comes out
inside *= p ** (count % 2) # a leftover stays in
p += 1
return outside, inside
for n in [72, 50, 48, 360, 98, 17, 400]:
a, b = simplify_sqrt(n)
print(f"√{n} = {a}√{b}" if b > 1 else f"√{n} = {a}")√72 = 6√2
√50 = 5√2
√48 = 4√3
√360 = 6√10
√98 = 7√2
√17 = 1√17
√400 = 20
(A careful version would print instead of , an easy improvement to try.)
SymPy simplifies radicals automatically and keeps them exact:
from sympy import sqrt, Rational
print(sqrt(72))
print(sqrt(360))
print(sqrt(8) + sqrt(18))
print(sqrt(Rational(5, 12)))
print(float(sqrt(72)))6*sqrt(2)
6*sqrt(10)
5*sqrt(2)
sqrt(15)/6
8.48528137423857
Adding and subtracting
Radicals combine only when the part under the root is the same, like collecting like terms ():
Sometimes simplifying first reveals like terms. That is what SymPy did above:
Multiplying and dividing
Multiply the outside numbers together and the inside numbers together, then simplify:
Rationalising the denominator
By tradition, a simplified answer has no root in the denominator. Multiply top and bottom by the root, which changes nothing because you multiply by 1:
The last one matches SymPy's sqrt(15)/6. is worth recognising: it is and .
Checking with decimals
Two forms of the same radical have the same decimal value, which is a quick check for your algebra:
import math
pairs = [("√72", math.sqrt(72), "6√2", 6 * math.sqrt(2)),
("1/√2", 1 / math.sqrt(2), "√2/2", math.sqrt(2) / 2),
("√8+√18", math.sqrt(8) + math.sqrt(18), "5√2", 5 * math.sqrt(2))]
for a, x, b, y in pairs:
print(f"{a:7} = {x:.6f} {b:5} = {y:.6f} close: {math.isclose(x, y)}")√72 = 8.485281 6√2 = 8.485281 close: True
1/√2 = 0.707107 √2/2 = 0.707107 close: True
√8+√18 = 7.071068 5√2 = 7.071068 close: True
Tip: Use
math.iscloserather than==to compare floats: two correct computations can differ in the last binary digit.
In software
- Exact geometry: a diagonal of a unit square is , a normalised direction is . You'll see 0.7071 constants in game code moving a character diagonally at the same speed as straight (see length and normalize).
- Computer algebra: SymPy, Mathematica and friends keep symbolic, so is exactly 2, while floats give 2.0000000000000004.
- Grid sizes: a hexagon grid uses for row spacing; keeping it symbolic until the end avoids rounding drift.
Exercises
Exercise 1: Simplify
Simplify , , and .
Answer
, , , .
Solution
Exercise 2: Combine
Simplify .
Answer
.
Solution
Exercise 3: Rationalise
Write and without a root in the denominator.
Answer
and .
Solution
Check: and .
Exercise 4: Fix the code
Change simplify_sqrt from this lesson so that it prints √17 instead of 1√17, and check it on 17, 12 and 64.
Answer
, , 8.
Solution
Only the printing changes: leave out the outside factor when it is 1.
def simplify_sqrt(n):
outside, inside, p = 1, 1, 2
while n > 1:
count = 0
while n % p == 0:
n //= p
count += 1
outside *= p ** (count // 2)
inside *= p ** (count % 2)
p += 1
return outside, inside
def show(n):
a, b = simplify_sqrt(n)
if b == 1:
return f"√{n} = {a}"
return f"√{n} = {'' if a == 1 else a}√{b}"
for n in [17, 12, 64]:
print(show(n))√17 = √17
√12 = 2√3
√64 = 8
Test yourself
Simplify .
What is ?
Which equals with a rational denominator?