Math Fundamentals18 sections · 814 units
Open in Course

Largest Prime Factor - Algorithm

Step by step approach

Here is the algorithm:

1.1. Initialize largestFactor=1largestFactor = 1 and i=2i = 2.

2.2. While i×ini \times i \leq n: if ii divides nn, set largestFactor=ilargestFactor = i and divide nn by ii. Otherwise, increment ii.

3.3. If n>1n > 1 after the loop, then nn itself is the largest prime factor.

4.4. Return max(largestFactor,n)\max(largestFactor, n).