Least prime factor of numbers till n

Last Updated : 26 Apr, 2026

Given a number n, print least prime factors of all numbers from 1 to n. The least prime factor of an integer n is the smallest prime number that divides the number.

Note: We need to print 1 for 1 and 0 for 0 even of 0  

Input: n = 6
Output: [0, 1, 2, 3, 2, 5, 2] 
Explanation:
Number : 0 1 2 3 4 5 6
Least Prime Factor: 0 1 2 3 2 5 2

Input: n = 11
Output: [0, 1, 2, 3, 2, 5, 2, 7, 2, 3, 2, 11]
Explanation:
Number: 0 1 2 3 4 5 6 7 8 9 10 11
Least Prime Factor: 0 1 2 3 2 5 2 7 2 3 2 11

Try It Yourself
redirect icon

Trying Every Number One by One - O(n * √n) Time and O(n) Space

  • For every number from 1 to n, find its least prime factor by checking divisibility starting from 2 up to its square root.
  • Since any number must have a factor ≤ √i, the first number that divides i is its smallest prime factor, and if no such divisor is found, then the number itself is prime.
  • 1 and 0 are handled separately by assigning it values as 1 and 0 respectively.
C++
#include <iostream>
#include <vector>
using namespace std;

// Function to compute Least Prime Factor (LPF)
vector<int> leastPrimeFactor(int n) {

    // Create a vector to store LPF for each number
    vector<int> lpf(n + 1);

    // Special cases
    lpf[0] = 0;
    lpf[1] = 1;

    // Loop through all numbers from 2 to n
    for (int i = 2; i <= n; i++) {

        // To check if we found a divisor
        bool found = false;

        // Try to find the smallest divisor of i
        for (int j = 2; j * j <= i; j++) {

            if (i % j == 0) {
                
                // smallest factor found
                lpf[i] = j;
                found = true;

                // Stop after finding the smallest factor
                break;
            }
        }

        // If no divisor was found, then i is prime
        if (!found) {
            lpf[i] = i;
        }
    }

    return lpf;
}

int main() {
    int n = 6;

    vector<int> result = leastPrimeFactor(n);

    for (int i = 0; i <= n; i++) {
        cout << result[i] << " ";
    }

    return 0;
}
Java
class GFG {

    // Function to compute Least Prime Factor (LPF) for every number from 0 to n
    static int[] leastPrimeFactor(int n) {
        
        // Array to store the least prime factor of each number
        int[] lpf = new int[n + 1];

        // Special cases
        lpf[0] = 0; 
        lpf[1] = 1; 

        // Loop through all numbers from 2 to n
        for (int i = 2; i <= n; i++) {
            // To check if a divisor is found
            boolean found = false; 

            for (int j = 2; j * j <= i; j++) {

                if (i % j == 0) {
                    // First divisor found 
                    lpf[i] = j;
                    found = true;
                    // Stop early since we only need the smallest factor
                    break;
                }
            }

            // If no divisor was found, then i is a prime number
            if (!found) {
                lpf[i] = i;
            }
        }

        return lpf;
    }

    public static void main(String[] args) {
        int n = 6;

        int[] result = leastPrimeFactor(n);

        for (int i = 0; i <= n; i++) {
            System.out.print(result[i] + " ");
        }
    }
}
Python
def leastPrimeFactor(n):
    # Create a list to store the least prime factor for each number
    lpf = [0] * (n + 1)

    # Special cases
    lpf[0] = 0  # 0 has no prime factor
    lpf[1] = 1  # By convention, LPF of 1 is set to 1

    # Loop through all numbers from 2 to n
    for i in range(2, n + 1):
        # To check if we found a divisor
        found = False 
        
        for j in range(2, int(i ** 0.5) + 1):

            if i % j == 0:
                # First divisor found 
                lpf[i] = j
                found = True
                # Stop once smallest factor is found
                break  
            
        # If no divisor is found, then i is prime
        if not found:
            lpf[i] = i

    return lpf


