Given a number N, find the number of distinct prime factors for all numbers in the range [1, N].
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.
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.
- 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
- Count numbers from range whose prime factors are only 2 and 3
- K-Primes (Numbers with k prime factors) in a range
- 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 email@example.com. 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.