All Euler problems
Project Euler

Largest Prime Factor

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

Source sync Apr 19, 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 p2p \ge 2 is prime if its only positive divisors are 1 and pp. An integer n2n \ge 2 that is not prime is composite.

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

n=p1a1p2a2prarn = 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 ai1a_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 n2n \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 ada \mid d and dnd \mid n, transitivity of divisibility gives ana \mid n. But 2a<d2 \le a < d, contradicting the minimality of dd. \square

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

Proof. Since nn is composite, write n=abn = ab with integers 1<ab<n1 < a \le b < n (choosing aba \le b without loss of generality). Then a2ab=na^2 \le a \cdot b = n, so ana \le \sqrt{n}. Let pp be the smallest divisor of aa with p2p \ge 2. By Theorem 2, pp is prime. Since pap \mid a and ana \mid n, we have pnp \mid n. Moreover, panp \le a \le \sqrt{n}, so pnp \le \lfloor\sqrt{n}\rfloor. \square

Corollary 1 (Primality Test). An integer n2n \ge 2 is prime if and only if no prime pnp \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 n2n \ge 2, the trial division algorithm operates as follows:

  1. Initialize d2d \leftarrow 2.
  2. While d2nd^2 \le n:
    • (a) While dnd \mid n: replace nn/dn \leftarrow n/d and record dd as a prime factor.
    • (b) Set dd+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 n1n \ge 1, n0=nQn_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 dnd \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 dnd \mid n and d2d \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, dnd \nmid n, and no prime factor of nn is d\le d.

  • Case dnd \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 pn<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 n2n \ge 2.

Proof. Define the measure function μ=n+(nd)\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 dnd \le \lfloor\sqrt{n}\rfloor), the algorithm terminates. \square

Factorization of 600,851,475,143

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

Proof. We verify by direct computation:

71×839=59569.71 \times 839 = 59\,569. 59569×1471=87625999.59\,569 \times 1471 = 87\,625\,999. 87625999×6857=600851475143.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 pnp \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=235+171 = 2 \cdot 35 + 1, 71=323+271 = 3 \cdot 23 + 2, 71=514+171 = 5 \cdot 14 + 1, 71=710+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, 3203 \nmid 20), does not end in 0 or 5, and direct verification confirms 78397 \nmid 839 (839=7119+6839 = 7 \cdot 119 + 6), 1183911 \nmid 839 (839=1176+3839 = 11 \cdot 76 + 3), 1383913 \nmid 839 (839=1364+7839 = 13 \cdot 64 + 7), 1783917 \nmid 839 (839=1749+6839 = 17 \cdot 49 + 6), 1983919 \nmid 839 (839=1944+3839 = 19 \cdot 44 + 3), 2383923 \nmid 839 (839=2336+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, 3133 \nmid 13; does not end in 0 or 5. Checking: 714717 \nmid 1471 (1471=7210+11471 = 7 \cdot 210 + 1), 11147111 \nmid 1471 (1471=11133+81471 = 11 \cdot 133 + 8), 13147113 \nmid 1471 (1471=13113+21471 = 13 \cdot 113 + 2), 17147117 \nmid 1471 (1471=1786+91471 = 17 \cdot 86 + 9), 19147119 \nmid 1471 (1471=1977+81471 = 19 \cdot 77 + 8), 23147123 \nmid 1471 (1471=2363+221471 = 23 \cdot 63 + 22), 29147129 \nmid 1471 (1471=2950+211471 = 29 \cdot 50 + 21), 31147131 \nmid 1471 (1471=3147+141471 = 31 \cdot 47 + 14), 37147137 \nmid 1471 (1471=3739+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, 3263 \nmid 26; does not end in 0 or 5. Checking representative cases: 768577 \nmid 6857 (6857=7979+46857 = 7 \cdot 979 + 4), 11685711 \nmid 6857 (6857=11623+46857 = 11 \cdot 623 + 4), 13685713 \nmid 6857 (6857=13527+66857 = 13 \cdot 527 + 6), 71685771 \nmid 6857 (6857=7196+416857 = 71 \cdot 96 + 41), 73685773 \nmid 6857 (6857=7393+686857 = 73 \cdot 93 + 68), 79685779 \nmid 6857 (6857=7986+636857 = 79 \cdot 86 + 63). No prime up to 82 divides 6857. Hence 6857 is prime.

\square

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

Proof. By Lemma 1, 600851475143=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 d2nd^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=600851475143n = 600\,851\,475\,143:

StepddActionRemaining nn
17171n71 \mid n; divide84626968338\,462\,696\,833
2839839n839 \mid n; divide1008664710\,086\,647
314711471n1471 \mid n; divide68576\,857
41472d2=2166784>6857d^2 = 2\,166\,784 > 6857; exit loop68576\,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 d2nn0d^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 d2d \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 log2n0\lfloor\log_2 n_0\rfloor. The total iteration count is thus O(n0+logn0)=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,,n0d = 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=600851475143n_0 = 600\,851\,475\,143, the worst-case bound is n0775146\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;
}