Largest prime factor

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

What is the largest prime factor of the number 600851475143 ?


Idea

A problem of 'prime factor decomposition'.
Iterate from smallest prime number 2, repeatedly devided by current largest prime factor.


In [1]:
def solve(number):
    largest_prime_factor = 1
    d = 2
    while d ** 2 <= number:
        while number % d == 0:
            number //= d
        largest_prime_factor = d
        d += 1
    return max(largest_prime_factor, number)
In [2]:
solve(13195)
Out[2]:
29
In [3]:
solve(600851475143)
Out[3]:
6857