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“.
Examples :
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:
C++
// C++ program to check whether number // has exactly three distinct factors // or not #include <bits/stdc++.h> using namespace std; // Utility function to check whether a // number is prime or not bool isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Function to check whether given number // has three distinct factors or not bool isThreeDisctFactors(long long n) { // Find square root of number int sq = (int)sqrt(n); // Check whether number is perfect // square or not if (1LL * sq * sq != n) return false; // If number is perfect square, check // whether square root is prime or // not return isPrime(sq) ? true : false; } // Driver program int main() { long long num = 9; if (isThreeDisctFactors(num)) cout << "Yes\n"; else cout << "No\n"; num = 15; if (isThreeDisctFactors(num)) cout << "Yes\n"; else cout << "No\n"; num = 12397923568441; if (isThreeDisctFactors(num)) cout << "Yes\n"; else cout << "No\n"; return 0; } |
Java
// Java program to check whether number // has exactly three distinct factors // or not public class GFG { // Utility function to check whether a // number is prime or not static boolean isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Function to check whether given number // has three distinct factors or not static boolean isThreeDisctFactors(long n) { // Find square root of number int sq = (int)Math.sqrt(n); // Check whether number is perfect // square or not if (1L * sq * sq != n) return false; // If number is perfect square, check // whether square root is prime or // not return isPrime(sq) ? true : false; } // Driver program public static void main(String[] args) { long num = 9; if (isThreeDisctFactors(num)) System.out.println("Yes"); else System.out.println("No"); num = 15; if (isThreeDisctFactors(num)) System.out.println("Yes"); else System.out.println("No"); num = 12397923568441L; if (isThreeDisctFactors(num)) System.out.println("Yes"); else System.out.println("No"); } } |
Python3
# Python 3 program to check whether number # has exactly three distinct factors # or not from math import sqrt # Utility function to check whether a # number is prime or not def isPrime(n): # Corner cases if (n <= 1): return False if (n <= 3): return True # This is checked so that we can skip # middle five numbers in below loop if (n % 2 == 0 or n % 3 == 0): return False k= int(sqrt(n))+1 for i in range(5,k,6): if (n % i == 0 or n % (i + 2) == 0): return False return True # Function to check whether given number # has three distinct factors or not def isThreeDisctFactors(n): # Find square root of number sq = int(sqrt(n)) # Check whether number is perfect # square or not if (1 * sq * sq != n): return False # If number is perfect square, check # whether square root is prime or # not if (isPrime(sq)): return True else: return False # Driver program if __name__ == '__main__': num = 9 if (isThreeDisctFactors(num)): print("Yes") else: print("No") num = 15 if (isThreeDisctFactors(num)): print("Yes") else: print("No") num = 12397923568441 if (isThreeDisctFactors(num)): print("Yes") else: print("No") # This code is contributd by # Surendra_Gangwar |
C#
// C# program to check whether number // has exactly three distinct factors // or not using System; public class GFG { // Utility function to check whether // a number is prime or not static bool isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can // skip middle five numbers in // below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Function to check whether given number // has three distinct factors or not static bool isThreeDisctFactors(long n) { // Find square root of number int sq = (int)Math.Sqrt(n); // Check whether number is perfect // square or not if (1LL * sq * sq != n) return false; // If number is perfect square, check // whether square root is prime or // not return isPrime(sq) ? true : false; } // Driver program static public void Main() { long num = 9; if (isThreeDisctFactors(num)) Console.WriteLine("Yes"); else Console.WriteLine("No"); num = 15; if (isThreeDisctFactors(num)) Console.WriteLine("Yes"); else Console.WriteLine("No"); num = 12397923568441; if (isThreeDisctFactors(num)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This Code is contributed by vt_m. |
PHP
<?php // PHP program to check whether // number has exactly three // distinct factors or not // Utility function to check // whether a number is prime // or not function isPrime($n) { // Corner cases if ($n <= 1) return false; if ($n <= 3) return true; // This is checked so that // we can skip middle five // numbers in below loop if ($n % 2 == 0 || $n % 3 == 0) return false; for ($i = 5; $i * $i <= $n; $i = $i + 6) if ($n % $i == 0 || $n % ($i + 2) == 0) return false; return true; } // Function to check // whether given number // has three distinct // factors or not function isThreeDisctFactors($n) { // Find square root of number $sq = sqrt($n); // Check whether number is // perfect square or not if ($sq * $sq != $n) return false; // If number is perfect square, // check whether square root is // prime or not return isPrime($sq) ? true : false; } // Driver Code $num = 9; if (isThreeDisctFactors($num)) echo "Yes\n"; else echo "No\n"; $num = 15; if (isThreeDisctFactors($num)) echo "Yes\n"; else echo "No\n"; $num = 12397923568441; if (isThreeDisctFactors($num)) echo "Yes\n"; else echo "No\n"; // This code is contributed by ak_t ?> |
Output :
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 contribute@geeksforgeeks.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.
Recommended Posts:
- 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
- Sum of nodes of Linked List whose contains values with exactly three factors
- 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
- Find number of factors of N when location of its two factors whose product is N is given
- Check whether count of distinct characters in a string is Prime or not
- 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
- Check whether given three numbers are adjacent primes
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- 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

