You are given two numbers a and b (1 <= a,b <= 10^8 ) and n. The task is to find all numbers between a and b inclusively having exactly n distinct prime factors. The solution should be designed in a way that it efficiently handles multiple queries for different values of a and b like in Competitive Programming.
Input : a = 1, b = 10, n = 2 Output : 2 // Only 6 = 2*3 and 10 = 2*5 have exactly two // distinct prime factors Input : a = 1, b = 100, n = 3 Output: 8 // only 30 = 2*3*5, 42 = 2*3*7, 60 = 2*2*3*5, 66 = 2*3*11, // 70 = 2*5*7, 78 = 2*3*13, 84 = 2*2*3*7 and 90 = 2*3*3*5 // have exactly three distinct prime factors
This problem is basically application of segmented sieve. As we know that all prime factors of a number are always less than or equal to square root of number i.e; sqrt(n). So we generate all prime numbers less than or equals to 10^8 and store them in an array. Now using this segmented sieve we check each number from a to b to have exactly n prime factors.
If you have another approach to solve this problem then please share in comments.
This article is contributed by Shashank Mishra ( Gullu ). 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of distinct power of prime factor of N
- Least prime factor of numbers till n
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Numbers less than N which are product of exactly two distinct prime numbers
- Find two distinct prime numbers with given product
- Number of distinct prime factors of first n natural numbers
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count of distinct sums that can be obtained by adding prime numbers from given arrays
- Prime Factor
- k-th prime factor of a given number
- N-th prime factor of a given number
- Queries on the sum of prime factor counts in a range
- Find sum of a number and its maximum prime factor
- Nearest element with at-least one common prime factor
- Sum of largest prime factor of each number less than equal to n
- Find largest prime factor of a number
- Sum of Maximum and Minimum prime factor of every number in the Array
- Count of subarrays whose products don't have any repeating prime factor
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array