Find largest prime factor of a number
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
The approach is simple, just factorise the given number by dividing it with the divisor of a number and keep updating the maximum prime factor. See this to understand more.
C++
// C++ Program to find largest prime // factor of number #include <iostream> #include<bits/stdc++.h> 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, thus skip // the even numbers and iterate only for // odd integers for (int i = 3; i <= sqrt(n); i += 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 maxPrime; } // Driver program to test above function int main() { long long n = 15; cout << maxPrimeFactors(n) << endl; n = 25698751364526; cout << maxPrimeFactors(n); } // This code is contributed by Shivi_Aggarwal |
chevron_right
filter_none
C
// C Program to find largest prime // factor of number #include <math.h> #include <stdio.h> // 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, thus skip // the even numbers and iterate only for // odd integers for (int i = 3; i <= sqrt(n); i += 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 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; } |
chevron_right
filter_none
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, // thus skip the even numbers // and iterate only for odd // integers for (int i = 3; i <= Math.sqrt(n); i += 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 maxPrime; } // Driver code public static void main(String[] args) { Long n = 15l; System.out.println(maxPrimeFactors(n)); n = 25698751364526l; System.out.println(maxPrimeFactors(n)); } } // This code is contributed by Gitanjali |
chevron_right
filter_none
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 = 15print(maxPrimeFactors(n)) n = 25698751364526print(maxPrimeFactors(n)) # This code is contributed by "Sharad_Bhardwaj". |
chevron_right
filter_none
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, // thus skip the even numbers // and iterate only for odd // integers for (int i = 3; i <= Math.Sqrt(n); i += 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 maxPrime; } // Driver code public static void Main() { long n = 15L; Console.WriteLine(maxPrimeFactors(n)); n = 25698751364526L; Console.WriteLine(maxPrimeFactors(n)); } } // This code is contributed by vt_m |
chevron_right
filter_none
PHP
<?php // PHP Program to find // largest prime factor // of number // A function to find // largest prime factor function 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; // equivalent to n /= 2 $n >>= 1; } // n must be odd at // this point, thus skip // the even numbers // and iterate only for // odd integers for ($i = 3; $i <= sqrt($n); $i += 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 $maxPrime; } // Driver Code $n = 15; echo maxPrimeFactors($n), "\n"; $n = 25698751364526; echo maxPrimeFactors($n), "\n"; // This code is contributed by aj_36 ?> |
chevron_right
filter_none
Output:
5 328513
Time complexity: ![]()
Auxiliary space: ![]()
Recommended Posts:
- Sum of largest prime factor of each number less than equal to n
- Find sum of a number and its maximum prime factor
- Find Largest Special Prime which is less than or equal to a given number
- k-th prime factor of a given number
- N-th prime factor of a given number
- Largest factor of a given number which is a perfect square
- Find the total number of composite factor for a given number
- Largest number with prime digits
- Largest number in [2, 3, .. n] which is co-prime with numbers in [2, 3, .. m]
- Largest number that divides x and is co-prime with y
- Prime Factor
- Largest number less than N whose each digit is prime number
- Queries for the smallest and the largest prime number of given digit
- Sum of largest divisible powers of p (a prime number) in a range
- Largest number not greater than N which can become prime after rearranging its digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


