All Euler problems
Project Euler

Largest Prime Factor

What is the largest prime factor of the number 600,851,475,143?

Source sync May 21, 2026
Problem #0003
Level Level 00
Solved By 593,079
Languages C++, Python
Answer 6857
Length 1,016 words
number_theorymodular_arithmeticbrute_force

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 p≥2p \ge 2 is prime if its only positive divisors are 1 and pp. An integer n≥2n \ge 2 that is not prime is composite.

Theorem 1 (Fundamental Theorem of Arithmetic). Every integer n≥2n \ge 2 has a unique factorization (up to order) as a product of primes:

n=p1a1p2a2⋯prarn = p_1^{a_1} p_2^{a_2} \cdots p_r^{a_r}

where p1<p2<⋯<prp_1 < p_2 < \cdots < p_r are primes and ai≥1a_i \ge 1.

We take this as an axiom of Z\mathbb{Z} (provable from the well-ordering principle via Bezout’s identity). The results below are self-contained.

Theorem 2 (Smallest Divisor is Prime). If n≥2n \ge 2 is an integer and dd is the smallest integer ≥2\ge 2 that divides nn, then dd is prime.

Proof. Suppose for contradiction that dd is composite. Then there exist integers a,ba, b with 1<a<d1 < a < d and 1<b1 < b such that d=abd = ab. Since a∣da \mid d and d∣nd \mid n, transitivity of divisibility gives a∣na \mid n. But 2≤a<d2 \le a < d, contradicting the minimality of dd. □\square

Theorem 3 (Square Root Bound for Composite Numbers). If n≥2n \ge 2 is composite, then nn has a prime factor pp satisfying p≤⌊n⌋p \le \lfloor\sqrt{n}\rfloor.

Proof. Since nn is composite, write n=abn = ab with integers 1<a≤b<n1 < a \le b < n (choosing a≤ba \le b without loss of generality). Then a2≤a⋅b=na^2 \le a \cdot b = n, so a≤na \le \sqrt{n}. Let pp be the smallest divisor of aa with p≥2p \ge 2. By Theorem 2, pp is prime. Since p∣ap \mid a and a∣na \mid n, we have p∣np \mid n. Moreover, p≤a≤np \le a \le \sqrt{n}, so p≤⌊n⌋p \le \lfloor\sqrt{n}\rfloor. □\square

Corollary 1 (Primality Test). An integer n≥2n \ge 2 is prime if and only if no prime p≤⌊n⌋p \le \lfloor\sqrt{n}\rfloor divides nn.

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. □\square

Trial Division

Definition 2 (Trial Division Procedure). Given input n≥2n \ge 2, the trial division algorithm operates as follows:

  1. Initialize d←2d \leftarrow 2.
  2. While d2≤nd^2 \le n:
    • (a) While d∣nd \mid n: replace n←n/dn \leftarrow n/d and record dd as a prime factor.
    • (b) Set d←d+1d \leftarrow d + 1.
  3. If n>1n > 1, record nn as a prime factor.
  4. Return the largest recorded prime factor.

Theorem 4 (Correctness of Trial Division). The trial division procedure produces the complete prime factorization of the input nn, and in particular correctly identifies its largest prime factor.

Proof. Let n0n_0 denote the original input. We maintain the following loop invariant:

Invariant I(d)\mathcal{I}(d): The current value of nn satisfies n≥1n \ge 1, n0=n⋅Qn_0 = n \cdot Q where QQ is the product of all factors extracted so far, and nn has no prime factor strictly less than dd.

Initialization. At d=2d = 2, no factors have been extracted (Q=1Q = 1, n=n0n = n_0), and the condition “no prime factor <2< 2” is vacuous. So I(2)\mathcal{I}(2) holds.

Maintenance. Suppose I(d)\mathcal{I}(d) holds at the start of an iteration.

  • Case d∣nd \mid n: By I(d)\mathcal{I}(d), nn has no prime factor less than dd, so the smallest divisor of nn that is ≥2\ge 2 is at least dd. Since d∣nd \mid n and d≥2d \ge 2, the smallest divisor of nn is at most dd, hence it equals dd. By Theorem 2, dd is prime. We divide nn by dd (possibly multiple times), extracting all powers of dd. After this, d∤nd \nmid n, and no prime factor of nn is ≤d\le d.

  • Case d∤nd \nmid n: The invariant is unaffected. We increment dd.

