Check if a number has prime count of divisors - GeeksforGeeks

30 Apr, 2021
Given an integer N, the task is to check if the count of divisors of N is prime or not.
Examples: 
Input: N = 13 Output: Yes The divisor count is 2 (1 and 13) which is prime.
Input: N = 8 Output: No The divisors are 1, 2, 4 and 8.  
Approach: Please read this article to find the count of divisors of a number. So find the maximum value of i for every prime divisor p such that N % (pi) = 0. So the count of divisors gets multiplied by (i + 1). The count of divisors will be (i1 + 1) * (i2 + 1) * ... * (ik + 1). It can now be seen that there can only be one prime divisor for the maximum i and if N % pi = 0 then (i + 1) should be prime. The primality can be checked in sqrt(n) time and the prime factors can also be found in sqrt(n) time. So the overall time complexity will be O(sqrt(n)).
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function that returns true// if n is primebool Prime(int n){    // There is no prime    // less than 2    if (n < 2)        return false;     // Run a loop from 2 to sqrt(n)    for (int i = 2; i <= sqrt(n); i++)         // If there is any factor        if (n % i == 0)            return false;     return true;} // Function that returns true if n// has a prime count of divisorsbool primeCountDivisors(int n){    if (n < 2)        return false;     // Find the prime factors    for (int i = 2; i <= sqrt(n); i++)        if (n % i == 0) {             // Find the maximum value of i for every            // prime divisor p such that n % (p^i) == 0            long a = n, c = 0;            while (a % i == 0) {                a /= i;                c++;            }             // If c+1 is a prime number and a = 1            if (a == 1 && Prime(c + 1))                return true;             // The number cannot have two factors            // to have count of divisors prime            else                return false;        }     // Else the number is prime so    // it has only two divisors    return true;} // Driver codeint main(){    int n = 13;     if (primeCountDivisors(n))        cout << "Yes";    else        cout << "No";     return 0;}
// Java implementation of the approachclass GFG{         // Function that returns true    // if n is prime    static boolean Prime(int n)    {        // There is no prime        // less than 2        if (n < 2)            return false;             // Run a loop from 2 to sqrt(n)        for (int i = 2; i <= (int)Math.sqrt(n); i++)                 // If there is any factor            if (n % i == 0)                return false;        return true;    }         // Function that returns true if n    // has a prime count of divisors    static boolean primeCountDivisors(int n)    {        if (n < 2)            return false;             // Find the prime factors        for (int i = 2; i <= (int)Math.sqrt(n); i++)            if (n % i == 0)            {                     // Find the maximum value of i for every                // prime divisor p such that n % (p^i) == 0                long a = n, c = 0;                while (a % i == 0)                {                    a /= i;                    c++;                }                     // If c+1 is a prime number and a = 1                if (a == 1 && Prime((int)c + 1) == true)                    return true;                     // The number cannot have two factors                // to have count of divisors prime                else                    return false;            }             // Else the number is prime so        // it has only two divisors        return true;    }         // Driver code    public static void main (String[] args)    {        int n = 13;             if (primeCountDivisors(n))            System.out.println("Yes");        else            System.out.println("No");    }} // This code is contributed by AnkitRai01
# Python3 implementation of the approachfrom math import sqrt # Function that returns true# if n is primedef Prime(n) :     # There is no prime    # less than 2    if (n < 2) :        return False;     # Run a loop from 2 to sqrt(n)    for i in range(2, int(sqrt(n)) + 1) :         # If there is any factor        if (n % i == 0) :            return False;     return True; # Function that returns true if n# has a prime count of divisorsdef primeCountDivisors(n) :     if (n < 2) :        return False;     # Find the prime factors    for i in range(2, int(sqrt(n)) + 1) :        if (n % i == 0) :             # Find the maximum value of i for every            # prime divisor p such that n % (p^i) == 0            a = n; c = 0;            while (a % i == 0) :                a //= i;                c += 1;             # If c + 1 is a prime number and a = 1            if (a == 1 and Prime(c + 1)) :                return True;             # The number cannot have two factors            # to have count of divisors prime            else :                return False;             # Else the number is prime so    # it has only two divisors    return True; # Driver codeif __name__ == "__main__" :     n = 13;     if (primeCountDivisors(n)) :        print("Yes");    else :        print("No"); # This code is contributed by AnkitRai01
// C# implementation of the approachusing System; class GFG{         // Function that returns true    // if n is prime    static bool Prime(int n)    {                 // There is no prime        // less than 2        if (n < 2)            return false;             // Run a loop from 2 to sqrt(n)        for (int i = 2; i <= (int)Math.Sqrt(n); i++)                 // If there is any factor            if (n % i == 0)                return false;        return true;    }         // Function that returns true if n    // has a prime count of divisors    static bool primeCountDivisors(int n)    {        if (n < 2)            return false;             // Find the prime factors        for (int i = 2; i <= (int)Math.Sqrt(n); i++)            if (n % i == 0)            {                     // Find the maximum value of i for every                // prime divisor p such that n % (p^i) == 0                long a = n, c = 0;                while (a % i == 0)                {                    a /= i;                    c++;                }                     // If c+1 is a prime number and a = 1                if (a == 1 && Prime((int)c + 1) == true)                    return true;                     // The number cannot have two factors                // to have count of divisors prime                else                    return false;            }             // Else the number is prime so        // it has only two divisors        return true;    }         // Driver code    public static void Main()    {        int n = 13;             if (primeCountDivisors(n))            Console.WriteLine("Yes");        else            Console.WriteLine("No");    }} // This code is contributed by AnkitRai01
<script> // Javascript implementation of the approach // Function that returns true// if n is primefunction Prime(n){         // There is no prime    // less than 2    if (n < 2)        return false;     // Run a loop from 2 to sqrt(n)    for(var i = 2; i <= Math.sqrt(n); i++)         // If there is any factor        if (n % i == 0)            return false;     return true;} // Function that returns true if n// has a prime count of divisorsfunction primeCountDivisors( n){    if (n < 2)        return false;     // Find the prime factors    for(var i = 2; i <= Math.sqrt(n); i++)        if (n % i == 0)        {                         // Find the maximum value of i for every            // prime divisor p such that n % (p^i) == 0            var a = n, c = 0;                         while (a % i == 0)            {                a /= i;                c++;            }             // If c+1 is a prime number and a = 1            if (a == 1 && Prime(c + 1))                return true;             // The number cannot have two factors            // to have count of divisors prime            else                return false;        }     // Else the number is prime so    // it has only two divisors    return true;} // Driver rcoden = 13; if (primeCountDivisors(n))    document.write("Yes");else    document.write("No"); // This code is contributed by SoumikMondal </script>
Yes
 
ankthon
SoumikMondal
Numbers
prime-factor
Technical Scripter 2019
Competitive Programming
Technical Scripter
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Breadth First Traversal ( BFS ) on a 2D array
Maximum LCM among all pairs (i, j) from the given Array
Shortest path in a directed graph by Dijkstra’s algorithm
Pairs with same Manhattan and Euclidean distance
Multistage Graph (Shortest Path)
Runtime Errors
Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
Most important type of Algorithms
Sequence Alignment problem
Check whether two strings contain same characters in same order