Data Structures & AlgorithmsHardCodeforces Div2 B · factorisation

Problem: What is the largest prime factor of 600851475143? Run the trial-division code.

Problem: What is the largest prime factor of 600851475143? Run the trial-division code.
n = 600851475143
f = 2
last = 1
while f * f <= n:
    while n % f == 0:
        last = f
        n //= f
    f += 1
if n > 1:
    last = n
print(last)
What is printed?
  1. A. 1471
  2. B. 839
  3. C. 71
  4. D. 6857

Correct answer: D. 6857

600851475143 = 71 * 839 * 1471 * 6857, and the largest prime factor is 6857. The trial-division loop removes each factor and keeps the last one found.

More Coding Interview Problems questions

Exams that ask this topic