In either case, after incrementing dd to d+1d+1, invariant I(d+1)\mathcal{I}(d+1) holds.

Termination. The loop exits when d2>nd^2 > n. At this point, I(d)\mathcal{I}(d) holds: nn has no prime factor less than dd. If n>1n > 1, then nn is prime, because if nn were composite, Theorem 3 would guarantee a prime factor p≤n<dp \le \sqrt{n} < d, contradicting I(d)\mathcal{I}(d). So nn is the final (and largest) prime factor.

Completeness. Every prime factor of n0n_0 is either extracted in step 2(a) or is the remaining nn in step 3. The largest among these is the answer. □\square

Theorem 5 (Termination). The trial division procedure terminates for all inputs n≥2n \ge 2.

Proof. Define the measure function μ=n+(⌊n⌋−d)\mu = n + (\lfloor\sqrt{n}\rfloor - d). In step 2(a), each division reduces nn by a factor of at least 2, so nn strictly decreases. In step 2(b), dd increases by 1. In both cases, μ\mu strictly decreases. Since μ\mu is a non-negative integer (the loop condition ensures d≤⌊n⌋d \le \lfloor\sqrt{n}\rfloor), the algorithm terminates. □\square

Factorization of 600,851,475,143

Lemma 1. 600 851 475 143=71×839×1471×6857600\,851\,475\,143 = 71 \times 839 \times 1471 \times 6857.

Proof. We verify by direct computation:

71×839=59 569.71 \times 839 = 59\,569. 59 569×1471=87 625 999.59\,569 \times 1471 = 87\,625\,999. 87 625 999×6857=600 851 475 143.✓87\,625\,999 \times 6857 = 600\,851\,475\,143. \quad \checkmark

□\square

Lemma 2. Each of 71,839,1471,685771, 839, 1471, 6857 is prime.

Proof. By Corollary 1, it suffices to check that no prime p≤⌊n⌋p \le \lfloor\sqrt{n}\rfloor divides nn for each factor.

  • 71: ⌊71⌋=8\lfloor\sqrt{71}\rfloor = 8. Primes to check: 2,3,5,72, 3, 5, 7. We have 71=2⋅35+171 = 2 \cdot 35 + 1, 71=3⋅23+271 = 3 \cdot 23 + 2, 71=5⋅14+171 = 5 \cdot 14 + 1, 71=7⋅10+171 = 7 \cdot 10 + 1. None divide 71. Hence 71 is prime.

  • 839: ⌊839⌋=28\lfloor\sqrt{839}\rfloor = 28. Primes to check: 2,3,5,7,11,13,17,19,232, 3, 5, 7, 11, 13, 17, 19, 23. Since 839 is odd, not divisible by 3 (8+3+9=208+3+9=20, 3∤203 \nmid 20), does not end in 0 or 5, and direct verification confirms 7∤8397 \nmid 839 (839=7⋅119+6839 = 7 \cdot 119 + 6), 11∤83911 \nmid 839 (839=11⋅76+3839 = 11 \cdot 76 + 3), 13∤83913 \nmid 839 (839=13⋅64+7839 = 13 \cdot 64 + 7), 17∤83917 \nmid 839 (839=17⋅49+6839 = 17 \cdot 49 + 6), 19∤83919 \nmid 839 (839=19⋅44+3839 = 19 \cdot 44 + 3), 23∤83923 \nmid 839 (839=23⋅36+11839 = 23 \cdot 36 + 11). Hence 839 is prime.

  • 1471: ⌊1471⌋=38\lfloor\sqrt{1471}\rfloor = 38. Primes to check: 2,3,5,7,11,13,17,19,23,29,31,372, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37. 1471 is odd; digit sum 1+4+7+1=131+4+7+1=13, 3∤133 \nmid 13; does not end in 0 or 5. Checking: 7∤14717 \nmid 1471 (1471=7⋅210+11471 = 7 \cdot 210 + 1), 11∤147111 \nmid 1471 (1471=11⋅133+81471 = 11 \cdot 133 + 8), 13∤147113 \nmid 1471 (1471=13⋅113+21471 = 13 \cdot 113 + 2), 17∤147117 \nmid 1471 (1471=17⋅86+91471 = 17 \cdot 86 + 9), 19∤147119 \nmid 1471 (1471=19⋅77+81471 = 19 \cdot 77 + 8), 23∤147123 \nmid 1471 (1471=23⋅63+221471 = 23 \cdot 63 + 22), 29∤147129 \nmid 1471 (1471=29⋅50+211471 = 29 \cdot 50 + 21), 31∤147131 \nmid 1471 (1471=31⋅47+141471 = 31 \cdot 47 + 14), 37∤147137 \nmid 1471 (1471=37⋅39+281471 = 37 \cdot 39 + 28). Hence 1471 is prime.

  • 6857: ⌊6857⌋=82\lfloor\sqrt{6857}\rfloor = 82. Primes to check: 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,792, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79. 6857 is odd; digit sum 6+8+5+7=266+8+5+7=26, 3∤263 \nmid 26; does not end in 0 or 5. Checking representative cases: 7∤68577 \nmid 6857 (6857=7⋅979+46857 = 7 \cdot 979 + 4), 11∤685711 \nmid 6857 (6857=11⋅623+46857 = 11 \cdot 623 + 4), 13∤685713 \nmid 6857 (6857=13⋅527+66857 = 13 \cdot 527 + 6), 71∤685771 \nmid 6857 (6857=71⋅96+416857 = 71 \cdot 96 + 41), 73∤685773 \nmid 6857 (6857=73⋅93+686857 = 73 \cdot 93 + 68), 79∤685779 \nmid 6857 (6857=79⋅86+636857 = 79 \cdot 86 + 63). No prime up to 82 divides 6857. Hence 6857 is prime.

