The prime factors of $13195$ are $5, 7, 13$ and $29$.
What is the largest prime factor of the number $600851475143$?
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:
n to the number whose largest prime factor we want to find, which is $600851475143$.i to $2
$, which is the smallest prime number and will be used to test for factors of n.while loop that continues as long as i * i is less than or equal to n. This is because if n has a factor larger than its square root, the corresponding cofactor must be smaller than the square root.n is divisible by i using the modulus operator. If it is not divisible (i.e., the remainder is not zero), we increment i by $1$ to test the next integer.n is divisible by i, we divide n by i using integer division (which discards any remainder). This effectively reduces n by removing the factor i.n, which at this point will be the largest prime factor of the original number.Running the above code leads to an answer of $6857$