Given two integers L and R (L < = R). The task is to find all square free semiprimes in the range L to R (both inclusive).
Input : L = 1, R = 10
Output : 2
4, 6, 9, 10 are semi primes. But 6, 10 are square free semi primes.
Input : L = 10, R = 20
Output : 3
Semiprimes are numbers of the form where p and q are primes, not necessarily distinct. All semiprime has only 4 factors where p and q are the only two prime factors and .
Precompute all prime number upto . Find all combinations of two prime p and q such that is between L and R. Iterating through all combinations of prime would give a time complexity of . This solution, however, will not work for large L and R values.
Time Complexity: O(N^2)
Precompute all prime number up to . We can divide the problem of finding two primes p and q into a simpler form.
As we can say that . Similarly as we can say that .
Now the problem is reduced to finding the count of q such that for all p.
Here, we can use binary search for finding upper_bound of from list of prime numbers and subtract it from index of lower_bound of from list of prime numbers to find the count of all q in range to for the given p. Repeating the above step for all prime p will give the answer for given range L to R
Below is the implementation of the above approach :
Time Complexity: O(N*logN)
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Square Free Number
- Nth Square free number
- Minimum number of Square Free Divisors
- Range Sum Queries and Update with Square Root
- Probability of getting a perfect square when a random number is chosen in a given range
- Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Check if a number is perfect square without finding square root
- Count square and non-square numbers before n
- Cube Free Numbers smaller than n
- Range Queries to count elements lying in a given Range : MO's Algorithm
- Data Structures and Algorithms Online Courses : Free and Paid
- Find smallest perfect square number A such that N + A is also a perfect square number
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
Improved By : Akanksha_Rai