□\square

Theorem 6. The largest prime factor of 600 851 475 143600\,851\,475\,143 is 68576857.

Proof. By Lemma 1, 600 851 475 143=71×839×1471×6857600\,851\,475\,143 = 71 \times 839 \times 1471 \times 6857. By Lemma 2, all four factors are prime. By the Fundamental Theorem of Arithmetic, this is the unique prime factorization. The largest factor is 68576857. □\square

Editorial

We remove prime factors by trial division. Starting with d=2d = 2, we divide the current value of nn by dd as long as dd is a factor, then increment dd and continue while d2≤nd^2 \le n. 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 n=600 851 475 143n = 600\,851\,475\,143:

StepddActionRemaining nn
17171∣n71 \mid n; divide8 462 696 8338\,462\,696\,833
2839839∣n839 \mid n; divide10 086 64710\,086\,647
314711471∣n1471 \mid n; divide6 8576\,857
41472d2=2 166 784>6857d^2 = 2\,166\,784 > 6857; exit loop6 8576\,857

Return n=6857n = 6857.

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 O(n)O(\sqrt{n}) time and O(1)O(1) space in the worst case.

Proof.

Time bound. The outer loop increments dd from 2 up to at most ⌊n⌋\lfloor\sqrt{n}\rfloor (where nn is the current value). We claim the total number of iterations of both the inner and outer loops combined is O(n0)O(\sqrt{n_0}) where n0n_0 is the original input.

The outer variable dd increases by 1 each outer iteration and runs while d2≤n≤n0d^2 \le n \le n_0, so there are at most ⌊n0⌋\lfloor\sqrt{n_0}\rfloor outer iterations. For the inner loop: each inner iteration divides nn by d≥2d \ge 2, so nn halves (at least) each time. Since nn starts at n0n_0 and each division produces a strict decrease, the total number of inner iterations across all values of dd is at most ⌊log⁡2n0⌋\lfloor\log_2 n_0\rfloor. The total iteration count is thus O(n0+log⁡n0)=O(n0)O(\sqrt{n_0} + \log n_0) = O(\sqrt{n_0}).

Space bound. The algorithm uses only the variables dd and nn, which is O(1)O(1) additional space.

Tightness. When n0n_0 is prime, the inner loop never executes, and the outer loop runs for d=2,3,…,⌊n0⌋d = 2, 3, \ldots, \lfloor\sqrt{n_0}\rfloor, giving Θ(n0)\Theta(\sqrt{n_0}) iterations. Hence the worst-case time is Θ(n0)\Theta(\sqrt{n_0}).

Application. For n0=600 851 475 143n_0 = 600\,851\,475\,143, the worst-case bound is n0≈775 146\sqrt{n_0} \approx 775\,146 iterations. In practice, the first factor d=71d = 71 is found early, reducing nn and the effective search space. □\square

Answer

6857\boxed{6857}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_003/solution.cpp
#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;
}