Number of distinct prime factors of first n natural numbers

Given a number N, find the number of distinct prime factors for all numbers in the range [1, N].

Examples :

Input : N = 3
Output :  0 1 1
Number of distinct Prime Factors of 1 is 0
Number of distinct Prime Factors of 2 is 1
Number of distinct Prime Factors of 3 is 1

Input : 6
Output : 0 1 1 1 1 2
Number of distinct Prime Factors of 1 is 0
Number of distinct Prime Factors of 2 is 1
Number of distinct Prime Factors of 3 is 1
Number of distinct Prime Factors of 4 is 1
Number of distinct Prime Factors of 5 is 1
Number of distinct Prime Factors of 6 is 2
2, 3 and 5 are themselves prime. The only prime 
factor of 4 is 2.The two prime factors of 6 are 
2 and 3.

The idea is based on Sieve of Erathosthenes. Whenever we mark number as prime, we also increment count of prime factors in its multiples.

C++



filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find number of  distinct prime factors
// for all number in range [1, N]
#include <bits/stdc++.h>
using namespace std;
  
void printDistinctPFs(int n)
{
   // array to store the number of distinct primes
    long long factorCount[n + 1];
  
    // true if index 'i' is a prime
    bool prime[n + 1];
  
    // initializing the number of factors to 0 and
    for (int i = 0; i <= n; i++) {
        factorCount[i] = 0;
        prime[i] = true// Used in Sieve
    }
  
    for (int i = 2; i <= n; i++) {
   
        // condition works only when 'i' is prime, 
        // hence for factors of all prime number, 
        // the prime status is changed to false
        if (prime[i] == true) { 
              
            // Number is prime
            factorCount[i] = 1; 
              
            // number of factor of a prime number is 1
            for (int j = i * 2; j <= n; j += i) {
  
                // incrementing factorCount all 
                // the factors of i
                factorCount[j]++; 
  
                // and changing prime status to false
                prime[j] = false
            }
        }
    }
  
    // Printing result
    for (int i = 1; i <= n; i++) 
        cout << factorCount[i] << " ";
}
  
