![]() |
VOOZH | about |
Given a positive integer n (1 ≤ n ≤ 1015), the task is to find its largest prime factor the biggest prime number that divides n exactly. For Example:
Input: n = 6
Output: 3
Explanation: prime factors of 6 are 2 and 3, and the largest among them is 3.
Let's explore different methods to find largest prime factor of a number in Python.
sympy provides the function factorint() which directly returns all prime factors of a number and their powers, and using max() we can extract the largest number.
Output
5
Another sympy method is primefactors(), it directly gives a sorted list of all prime factors of a number.
Output
3
This method divides the number continuously by small divisors starting from 2 until only the largest prime factor remains.
17
Explanation:
This method does not use any external modules. It uses a loop that keeps dividing the number by its smallest divisor until all smaller factors are removed.
5
Explanation: