Python Program for Find largest prime factor of a number Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Given a positive integer \'n\'( 1 <= n <= 1015). Find the largest prime factor of a number. Input: 6 Output: 3 Explanation Prime factor of 6 are- 2, 3 Largest of them is 3 Input: 15 Output: 5 Python3 # Python3 code to find largest prime # factor of number import math # A function to find largest prime factor def maxPrimeFactors (n): # Initialize the maximum prime factor # variable with the lowest one maxPrime = -1 # Print the number of 2s that divide n while n % 2 == 0: maxPrime = 2 n >>= 1 # equivalent to n /= 2 # n must be odd at this point, # thus skip the even numbers and # iterate only for odd integers for i in range(3, int(math.sqrt(n)) + 1, 2): while n % i == 0: maxPrime = i n = n / i # This condition is to handle the # case when n is a prime number # greater than 2 if n > 2: maxPrime = n return int(maxPrime) # Driver code to test above function n = 15 print(maxPrimeFactors(n)) n = 25698751364526 print(maxPrimeFactors(n)) # This code is contributed by "Sharad_Bhardwaj". Output5 328513 Time complexity: \text{O}(\sqrt{n}) Auxiliary space: \text{O}(1) Please refer complete article on Find largest prime factor of a number for more details! Method 2: Follow the steps below for the implementation: Initialize variables largest_prime to -1, i to 2, and n to the input integer.Start a while loop that continues as long as i * i <= n. This loop will iterate through all possible factors of n.In the while loop, start another while loop that continues as long as n % i == 0. This inner loop will divide n by i until n is no longer divisible by i.In the inner loop, set largest_prime to i, and update n by dividing it by i.At the end of the inner loop, increment i by 1.After the outer loop, if n > 1, set largest_prime to n. This is because n could be a prime number larger than any of its factors.Return largest_prime. Python3 def largest_prime_factor(n): """ Find the largest prime factor of a positive integer 'n' :param n: positive integer ( 1 <= n <= 10^15) :return: largest prime factor of n """ largest_prime = -1 i = 2 while i * i <= n: while n % i == 0: largest_prime = i n = n // i i = i + 1 if n > 1: largest_prime = n return largest_prime n = 15 print(largest_prime_factor(n)) n = 25698751364526 print(largest_prime_factor(n)) Output5 328513 Time complexity: O(sqrt(n)).Auxiliary space: O(1), Comment K kartik Follow Improve K kartik Follow Improve Article Tags : Python Programs DSA Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 2 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 14 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 6 min read Problem of The Day - Develop the Habit of Coding 5 min read Like