Java ProgramsNumbersFind Largest Prime Factor

Find Largest Prime Factor in Java

intermediate·  Numbers  ·  Number Theory

Problem

The largest prime factor of a number is the biggest prime that divides it evenly, found by stripping away every smaller prime factor first.

Given a number, find its largest prime factor.

Input
780
Output
Largest prime factor: 13

Java Program

Java
public class LargestPrimeFactor { public static void main(String[] args) { int n = 780; int largest = -1; for (int i = 2; (long) i * i <= n; i++) { while (n % i == 0) { largest = i; // record i as the largest factor found so far n /= i; // strip this factor out before trying the next candidate } } if (n > 1) largest = n; // whatever remains is itself prime System.out.println("Largest prime factor: " + largest); } }

Output

Largest prime factor: 13

Core Logic

Repeatedly dividing out every factor of each candidate divisor, from smallest to largest, strips the number down to nothing but its largest prime factor by the time the loop ends.

How It Works
  1. 1The loop tries every candidate divisor i starting from 2, only while i * i &lt;= n.
  2. 2The inner while (n % i == 0) keeps dividing i out of n for as long as it evenly divides, recording i as the largest factor found so far each time.
  3. 3Once i no longer divides n, the outer loop moves to the next candidate.
  4. 4If anything greater than 1 remains after the loop, that remainder is itself prime and becomes the final answer.
For 780, dividing out 2 twice leaves 195, dividing out 3 leaves 65, dividing out 5 leaves 13 — and since 13 * 13 &gt; 13, the loop stops and the remaining 13 is reported as the largest prime factor.
💡

Key Point: The loop only ever needs to reach √n relative to the *shrinking* value of n, not the original — once every smaller factor is divided out, whatever's left is guaranteed to be prime.

Complexity
Time Complexity: O(√n)Space Complexity: O(1)

Why: The loop only needs to try divisors up to √n, since any factor larger than that must pair with one already found, and only the running largest value is kept.

Key Concepts

trial divisionsquare-root boundrepeated division

Approach 2: Handle 2 Separately (Odd-Only Loop)

Java
public class LargestPrimeFactorOddOnly { public static void main(String[] args) { int n = 780; int largest = -1; while (n % 2 == 0) { largest = 2; n /= 2; } // Only odd candidates remain possible once every factor of 2 is gone for (int i = 3; (long) i * i <= n; i += 2) { while (n % i == 0) { largest = i; n /= i; } } if (n > 1) largest = n; System.out.println("Largest prime factor: " + largest); } }

Output

Largest prime factor: 13

Core Logic

Dividing out every factor of 2 up front means the main loop never has to waste time testing even candidates again — it can skip straight to odd numbers.

How It Works
  1. 1A while (n % 2 == 0) loop strips out every factor of 2 before anything else runs.
  2. 2The main loop then starts at 3 and increments by 2 each time, trying only odd candidates.
  3. 3The same inner while division logic applies for each odd candidate.
  4. 4As before, whatever remains greater than 1 after the loop is the final largest factor.
For 780, dividing out 2 twice up front leaves 195; the odd-only loop then finds 3 and 5, leaving 13 as the remaining largest factor.
💡

Key Point: Since every even number past 2 can never be prime, testing them in the main loop was always wasted work — this cuts the number of candidates tried roughly in half.

Complexity
Time Complexity: O(√n)Space Complexity: O(1)

Why: Handling factor 2 up front lets the main loop skip every even candidate divisor, roughly halving the number of iterations tried, though the asymptotic bound is unchanged.

Key Concepts

even/odd optimizationskip even candidates

Related Programs