All problems

Problem 349

Largest Prime Factor

Easy · 10 pts · Math, Number Theory

Problem

Print the largest prime number that divides N. Trial division works well: divide out each factor d starting from 2 for as long as it divides N, and only test d while d · d <= N. Whatever is left above 1 at the end is itself a prime factor. For example, 84 = 2 · 2 · 3 · 7, so its largest prime factor is 7; if N itself is prime, the answer is N. The numbers in this problem can be larger than a 32-bit int can hold (about 2.1 × 10^9), so use a 64-bit integer type: long long in C++, long in Java (Python needs nothing special).

Input

A single integer N.

Output

Print the largest prime factor of N.

Constraints

2 <= N <= 1000000000000

Example input

13195

Example output

29

Explanation

13195 factors into the primes 5 · 7 · 13 · 29. The largest of these is 29, so the answer is 29.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Perfect Square Check Next: Sum of Primes up to N