InferenceAI

Enter at least two characters to search.

Start free

Largest Prime Factor

easyMath15 min10 XP

Return the largest prime factor of an integer n≥2n \ge 2.

Requirements

  • Trial division up to n\sqrt{n} is enough: O(n)O(\sqrt{n}) time.

Constraints

  • 2≤n≤10122 \le n \le 10^{12}

Examples

EXAMPLE WALKTHROUGHseveral factors

A guided view of the published example, not a trace of your code.

INPUT[13195]
TASKlargest_prime_factor()
EXPECTED OUTPUT29

Start with the input values shown in this example.

Example 1 of 3

Input: n = 13195

Output: 29

Explanation: several factors

Input: n = 2

Output: 2

Explanation: smallest prime

Input: n = 97

Output: 97

Explanation: prime

Hint 1

Divide out each factor completely before moving to the next candidate.

Hint 2

Whatever remains above 1 after the loop is itself prime.

Topics

DSAMathPython