if __name__ == "__main__":  
    n = 6

    result = leastPrimeFactor(n)

    for x in result:
        print(x, end=" ")
C#
using System;

class GFG {

    // Function to compute Least Prime Factor (LPF) 
    static int[] leastPrimeFactor(int n) {

        // Create an array to store LPF for each number
        int[] lpf = new int[n + 1];

        // Special cases
        lpf[0] = 0;
        lpf[1] = 1;

        // Loop through all numbers from 2 to n
        for (int i = 2; i <= n; i++) {

            // To check if we found a divisor
            bool found = false;

            // Try to find the smallest divisor of i
            for (int j = 2; j * j <= i; j++) {

                if (i % j == 0) {
                    // smallest factor found
                    lpf[i] = j;
                    found = true;

                    // Stop after finding the smallest factor
                    break;
                }
            }

            // If no divisor was found, then i is prime
            if (!found) {
                lpf[i] = i;
            }
        }

        return lpf;
    }

    static void Main() {
        int n = 6;

        int[] result = leastPrimeFactor(n);

        for (int i = 0; i <= n; i++) {
            Console.Write(result[i] + " ");
        }
    }
}
JavaScript
function leastPrimeFactor(n) {
    
    // Create an array to store the least prime factor (LPF) for each number
    let lpf = new Array(n + 1).fill(0);

    // Special cases
    lpf[0] = 0; 
    lpf[1] = 1;

    // Loop through all numbers from 2 to n
    for (let i = 2; i <= n; i++) {
        
        // To check if we found a divisor
        let found = false;

        for (let j = 2; j * j <= i; j++) {
            if (i % j === 0) {
                
                // First divisor found
                lpf[i] = j;
                found = true;
                break;
            }
        }

        // If no divisor is found, then i is prime
        if (!found) {
            lpf[i] = i;
        }
    }

    return lpf;
}


// Driver Code 
let n = 6;
let result = leastPrimeFactor(n);

for (let i = 0; i <= n; i++) {
    process.stdout.write(result[i] + " ");
}

Output
0 1 2 3 2 5 2 

Using Sieve of Eratosthenes - O(n * log(n)) Time and O(n) Space

We can use a variation of sieve of Eratosthenes to solve the above problem. 

  • Initialize an array where each number is set to itself, then iterate from 2 to n. if a number i is still equal to its value, it is prime.
  • Since every multiple of i (2i, 3i, 4i...) is divisible by it, those numbers are not prime, so while marking these multiples we assign i as their least prime factor because i is the smallest prime dividing them.
  • Since we only assign when a number is not already marked, the first prime that reaches a number ensures it gets its correct smallest prime factor.
C++
#include <iostream>
#include <vector>
using namespace std;

// Function to compute Least Prime Factor (LPF) using Sieve approach
vector<int> leastPrimeFactor(int n) {

    // Assume all numbers are prime at the start
    vector<int> lpf(n + 1);
    for (int i = 0; i <= n; i++) {
        lpf[i] = i;
    }

    // Base cases
    lpf[0] = 0;
    lpf[1] = 1; 
    
    // Start from 2 and process each number
    for (int i = 2; i <= n; i++) {

        // i is prime
        if (lpf[i] == i) {

            // Update multiples of i
            // Start from 2*i because i itself is prime and already correct
            for (int j = i * 2; j <= n; j += i) {

                // If lpf[j] is still equal to j,
                // it means no smaller factor has been assigned yet
                if (lpf[j] == j) {

                    // Assign i as the smallest prime factor of j
                    lpf[j] = i;
                }
            }
        }
    }

    return lpf;
}

int main() {
    int n = 6;

    vector<int> result = leastPrimeFactor(n);

    for (int i = 0; i <= n; i++) {
        cout << result[i] << " ";
    }
}
Java
class GFG {

