Largest Prime Factor
What is the largest prime factor of the number 600,851,475,143?
Problem Statement
This archive keeps the full statement, math, and original media on the page.
The prime factors of \(13195\) are \(5\), \(7\), \(13\) and \(29\).
What is the largest prime factor of the number \(600851475143\) ?
Problem 3: Largest Prime Factor
Mathematical Development
Fundamental Theorems
Definition 1. An integer is prime if its only positive divisors are 1 and . An integer that is not prime is composite.
Theorem 1 (Fundamental Theorem of Arithmetic). Every integer has a unique factorization (up to order) as a product of primes:
where are primes and .
We take this as an axiom of (provable from the well-ordering principle via Bezout’s identity). The results below are self-contained.
Theorem 2 (Smallest Divisor is Prime). If is an integer and is the smallest integer that divides , then is prime.
Proof. Suppose for contradiction that is composite. Then there exist integers with and such that . Since and , transitivity of divisibility gives . But , contradicting the minimality of .
Theorem 3 (Square Root Bound for Composite Numbers). If is composite, then has a prime factor satisfying .
Proof. Since is composite, write with integers (choosing without loss of generality). Then , so . Let be the smallest divisor of with . By Theorem 2, is prime. Since and , we have . Moreover, , so .
Corollary 1 (Primality Test). An integer is prime if and only if no prime divides .
Proof. The forward direction is trivial (a prime has no divisors other than 1 and itself). The contrapositive of the reverse direction is Theorem 3.
Trial Division
Definition 2 (Trial Division Procedure). Given input , the trial division algorithm operates as follows:
- Initialize .
- While :
- (a) While : replace and record as a prime factor.
- (b) Set .
- If , record as a prime factor.
- Return the largest recorded prime factor.
Theorem 4 (Correctness of Trial Division). The trial division procedure produces the complete prime factorization of the input , and in particular correctly identifies its largest prime factor.
Proof. Let denote the original input. We maintain the following loop invariant:
Invariant : The current value of satisfies , where is the product of all factors extracted so far, and has no prime factor strictly less than .
Initialization. At , no factors have been extracted (, ), and the condition “no prime factor ” is vacuous. So holds.
Maintenance. Suppose holds at the start of an iteration.
-
Case : By , has no prime factor less than , so the smallest divisor of that is is at least . Since and , the smallest divisor of is at most , hence it equals . By Theorem 2, is prime. We divide by (possibly multiple times), extracting all powers of . After this, , and no prime factor of is .
-
Case : The invariant is unaffected. We increment .
In either case, after incrementing to , invariant holds.
Termination. The loop exits when . At this point, holds: has no prime factor less than . If , then is prime, because if were composite, Theorem 3 would guarantee a prime factor , contradicting . So is the final (and largest) prime factor.
Completeness. Every prime factor of is either extracted in step 2(a) or is the remaining in step 3. The largest among these is the answer.
Theorem 5 (Termination). The trial division procedure terminates for all inputs .
Proof. Define the measure function . In step 2(a), each division reduces by a factor of at least 2, so strictly decreases. In step 2(b), increases by 1. In both cases, strictly decreases. Since is a non-negative integer (the loop condition ensures ), the algorithm terminates.
Factorization of 600,851,475,143
Lemma 1. .
Proof. We verify by direct computation:
Lemma 2. Each of is prime.
Proof. By Corollary 1, it suffices to check that no prime divides for each factor.
-
71: . Primes to check: . We have , , , . None divide 71. Hence 71 is prime.
-
839: . Primes to check: . Since 839 is odd, not divisible by 3 (, ), does not end in 0 or 5, and direct verification confirms (), (), (), (), (), (). Hence 839 is prime.
-
1471: . Primes to check: . 1471 is odd; digit sum , ; does not end in 0 or 5. Checking: (), (), (), (), (), (), (), (), (). Hence 1471 is prime.
-
6857: . Primes to check: . 6857 is odd; digit sum , ; does not end in 0 or 5. Checking representative cases: (), (), (), (), (), (). No prime up to 82 divides 6857. Hence 6857 is prime.
Theorem 6. The largest prime factor of is .
Proof. By Lemma 1, . By Lemma 2, all four factors are prime. By the Fundamental Theorem of Arithmetic, this is the unique prime factorization. The largest factor is .
Editorial
We remove prime factors by trial division. Starting with , we divide the current value of by as long as is a factor, then increment and continue while . When the loop ends, any remaining value greater than 1 is the largest prime factor; otherwise the last divisor used is the answer. This works because every composite factor has a prime divisor at most the square root of the current remainder.
Execution Trace for :
| Step | Action | Remaining | |
|---|---|---|---|
| 1 | 71 | ; divide | |
| 2 | 839 | ; divide | |
| 3 | 1471 | ; divide | |
| 4 | 1472 | ; exit loop |
Return .
Pseudocode
Algorithm: Largest Prime Factor
Require: An integer n > 1.
Ensure: The largest prime divisor of n.
1: Initialize m ← n, d ← 2, and p_max ← 1.
2: While d^2 ≤ m do:
3: If d does not divide m, advance to the next divisor; otherwise set p_max ← d and remove the full power of d from m.
4: If m > 1, set p_max ← m.
5: Return p_max.
Complexity Analysis
Theorem 7. Trial division runs in time and space in the worst case.
Proof.
Time bound. The outer loop increments from 2 up to at most (where is the current value). We claim the total number of iterations of both the inner and outer loops combined is where is the original input.
The outer variable increases by 1 each outer iteration and runs while , so there are at most outer iterations. For the inner loop: each inner iteration divides by , so halves (at least) each time. Since starts at and each division produces a strict decrease, the total number of inner iterations across all values of is at most . The total iteration count is thus .
Space bound. The algorithm uses only the variables and , which is additional space.
Tightness. When is prime, the inner loop never executes, and the outer loop runs for , giving iterations. Hence the worst-case time is .
Application. For , the worst-case bound is iterations. In practice, the first factor is found early, reducing and the effective search space.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n = 600851475143LL;
for (long long d = 2; d * d <= n; d++)
while (n % d == 0)
n /= d;
cout << n << endl;
return 0;
}
"""Problem 3: Largest Prime Factor"""
def largest_prime_factor(n: int) -> int:
d = 2
while d * d <= n:
while n % d == 0:
n //= d
d += 1
return n if n > 1 else d - 1
answer = largest_prime_factor(600851475143)
assert answer == 6857
print(answer)