Given a positive integers ‘q’ and ‘n’. For each query ‘q’ find whether a number ‘n’ have exactly four distinct divisors or not. If the number have exactly four divisors then print ‘Yes’ else ‘No’.1 <= q, n <= 106
Input: 2 10 12 Output: Yes No Explanation: For 1st query, n = 10 has exactly four divisor i.e., 1, 2, 5, 10. For 2nd query, n = 12 has exactly six divisor i.e., 1, 2, 3, 4, 6, 12.
Simple approach is to count factors by generating all divisors of a number by using this approach, after that check whether the count of all factors are equal to ‘4’ or not. Time complexity of this approach is O(sqrt(n)).
Better approach is to use Number theory. For a number to be have four factors, it must satisfy the following conditions:-
- If number is a product of exactly two prime numbers(say p, q). Thus we can assure that it will have four factors i.e, 1, p, q, n.
- If a number is cube of a prime number (or cube root of the number is prime). For example, let’s say n = 8, cube root = 2 that means ‘8’ can be written as 2*2*2 hence four factors are:- 1, 2, 4 and 8.
We can use sieve of Eratosthenes such that we will pre-calculate all the prime factor from 1 to 106. Now we will mark all numbers which are the product of two prime number by using two ‘for loops’ i.e, mark[p * q] =true. Meanwhile we will also mark all numbers(cube root) by taking cube of number i.e, mark[p * p * p] = true.
After that we can easily answer each query in O(1) time.
Below is pseudo code, have a look for better understanding
Time complexity: O(1) for every query.
This article is contributed by Shubham Bansal. 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.
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.
- Check whether a number has exactly three distinct factors or not
- Maximum number of prime factors a number can have with exactly x factors
- Check if a number exists having exactly N factors and K prime factors
- Number which has the maximum number of distinct prime factors in the range M to N
- Find four factors of N with maximum product and sum equal to N | Set-2
- Find four factors of N with maximum product and sum equal to N
- Find four factors of N with maximum product and sum equal to N | Set 3
- Maximize the product of four factors of a Number
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Find number of factors of N when location of its two factors whose product is N is given
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- Check whether a number has consecutive 0's in the given base or not
- Queries on sum of odd number digit sums of all the factors of a number
- Check if there exists a number with X factors out of which exactly K are prime
- Find the row whose product has maximum count of prime factors
- Find the number of distinct pairs of vertices which have a distance of exactly k in a tree
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Count number of integers less than or equal to N which has exactly 9 divisors
- Find the number of ways to divide number into four parts such that a = c and b = d
- First element of every K sets having consecutive elements with exactly K prime factors less than N
Improved By : Mithun Kumar