Find largest prime factor of a number

Given a positive integer ‘n'( 1 <= n <= 1015). Find the largest prime factor of a number.

Input: 6Output: 3ExplanationPrime factor of 6 are- 2, 3Largest of them is '3'Input: 15Output: 5
Recommended Practice

Method 1:

The approach is simple, just factorize the given number by dividing it with the divisor of a number and keep updating the maximum prime factor. See this to understand more.

Below is the implementation of the above approach

C++

 // C++ Program to find largest prime // factor of number #include  #include using namespace std;   // A function to find largest prime factor long long maxPrimeFactors(long long n) {     // Initialize the maximum prime factor     // variable with the lowest one     long long 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      while (n % 3 == 0) {         maxPrime = 3;         n=n/3;      }       // now we have to iterate only for integers      // who does not have prime factor 2 and 3     for (int i = 5; i <= sqrt(n); i += 6) {         while (n % i == 0) {             maxPrime = i;             n = n / i;         }       while (n % (i+2) == 0) {             maxPrime = i+2;             n = n / (i+2);         }     }       // This condition is to handle the case     // when n is a prime number greater than 4     if (n > 4)         maxPrime = n;       return maxPrime; }   // Driver program to test above function int main() {     long long n = 15;     cout << maxPrimeFactors(n) << endl;       n = 25698751364526;     cout <<  maxPrimeFactors(n);   }

C

 // C Program to find largest prime // factor of number #include  #include    // A function to find largest prime factor long long maxPrimeFactors(long long n) {     // Initialize the maximum prime factor     // variable with the lowest one     long long 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     while (n % 3 == 0) {         maxPrime = 3;         n = n / 3;     }       // now we have to iterate only for integers     // who does not have prime factor 2 and 3     for (int i = 5; i*i<=n; i += 6) {         while (n % i == 0) {             maxPrime = i;             n = n / i;         }         while (n % (i + 2) == 0) {             maxPrime = i + 2;             n = n / (i + 2);         }     }       // This condition is to handle the case     // when n is a prime number greater than 4     if (n > 4)         maxPrime = n;       return maxPrime; }   // Driver program to test above function int main() {     long long n = 15;     printf("%lld\n", maxPrimeFactors(n));       n = 25698751364526;     printf("%lld", maxPrimeFactors(n));       return 0; }

Java

 // Java Program to find largest // prime factor of number import java.io.*; import java.util.*;   class GFG {       // function to find largest prime factor     static long maxPrimeFactors(long n)     {         // Initialize the maximum prime         // factor variable with the         // lowest one         long maxPrime = -1;           // Print the number of 2s         // that divide n         while (n % 2 == 0) {             maxPrime = 2;               // equivalent to n /= 2             n >>= 1;         }         // n must be odd at this point         while (n % 3 == 0) {             maxPrime = 3;             n = n / 3;         }           // now we have to iterate only for integers         // who does not have prime factor 2 and 3         for (int i = 5; i <= Math.sqrt(n); i += 6) {             while (n % i == 0) {                 maxPrime = i;                 n = n / i;             }             while (n % (i + 2) == 0) {                 maxPrime = i + 2;                 n = n / (i + 2);             }         }           // This condition is to handle the case         // when n is a prime number greater than 4         if (n > 4)             maxPrime = n;           return maxPrime;     }       // Driver code     public static void main(String[] args)     {         Long n = 15l;         System.out.println(maxPrimeFactors(n));           n = 25698751364526l;         System.out.println(maxPrimeFactors(n));     } }

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     while n % 3 == 0:         maxPrime = 3         n=n/3           # now we have to iterate only for integers      # who does not have prime factor 2 and 3     for i in range(5, int(math.sqrt(n)) + 1, 6):         while n % i == 0:             maxPrime = i             n = n / i         while n % (i+2) == 0:             maxPrime = i+2             n = n / (i+2)               # This condition is to handle the      # case when n is a prime number      # greater than 4     if n > 4:         maxPrime = n           return int(maxPrime)   # Driver code to test above function n = 15 print(maxPrimeFactors(n))   n = 25698751364526 print(maxPrimeFactors(n))

