Math Fundamentals18 sections · 814 units
Open in Course

Largest Prime Factor - Insight

The key observation

You do not need to find all prime factors and then pick the largest. Instead, you can divide out small factors as you find them.

Start with i=2i = 2. While ii divides nn, divide nn by ii. Then increment ii. After all small factors are removed, what remains is either 11 or the largest prime factor.

Why does this work? If nn has a composite factor, its prime factors are smaller and will be divided out first. The largest factor left is always prime.