// Driver code
int main()
{
    int n = 20; // input
    printDistinctPFs(n); 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find number 
// of distinct prime factors 
// for all number in range [1, N]
import java.io.*;
  
class GFG 
{
static void printDistinctPFs(int n)
{
    // array to store the number
    // of distinct primes
    long factorCount[] = new long[n + 1];
  
    // true if index 'i' is a prime
    boolean prime[] = new boolean[n + 1];
  
    // initializing the number
    // of factors to 0 and
    for (int i = 0; i <= n; i++) 
    {
        factorCount[i] = 0;
        prime[i] = true; // Used in Sieve
    }
  
    for (int i = 2; i <= n; i++) 
    {
  
        // condition works only when  
        // 'i' is prime, hence for 
        // factors of all prime number, 
        // the prime status is changed to false
        if (prime[i] == true
        
              
            // Number is prime
            factorCount[i] = 1
              
            // number of factor of
            // a prime number is 1
            for (int j = i * 2; j <= n; j += i) 
            {
  
                // incrementing factorCount  
                // all the factors of i
                factorCount[j]++; 
  
                // and changing prime 
                // status to false
                prime[j] = false
            }
        }
    }
  
    // Printing result
    for (int i = 1; i <= n; i++) 
    System.out.print( factorCount[i] + " ");
}
  
// Driver Code
public static void main (String[] args)
{
    int n = 20; // input
    printDistinctPFs(n);
}
}
  
//This code is contributed by anuj_67.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find 
# number of distinct prime 
# factors for all number 
# in range [1, N]
def printDistinctPFs(n):
      
    # array to store the 
    # number of distinct primes
    factorCount = [];
  
    # true if index 
    # 'i' is a prime
    prime = [];
  
    # initializing the number
    # of factors to 0 and
    for i in range(n + 1): 
        factorCount.append(0);
        prime.append(1); 
        # Used in Sieve
  
    for i in range(2, n + 1): 
          
        # condition works only 
        # when 'i' is prime, 
        # hence for factors of 
        # all prime number, 
        # the prime status is 
        # changed to false
        if (prime[i] == 1): 
              
            # Number is prime
            factorCount[i] = 1
              
            # number of factor of 
            # a prime number is 1
            j = i * 2;
            while(j <= n): 
                  
                # incrementing factorCount 
                # all the factors of i
                factorCount[j] = factorCount[j] + 1
  
                # and changing prime 
                # status to false
                prime[j] = 0;
                j = j + i;
  
    # Printing result
    for i in range(1, n + 1): 
        print(factorCount[i] ,
                   end = " ");
  
# Driver code
n = 20;
  
# input
printDistinctPFs(n); 
  
# This code is contributed 
# by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find number 
// of distinct prime factors 
// for all number in range [1, N]
using System;
  
class GFG 
{
static void printDistinctPFs(int n)
{
    // array to store the number
    // of distinct primes
    long []factorCount = new long[n + 1];
  
    // true if index 'i' is a prime
    bool []prime = new bool[n + 1];
  
    // initializing the number
    // of factors to 0 and
    for (int i = 0; i <= n; i++) 
    {
        factorCount[i] = 0;
        prime[i] = true; // Used in Sieve
    }
  
    for (int i = 2; i <= n; i++) 
    {
  
        // condition works only 
        // when 'i' is prime, 
        // hence for factors of 
        // all prime number, the
        // prime status is changed
        // to false
        if (prime[i] == true
        
              
            // Number is prime
            factorCount[i] = 1; 
              
            // number of factor of
            // a prime number is 1
            for (int j = i * 2; j <= n; j += i) 
            {
  
                // incrementing factorCount 
                // all the factors of i
                factorCount[j]++; 
  
                // and changing prime 
                // status to false
                prime[j] = false
            }
        }
    }
  
    // Printing result
    for (int i = 1; i <= n; i++) 
    Console.Write(factorCount[i] + " ");
}
  
// Driver Code
public static void Main ()
{
    int n = 20; // input
    printDistinctPFs(n);
}
}
  
// This code is contributed by anuj_67.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find number 
// of distinct prime factors
// for all number in range [1, N]
function printDistinctPFs($n)
{
    // array to store the 
    // number of distinct primes
    $factorCount = array();
  
    // true if index 
    // 'i' is a prime
    $prime = array();
  
    // initializing the number
    // of factors to 0 and
    for ($i = 0; $i <= $n; $i++) 
    {
        $factorCount[$i] = 0;
        $prime[$i] = true; // Used in Sieve
    }
  
    for ($i = 2; $i <= $n; $i++) 
    {
  
        // condition works only 
        // when 'i' is prime, 
        // hence for factors of 
        // all prime number, 
        // the prime status is 
        // changed to false
        if ($prime[$i] == true) 
        
              
            // Number is prime
            $factorCount[$i] = 1; 
              
            // number of factor of 
            // a prime number is 1
            for ($j = $i * 2; 
                 $j <= $n; $j += $i
            {
  
                // incrementing factorCount 
                // all the factors of i
                $factorCount[$j]++; 
  
                // and changing prime 
                // status to false
                $prime[$j] = false; 
            }
        }
    }
  
    // Printing result
    for ($i = 1; $i <= $n; $i++) 
        echo $factorCount[$i] , " ";
}
  
// Driver code
$n = 20; // input
printDistinctPFs($n); 
  
// This code is contributed 
// by anuj_67.
?>

chevron_right


Output :

0 1 1 1 1 2 1 1 1 2 1 2 1 2 2 1 1 2 1 2

This is the most efficient way to calculate the number of prime factors for numbers in [1, N]. Here the data type of n, factorCount can be changed to solve problems with huge constraints.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : vt_m, Mithun Kumar