C#

 // C# program to find largest // prime factor of number using System;   class GFG {       // function to find largest prime factor     static long maxPrimeFactors(long n)     {         // Initialize the maximum prime         // factor variable with the         // lowest one         long maxPrime = -1;           // Print the number of 2s         // that divide n         while (n % 2 == 0) {             maxPrime = 2;               // equivalent to n /= 2             n >>= 1;         }         // n must be odd at this point         while (n % 3 == 0) {             maxPrime = 3;             n = n / 3;         }         // now we have to iterate only for integers         // who does not have prime factor 2 and 3         for (int i = 5; i <= Math.Sqrt(n); i += 6) {             while (n % i == 0) {                 maxPrime = i;                 n = n / i;             }             while (n % (i + 2) == 0) {                 maxPrime = i + 2;                 n = n / (i + 2);             }         }           // This condition is to handle the case         // when n is a prime number greater than 4         if (n > 4)             maxPrime = n;           return maxPrime;     }       // Driver code     public static void Main()     {         long n = 15L;         Console.WriteLine(maxPrimeFactors(n));           n = 25698751364526L;         Console.WriteLine(maxPrimeFactors(n));     } }

Javascript

 

PHP

 >= 1;      }     // n must be odd at this point     while ($n % 3 == 0) {  $maxPrime = 3;         $n=$n/3;      }     // now we have to iterate only for integers      // who does not have prime factor 2 and 3     for ($i = 3; $i <= sqrt($n); $i += 2)     {         while ($n % $i == 0)         {             $maxPrime = $i;             $n = $n / $i;  }  while ($n % ($i+2) == 0) {  $maxPrime = $i+2;  $n = $n / ($i+2);         }     }       // This condition is      // to handle the case      // when n is a prime      // number greater than 4     if ($n > 4)  $maxPrime = $n;  return $maxPrime; }       // Driver Code     $n = 15;  echo maxPrimeFactors($n), "\n";       $n = 25698751364526;  echo maxPrimeFactors($n), "\n";     ?>

Output

5
328513



Time complexity:
Auxiliary space:

Method 2:

Follow the steps below for the implementation:

1. Initialize variables largest_prime to -1, i to 2, and n to the input integer.
2. Start a while loop that continues as long as i * i <= n. This loop will iterate through all possible factors of n.
3. 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.
4. In the inner loop, set largest_prime to i, and update n by dividing it by i.
5. At the end of the inner loop, increment i by 1.
6. 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.
7. Return largest_prime.

Below is the implementation of the above approach:

C++

 // C++ code to find largest prime // factor of number #include  using namespace std;   int maxPrimeFactors(long long 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      */     int largest_prime = -1;     int 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; }   int main() {     long long n = 15;     cout << maxPrimeFactors(n) << endl;       n = 25698751;     cout << maxPrimeFactors(n) << endl;       return 0; }   // This code is contributed by Susobhan Akhuli

Java

 /*package whatever //do not write package name here */   import java.io.*;   class GFG {       static long maxPrimeFactors(long 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          */         long largest_prime = -1;         long 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;     }     public static void main (String[] args) {         long n = 15;         System.out.println(maxPrimeFactors(n));           n = 25698751;         System.out.println(maxPrimeFactors(n));       } } //code contributed by shubhamrajput6156

Python3

 import math   def max_prime_factors(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   if __name__ == "__main__":     n = 15     print(max_prime_factors(n))       n = 25698751     print(max_prime_factors(n))

C#

 // C# code to find largest prime // factor of number using System;   class GFG {     static int MaxPrimeFactors(long 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         */         int largestPrime = -1;         int i = 2;         while (i * i <= n)         {             while (n % i == 0)             {                 largestPrime = i;                 n = n / i;             }             i = i + 1;         }         if (n > 1)         {             largestPrime = (int)n;         }         return largestPrime;     }       static void Main()     {         long n = 15;         Console.WriteLine(MaxPrimeFactors(n));           n = 25698751;         Console.WriteLine(MaxPrimeFactors(n));     } }

Javascript

 // JS code to find largest prime // factor of number   function maxPrimeFactors(n) {     /*      * Find the largest prime factor of a positive leteger      * 'n'      * @param n: positive leteger (1 <= n <= 10^15)      * @return: largest prime factor of n      */     let largest_prime = -1;     let 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; }   let n = 15; document.write(maxPrimeFactors(n));   n = 25698751; document.write(maxPrimeFactors(n));

Output

5
1409



Time complexity: O(sqrt(n)).
Auxiliary space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next