Here is the full solution:
function largestPrimeFactor(n)
largestFactor := 1
i := 2
while i * i <= n
while n mod i = 0
largestFactor := i
n := n / i
i := i + 1
if n > 1 then
largestFactor := n
return largestFactor
Time: . Space: . The inner while loop divides out all occurrences of each prime factor before moving to the next.