Given a positive integer n(1 <= n <= 1018). Check whether a number has exactly three distinct factors or not. Print “Yes” if it has otherwise “No“.
Input : 9 Output: Yes Explanation Number 9 has exactly three factors: 1, 3, 9, hence answer is 'Yes' Input : 10 Output : No
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 ‘3’ or not. Time complexity of this approach is O(sqrt(n)).
Better approach is to use Number theory. According to property of perfect square, “Every perfect square(x2) always have only odd numbers of factors“.
If the square root of given number(say x2) is prime(after conforming that number is perfect square) then it must have exactly three distinct factors i.e.,
- A number 1 of course.
- Square root of a number i.e., x(prime number).
- Number itself i.e., x2.
Below is the implementation of above approach:
Yes No No
Time complexity : O(n1/4)
Auxiliary space: O(1)
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 firstname.lastname@example.org. 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.
- Queries to find whether a number has exactly four distinct factors or not
- Check if a number exists having exactly N factors and K prime factors
- Maximum number of prime factors a number can have with exactly x factors
- Number which has the maximum number of distinct prime factors in the range M to N
- Check whether a number has consecutive 0's in the given base or not
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Check whether triangle is valid or not if three points are given
- Check if there exists a number with X factors out of which exactly K are prime
- Check whether count of odd and even factors of a number are equal
- Check whether count of distinct characters in a string is Prime or not
- Find number of factors of N when location of its two factors whose product is N is given
- Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Number of quadruples where the first three terms are in AP and last three terms are in GP
- Count number of integers less than or equal to N which has exactly 9 divisors
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- Check whether given three numbers are adjacent primes
- Check if all Prime factors of number N are unique or not
- First element of every K sets having consecutive elements with exactly K prime factors less than N
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C