Back to Problems

Problem 3: Largest Prime Factor

The prime factors of $13195$ are $5, 7, 13$ and $29$.

What is the largest prime factor of the number $600851475143$?

Solution Approach

This problem can be trivially solved by repeatedly dividing the number by its smallest factors until only the largest prime factor remains (i.e. trial division)

n = 600851475143
i = 2
while i * i <= n:
    if n % i:
        i += 1
    else:
        n //= i
print(n)

Explained line by line:

Running the above code leads to an answer of $6857$