Prime Factors | Practice | GeeksforGeeks

Given a number N. Find its unique prime factors in increasing order.
 
Example 1:
Input: N = 100
Output: 2 5
Explanation: 2 and 5 are the unique prime
factors of 100.

Example 2:
Input: N = 35
Output: 5 7
Explanation: 5 and 7 are the unique prime
factors of 35.

 
Your Task:
You don't need to read or print anything. Your task is to complete the function AllPrimeFactors() which takes N as input parameter and returns a list of all unique prime factors of N in increasing order.
 
Expected Time Complexity: O(N)
Expected Space Complexity: O(N)
 
Constraints:
1 <= N  <= 106
0
pruthvibelgaonkar4692 months ago
SIMPLEST SOLUTION
 
vector<int>AllPrimeFactors(int n) {   vector<int>v;   set<int>s;   if(n==2)   {       s.insert(n);   }   for(int i=2;i<=n;i++)   {       while(n%i==0)       {           s.insert(i);           n/=i;       }   }              for(auto it:s)       {           v.push_back(it);       }    return v;}
0
shreyash9779662 months ago
	vector<int>AllPrimeFactors(int n) {
	    // Code here
	    set<int>s;
	    vector<int>v;
	    if(n==2) s.insert(n);
	    for(int i=2;i<=n;i++){
	        while(n%i==0){
	            s.insert(i);
	            n/=i;
	        }
	    }
	    
	   for(auto it:s){
	       v.push_back(it);
	   }
	   return v;
	    
	}
0
ayazmrz982 months ago
static boolean isprime(int n)
   {
       if(n<=1)
       {
           return false;
       }
       for(int i=2;i<=Math.sqrt(n);i++)
       {
           if(n%i==0)
           {
               return false;
           }
       }
       return true;
   }
   public int[] AllPrimeFactors(int N)
   {
       ArrayList<Integer> arr = new ArrayList<Integer>();
       for(int i=2;i<=N;i++)
       {
           int m=0;
           if(N%i==0)
           {
              m=i; 
           if(isprime(m))
             {
                arr.add(m);
             }
           }
       }
       int[] result = new int[arr.size()];
       for(int i=0;i<arr.size();i++)
       {
           result[i]=arr.get(i);
       }
       return result;
   }
+1
shubhamkhavare2 months ago
Simple Java Code:
class Solution{   public Boolean isPrime(int n){       if(n <= 1)       {           return false;       }       for(int i = 2 ; i <= Math.sqrt(n) ; i++)       {           if(n%i == 0)           {               return false;           }       }       return true;   }   public int[] AllPrimeFactors(int N)   {       ArrayList<Integer> al = new ArrayList<Integer>();       for(int i = 1 ; i <= N ; i++)       {           int m = 0;           if(N%i == 0)           {              m = i;           }           if(isPrime(m))           {               al.add(i);           }       }       int[] arr = new int[al.size()];       for(int i = 0 ; i < al.size(); i++)       {           arr[i] = al.get(i);       }       return arr;   }}
0
snehads26133 months ago
vector<int>AllPrimeFactors(int n) {        // Code here    set<int> st;    vector<int> v;    for(int i=2;i*i<=n;i++){        while(n%i==0){            st.insert(i);            n/=i;        }    }    if(n>1){        st.insert(n);    }    for(auto it: st){        v.push_back(it);    }    return v;}
0
sahibealam293 months ago
class Solution{   public int[] AllPrimeFactors(int N)   {       ArrayList<Integer> arrLi = new ArrayList<>();       if(N%2==0){        arrLi.add(2);        while(N%2==0)            N/=2;    }    for(int i=3;i<=Math.sqrt(N);i+=2){        if(N%i==0)            arrLi.add(i);        while(N%i==0)            N/=i;    }    if(N!=1)        arrLi.add(N);    int[] arr = arrLi.stream().mapToInt(index -> index).toArray();    return arr;       // code here   }}
0
sahibealam293 months ago
class Solution{public:vector<int> AllPrimeFactors(int N) {    vector<int> vi;    if(N%2==0){        vi.push_back(2);        while(N%2==0)            N/=2;    }    for(int i=3;i<=sqrt(N);i+=2){        if(N%i==0)            vi.push_back(i);        while(N%i==0)            N/=i;    }    if(N!=1)        vi.push_back(N);    return vi;    // Code here}};
0
abhishekguptaaa3 months ago
class Solution{public:vector<int>AllPrimeFactors(int N) {        vector<int> v;        for(int i=2;i*i<=N;i++)    {        if(N%i==0)        {            while(N%i==0)            {                N=N/i;                v.push_back(i);            }        }    }    if(N>1)    {        v.push_back(N);    }       //for deleting the duplicate elements in the vector    v.erase(unique(v.begin(),v.end()),v.end());                   return v;        }};
0
akashdeepchauhan19994 months ago
class Solution{   public int[] AllPrimeFactors(int n)   {       ArrayList<Integer>al = new ArrayList<>();              int j = 0;       for(int i  =2; i*i <= n; i++)       {           if(n % i == 0)           {               al.add(i);               while(n%i == 0)               {                  n = n/i;               }           }       }             if(n!= 1)       {           al.add(n);       }       int l = al.size();       int [] ans = new int[l];       for(Integer p : al)       {           ans[j++] = p;       }             return ans;   }}
-1
Brijesh Yadav10 months ago
Brijesh Yadav
class Solution{public:
bool isprime(int n1){    for(int i=2;i*i<=n1;i++)    {        if(n1%i==0)        {            return false;        }    }    return true;}
vector<int>AllPrimeFactors(int n) {    // Code here
    vector<int>v;
    for(int i=2;i<=n;i++)    {        if(n%i==0)        {            if(isprime(i))            {                v.push_back(i);            }        }    }    return v;
}};
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.