Largest prime factor | Practice | GeeksforGeeks

Given a number N, the task is to find the largest prime factor of that number.
 
Example 1:
Input:
N = 5
Output:
5
Explanation:
5 has 1 prime factor 
i.e 5 only.

Example 2:
Input:
N = 24
Output:
3
Explanation:
24 has 2 prime factors 
3 and 2 in which 3 is 
greater


Your Task:
You don't need to read input or print anything. Your task is to complete the function largestPrimeFactor() which takes an integer N as input parameters and returns an integer, largest prime factor of N.
 
Expected Time Complexity: O(sqrt(N))
Expected Space Complexity: O(1)
 
Constraints:
1 <= N <= 105
0
mankesh016in 11 hours
 if(N<2) return N;
        long long ans;
        
        for(int i=2; i*i<=N; i++){
            if(N%i==0){
                while(N%i==0){
                    N/=i;
                }
                ans=i;
            }
        }
        if(N!=1) ans=N;
        
        return ans;
0
pikucode152 weeks ago
class Solution{public:    long long int largestPrimeFactor(int N){       int largestPrime=-1;       for(int i=2;i*i<=N;i++)       {           if(N%i==0)           {               while(N%i==0)               {                   largestPrime=i;                   N=N/i;               }           }       }       if(N>1)           largestPrime=N;       return largestPrime;   }};
0
pikucode152 weeks ago
class Solution{public:    int prime[100001];   long long int largestPrimeFactor(int N){       for(int i=0;i<100001;i++)           prime[i]=i;       for(int i=2;i*i<=100000;i++)       {           if(prime[i]==i)           {               for(int j=i*i;j<=100000;j+=i)               {                   if(prime[j]==j)                       prime[j]=i;               }           }       }       while(prime[N]!=N)           N=N/prime[N];       return N;   }};
0
psp91393109462 weeks ago
class Solution{public:    long long int largestPrimeFactor(int N){       int i=2;       while(N!=1){           if(N%i==0){N=N/i;}           else{i++;}       }       return i;   }};
0
bahubuli2 weeks ago
   long long int largestPrimeFactor(int N){
        
        vector<bool>primes(N+1,true);
        primes[0]=0;
        primes[1]=0;
        
        for(int i=2;i<=N;i++)
        {
            if(primes[i])
            for(int m = i*i;m<=N;m+=i)
                primes[m]=false;   
        }
        
        for(int i=N;i>1;i--)
            if(primes[i]==true && N%i==0) return i;
        
       return 1; 
    }
};
0
moaslam8263 weeks ago
c++ easy soln
long long int largestPrimeFactor(int N){
        int X=1e5+1;
        vector<int>arr (X,1);
        arr[0]=arr[1]=0;
       int hp[X]; hp[0]=0; hp[1]=1;
       for(int i=2;i<X;i++){
           if(arr[i]==1){
           for(int j=i;j<X;j+=i){
               arr[j]=0;
               hp[j]=i;
            }
           }
       }
       
       return hp[N];
    }
+1
mashhadihossain1 month ago
SIMPLE JAVA SOLUTION (0.2/1.2 SEC)
class Solution{     static boolean isPrime(int n)   {       int k=(int)Math.sqrt(n);       for(int i=2;i<=k;i++)       {           if(n%i==0)           {               return false;           }       }       return true;   }      static long largestPrimeFactor(int N) {       ArrayList<Long> list=new ArrayList<Long>();       // int x=Math.sqrt(N);       for(int i=1;i<=N;i++)       {           if(N%i==0 && isPrime(i))           {               list.add((long)i);           }       }       Collections.sort(list);       return list.get(list.size()-1);   }}
0
mohammadtanveer75401 month ago
class Solution{
public:
    int prime_no(int n)
    {
        int k=sqrt(n);
        for(int i=2;i<=k;i++)
            if(n%i==0)
             return -1;
        return 1;
    }
    vector<int> primes;
    
    long long int largestPrimeFactor(int N){
        for(int i=2;i<=1e5;i++)
            if(prime_no(i)==1)
                primes.push_back(i);  // using Hashing
        int i = primes.size()-1; 
        while(true)
        {
            if(N%primes[i]==0)
                return primes[i];
            
            i--;
        }
        
    }
};
0
rohitjug19csPremium1 month ago
JAVA Solution O(sqrt(N)) :
static long largestPrimeFactor(int N) {
      if(N==1)return (long)1;
      long lgp=2;
      while(lgp*lgp <= N){
        if(N%lgp == 0)N/=lgp;
        else lgp++;    
      }
      return Long.max(lgp, (long)N);
    }
0
gagansuneja2 months ago
JS Solution, 
use the principal that factor exists in pair.since starting from 1 max factor can go till sqrt of n, so we need to check all prime no which divide n from 1 to sqrt of n and similarly for corresponding pair which is quotient of (n/i)
ex 49, sqrt(49) = 7
1,7 = check for 1 not prime exclude check 7 is prime save it
2 not divides
3 not divides
4 same
5 same
6 same
7,1 here we have already saved 7
P.S. Check prime using sieve algorithm
JS Solution
class Solution {

    largestPrimeFactor(N){
        //code here
        if(N === 1) return 1;
        let maxPrimeFactor = 2;
        for(let i=1;i*i<=N;i++){
            if(N%i===0){
                if(this.isPrime(i) && i > maxPrimeFactor) {
                    maxPrimeFactor = i
                }
                if(this.isPrime(N/i) && (N/i) > maxPrimeFactor) {
                    maxPrimeFactor = N/i;
                }
            }
        }
        return maxPrimeFactor;
    }
    
    isPrime(number) {
        if(number === 2 || number === 3)  return true
        if(number%2 === 0 || number % 3 === 0 ) return false;
        
        
        for(let i=5;i*i<=number;i+=6){
            if(number%i === 0 || number%(i+2)===0) {
                return false;
            }
        }
        return true
    }
}
 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.