    // Function to compute Least Prime Factor (LPF) using Sieve approach
    static int[] leastPrimeFactor(int n) {

        // Create array and assume every number is prime initially
        int[] lpf = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            lpf[i] = i;
        }

        // Base cases
        lpf[0] = 0; 
        lpf[1] = 1;

        // Traverse from 2 to n
        for (int i = 2; i <= n; i++) {

            // If lpf[i] == i → i is still unchanged → so it is prime
            if (lpf[i] == i) {

                // Mark all multiples of i
                // Start from 2*i because i itself is already correct
                for (int j = i * 2; j <= n; j += i) {

                    // Update only if no smaller factor has been assigned yet
                    if (lpf[j] == j) {

                        // Assign i as the smallest prime factor
                        lpf[j] = i;
                    }
                }
            }
        }

        return lpf;
    }

    public static void main(String[] args) {
        int n = 6;

        int[] result = leastPrimeFactor(n);

        for (int i = 0; i <= n; i++) {
            System.out.print(result[i] + " ");
        }
    }
}
Python
def leastPrimeFactor(n):
    # Assume all numbers are prime at the start
    lpf = [0] * (n + 1)

    for i in range(n + 1):
        lpf[i] = i

    # Base cases
    lpf[0] = 0 
    lpf[1] = 1  

    # Traverse from 2 to n
    for i in range(2, n + 1):

        # it is prime
        if lpf[i] == i:

            # Mark all multiples of i
            # Start from 2*i because i itself is already correct
            for j in range(2 * i, n + 1, i):

                # Update only if no smaller factor has been assigned yet
                if lpf[j] == j:

                    # Assign i as the smallest prime factor
                    lpf[j] = i

    return lpf


if __name__ == "__main__": 
    n = 6
    
    result = leastPrimeFactor(n)

    for x in result:
        print(x, end=" ")
C#
using System;

class GFG
{
    // Function to compute Least Prime Factor (LPF) using Sieve approach
    static int[] LeastPrimeFactor(int n)
    {
        // Create an array and assume every number is prime initially
        int[] lpf = new int[n + 1];

        for (int i = 0; i <= n; i++)
        {
            lpf[i] = i;
        }

        // Base cases
        lpf[0] = 0; 
        lpf[1] = 1; 

        // Traverse from 2 to n
        for (int i = 2; i <= n; i++)
        {
            // it is prime
            if (lpf[i] == i)
            {
                // Mark all multiples of i
                // Start from 2*i because i itself is already correct
                for (int j = i * 2; j <= n; j += i)
                {
                    // Update only if no smaller factor has been assigned yet
                    if (lpf[j] == j)
                    {
                        // Assign i as the smallest prime factor
                        lpf[j] = i;
                    }
                }
            }
        }

        return lpf; 
    }

    static void Main()
    {
        int n = 6;

        int[] result = LeastPrimeFactor(n);

        for (int i = 0; i <= n; i++)
        {
            Console.Write(result[i] + " ");
        }
    }
}
JavaScript
function leastPrimeFactor(n) {
    // Create an array and assume every number is prime initially
    let lpf = new Array(n + 1);

    for (let i = 0; i <= n; i++) {
        lpf[i] = i;
    }

    // Base cases
    lpf[0] = 0; 
    lpf[1] = 1; 

    // Traverse from 2 to n
    for (let i = 2; i <= n; i++) {

        // it is prime
        if (lpf[i] === i) {

            // Mark all multiples of i
            // Start from 2*i because i itself is already correct
            for (let j = 2 * i; j <= n; j += i) {

                // Update only if no smaller factor has been assigned yet
                if (lpf[j] === j) {

                    // Assign i as the smallest prime factor
                    lpf[j] = i;
                }
            }
        }
    }

    return lpf;
}

// Driver Code 
let n = 6;

let result = leastPrimeFactor(n);

let output = "";
for (let i = 0; i <= n; i++) {
    output += result[i] + " ";
}
console.log(output.trim());

Output
0 1 2 3 2 5 2 
Comment