Least Prime Factor | Practice | GeeksforGeeks

Given a number N, find least prime factors for all numbers from 1 to N.  The least prime factor of an integer N is the smallest prime number that divides it.
Note : The least prime factor of all even numbers is 2. A prime number is its own least prime factor (as well as its own greatest prime factor).1 needs to be printed for 1.â€‹

Example 1:
Input: N = 6
Output: [1, 2, 3, 2, 5, 2] 
Explanation: least prime factor of 1 = 1,
least prime factor of 2 = 2,
least prime factor of 3 = 3,
least prime factor of 4 = 2,
least prime factor of 5 = 5,
least prime factor of 6 = 2.
So answer is[1, 2, 3, 2, 5, 2]. 


Example 2:
Input: N = 4
Output: [1, 2, 3, 2]
Explanation: least prime factor of 1 = 1,
least prime factor of 2 = 2,
least prime factor of 3 = 3,
least prime factor of 4 = 2.
So answer is[1, 2, 3, 2]. 


Your Task:  
You dont need to read input or print anything. Complete the function leastPrimeFactor() which takes N as input parameter and returns a list of integers containing all the least prime factor of each numbers from 1 to N.

Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N)

Constraints:
2<= n <=103
0
raunakgiri213 days ago
C++
 
class Solution { public:   bool isPrime(int n)   {       if(n<=1)return 0;       for(int i=2;i<sqrt(n);i++)       {           if(n%i==0)return 0;       }return 1;   }   int findLPF(int n)   {       for(int i=2;i*i<=n;i++)       {           if(isPrime(i) && n%i==0)return i;       }       return n;   }   vector<int> leastPrimeFactor(int n) {       // code here       vector<int> list;       list.push_back(1);       for(int i=1;i<=n;i++)       {           list.push_back(findLPF(i));       }       return list;   }};
0
moaslam8263 weeks ago
c++ seive algo
vector<int> leastPrimeFactor(int n) {
        // code here
        int X=1e3;
        int lp[X]={0};
        lp[1]=1;
        for(int i=2;i<X;i++){
            for(int j=i;j<X;j+=i){
                if(lp[j]==0){
                    lp[j]=i;
                }
            }
        }
        vector<int> ans;
        ans.push_back(1);
        for(int i=1;i<=n;i++){
            ans.push_back(lp[i]);
        }
        return ans;
    }
+1
mashhadihossain4 weeks ago
SIMPLEST JAVA SOLUTION (0.5/5.0 SEC)
class Solution{   public int[] leastPrimeFactor(int n)   {       int a[]=new int[n+1];       a[1]=1;       for(int i=2;i<=n;i++)       {           for(int j=2;j<=i;j++)           {               if(i%j==0)               {                   a[i]=j;                   break;               }           }       }       return a;   }}
0
chhitizgoyal2 months ago
Kindly correct me. (Java).
public static int prime(int num){       int ans = 1;       if(num%2==0){           ans = 2;       }else if(num%3==0){           ans = 3;       }       else if(num%5==0){           ans = 5;       }       else{           ans = num;       }       return ans;   }   public int[] leastPrimeFactor(int n)   {       int[] arr = new int[n+1];       for(int i=0; i<=n; i++){           arr[i] = prime(i);       }       return arr;   }
+1
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[] leastPrimeFactor(int n)
   {
       int[] arr = new int[n+1];
       arr[1]=1;
       for(int i=2;i<=n;i++)
       {
           if(i%2==0)
           {
               arr[i]=2;
               continue;
           }
           else
           {
               for(int j=3;j<=i;j++)
               {
                   if(i%j==0)
                   {
                       if(isprime(j))
                       {
                           arr[i]=j;
                           break;
                       }
                   }
               }
           }
       }
       
       return arr;
   }
+2
akshay014 months ago
no this is called a proper solution the best one execution time 0.0
vector<int> leastPrimeFactor(int n) {       // code here     vector<int> v;        v.push_back(0);        v.push_back(1);               for(int i=2;i<=n;i++)       {           for(int j=2;j<=i;j++)           {               if(i%j == 0)               {                   v.push_back(j);                   break;               }                          }       }              return v;   }
 
0
rakeshkumar696 months ago
worst compiler here 
its output should be 1 
but it is not showing that 
 
0
c1phani1simha6 months ago
class Solution { public:   vector<int> leastPrimeFactor(int n) {       vector<int> ans(n+1,0);       ans[1]=1;       for(int i=2;i<=n;i++)       {           if(ans[i]==0)           {               ans[i]=i;               for(int j=i*i;j<=n;j+=i)               {                   if(ans[j]==0)                   ans[j]=i;               }           }       }       return ans;          }};
0
Anjali Jaiswal10 months ago
Anjali Jaiswal
class Solution {  public:    vector<int> leastPrimeFactor(int n) {           vector<int>v;     // v.push_back(1);     int s[n+1];           for(int i=0;i<=n;i++)      {          s[i]=i;      }      for(int i=2;i<=sqrt(n);i++)      {          if(s[i]==i)          {              for(int j=i*i;j<=n;j+=i)              {                  if(s[j]==j)                  s[j]=i;              }          }      }      for(int i=0;i<=n;i++)      {          v.push_back(s[i]);      }            return v;    }};
0
Anjali Jaiswal10 months ago
Anjali Jaiswal
class Solution {  public:    vector<int> leastPrimeFactor(int n) {           vector<int>v;     // v.push_back(1);     int s[n+1];           for(int i=0;i<=n;i++)      {          s[i]=i;      }      for(int i=2;i<=sqrt(n);i++)      {          if(s[i]==i)          {              for(int j=i*i;j<=n;j+=i)              {                  if(s[j]==j)                  s[j]=i;              }          }      }      for(int i=0;i<=n;i++)      {          v.push_back(s[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.