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++
// 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; } |
Java
// 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. |
Python3
# 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 |
C#
// 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. |
PHP
<?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. ?> |
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.
Recommended Posts:
- Number which has the maximum number of distinct prime factors in the range M to N
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Distinct Prime Factors of Array Product
- Sort an array according to the increasing count of distinct Prime Factors
- Common prime factors of two numbers
- K-Primes (Numbers with k prime factors) in a range
- Count numbers from range whose prime factors are only 2 and 3
- Count common prime factors of two numbers
- Check whether a number has exactly three distinct factors or not
- Queries to find whether a number has exactly four distinct factors or not
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Numbers less than N which are product of exactly two distinct prime numbers
- Prime factors of a big number
- Exactly n distinct prime factor numbers from a to b
- Sum of Factors of a Number using Prime Factorization
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.



