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++
#include <bits/stdc++.h>
using namespace std;
bool Prime( int n)
{
if (n < 2)
return false ;
for ( int i = 2; i <= sqrt (n); i++)
if (n % i == 0)
return false ;
return true ;
}
bool primeCountDivisors( int n)
{
if (n < 2)
return false ;
for ( int i = 2; i <= sqrt (n); i++)
if (n % i == 0) {
long a = n, c = 0;
while (a % i == 0) {
a /= i;
c++;
}
if (a == 1 && Prime(c + 1))
return true ;
else
return false ;
}
return true ;
}
int main()
{
int n = 13;
if (primeCountDivisors(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG
{
static boolean Prime( int n)
{
if (n < 2 )
return false ;
for ( int i = 2 ; i <= ( int )Math.sqrt(n); i++)
if (n % i == 0 )
return false ;
return true ;
}
static boolean primeCountDivisors( int n)
{
if (n < 2 )
return false ;
for ( int i = 2 ; i <= ( int )Math.sqrt(n); i++)
if (n % i == 0 )
{
long a = n, c = 0 ;
while (a % i == 0 )
{
a /= i;
c++;
}
if (a == 1 && Prime(( int )c + 1 ) == true )
return true ;
else
return false ;
}
return true ;
}
public static void main (String[] args)
{
int n = 13 ;
if (primeCountDivisors(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
from math import sqrt
def Prime(n) :
if (n < 2 ) :
return False ;
for i in range ( 2 , int (sqrt(n)) + 1 ) :
if (n % i = = 0 ) :
return False ;
return True ;
def primeCountDivisors(n) :
if (n < 2 ) :
return False ;
for i in range ( 2 , int (sqrt(n)) + 1 ) :
if (n % i = = 0 ) :
a = n; c = 0 ;
while (a % i = = 0 ) :
a / / = i;
c + = 1 ;
if (a = = 1 and Prime(c + 1 )) :
return True ;
else :
return False ;
return True ;
if __name__ = = "__main__" :
n = 13 ;
if (primeCountDivisors(n)) :
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG
{
static bool Prime( int n)
{
if (n < 2)
return false ;
for ( int i = 2; i <= ( int )Math.Sqrt(n); i++)
if (n % i == 0)
return false ;
return true ;
}
static bool primeCountDivisors( int n)
{
if (n < 2)
return false ;
for ( int i = 2; i <= ( int )Math.Sqrt(n); i++)
if (n % i == 0)
{
long a = n, c = 0;
while (a % i == 0)
{
a /= i;
c++;
}
if (a == 1 && Prime(( int )c + 1) == true )
return true ;
else
return false ;
}
return true ;
}
public static void Main()
{
int n = 13;
if (primeCountDivisors(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function Prime(n)
{
if (n < 2)
return false ;
for ( var i = 2; i <= Math.sqrt(n); i++)
if (n % i == 0)
return false ;
return true ;
}
function primeCountDivisors( n)
{
if (n < 2)
return false ;
for ( var i = 2; i <= Math.sqrt(n); i++)
if (n % i == 0)
{
var a = n, c = 0;
while (a % i == 0)
{
a /= i;
c++;
}
if (a == 1 && Prime(c + 1))
return true ;
else
return false ;
}
return true ;
}
n = 13;
if (primeCountDivisors(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(sqrt(n)), as we are using a loop to traverse sqrt (n) times. Where n is the integer given as input.
Auxiliary Space: O(1), as we are not using any extra space.
Approach 2: HashMap:
- The approach uses a map to count the frequency of each prime factor. A map is a data structure in C++ that stores key-value pairs. In this case, the key is the prime factor, and the value is the frequency of that prime factor. The map is used because it allows us to keep track of the frequency of each prime factor efficiently without needing to know the prime factors beforehand.
- The approach then uses a for loop to iterate over all the prime factors and count their frequency using the map. It also checks if there is any remaining factor greater than 1 after dividing by all the prime factors up to the square root of the number. If there is such a factor, it is also included in the map with a frequency of 1.
- After counting the frequency of each prime factor, the approach uses another for loop to calculate the total number of divisors of the number using the formula (f1+1) * (f2+1) * … * (fn+1), where f1, f2, …, fn are the frequencies of each prime factor in the factorization of the number. This formula works because for each prime factor, we have (frequency + 1) choices for how many times to include that prime factor in a divisor. We multiply all these choices together to get the total number of divisors.
- Finally, the approach checks if the total number of divisors is a prime number using another for loop that checks if the number is divisible by any number from 2 up to the square root of the number of divisors. If it is divisible by any of these numbers, then it is not a prime number and the function returns false. If none of these numbers divide the number of divisors, then it is a prime number, and the function returns true.
Here is the code of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool primeCountDivisors( int n)
{
if (n < 2)
return false ;
vector< int > primes;
for ( int i = 2; i <= sqrt (n); i++) {
while (n % i == 0) {
primes.push_back(i);
n /= i;
}
}
if (n > 1) {
primes.push_back(n);
}
map< int , int > freq;
for ( int prime : primes) {
freq[prime]++;
}
int numDivisors = 1;
for ( auto it : freq) {
numDivisors *= (it.second + 1);
}
if (numDivisors < 2)
return false ;
for ( int i = 2; i <= sqrt (numDivisors); i++) {
if (numDivisors % i == 0)
return false ;
}
return true ;
}
int main()
{
int n = 13;
if (primeCountDivisors(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
public class PrimeCountDivisors {
public static boolean primeCountDivisors( int n) {
if (n < 2 ) {
return false ;
}
Map<Integer, Integer> primes = new HashMap<>();
for ( int i = 2 ; i <= Math.sqrt(n); i++) {
while (n % i == 0 ) {
primes.put(i, primes.getOrDefault(i, 0 ) + 1 );
n /= i;
}
}
if (n > 1 ) {
primes.put(n, 1 );
}
int numDivisors = 1 ;
for (Map.Entry<Integer, Integer> entry : primes.entrySet()) {
numDivisors *= (entry.getValue() + 1 );
}
if (numDivisors < 2 ) {
return false ;
}
for ( int i = 2 ; i <= Math.sqrt(numDivisors); i++) {
if (numDivisors % i == 0 ) {
return false ;
}
}
return true ;
}
public static void main(String[] args) {
int n = 13 ;
if (primeCountDivisors(n)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
}
|
Python3
import math
def primeCountDivisors(n):
if n < 2 :
return False
primes = []
i = 2
while i < = math.sqrt(n):
while n % i = = 0 :
primes.append(i)
n / / = i
i + = 1
if n > 1 :
primes.append(n)
freq = {}
for prime in primes:
freq[prime] = freq.get(prime, 0 ) + 1
numDivisors = 1
for count in freq.values():
numDivisors * = (count + 1 )
if numDivisors < 2 :
return False
for i in range ( 2 , int (math.sqrt(numDivisors)) + 1 ):
if numDivisors % i = = 0 :
return False
return True
if __name__ = = "__main__" :
n = 13
if primeCountDivisors(n):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class Program {
static bool PrimeCountDivisors( int n)
{
if (n < 2)
return false ;
List< int > primes = new List< int >();
for ( int i = 2; i <= Math.Sqrt(n); i++) {
while (n % i == 0) {
primes.Add(i);
n /= i;
}
}
if (n > 1) {
primes.Add(n);
}
Dictionary< int , int > freq
= new Dictionary< int , int >();
foreach ( int prime in primes)
{
if (freq.ContainsKey(prime)) {
freq[prime]++;
}
else {
freq[prime] = 1;
}
}
int numDivisors = 1;
foreach (KeyValuePair< int , int > pair in freq)
{
numDivisors *= (pair.Value + 1);
}
if (numDivisors < 2)
return false ;
for ( int i = 2; i <= Math.Sqrt(numDivisors); i++) {
if (numDivisors % i == 0)
return false ;
}
return true ;
}
static void Main()
{
int n = 13;
if (PrimeCountDivisors(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
function primeCountDivisors(n) {
if (n < 2) return false ;
let primes = [];
for (let i = 2; i <= Math.sqrt(n); i++) {
while (n % i === 0) {
primes.push(i);
n /= i;
}
}
if (n > 1) {
primes.push(n);
}
let freq = {};
for (let prime of primes) {
freq[prime] = (freq[prime] || 0) + 1;
}
let numDivisors = 1;
for (let key in freq) {
numDivisors *= (freq[key] + 1);
}
if (numDivisors < 2) return false ;
for (let i = 2; i <= Math.sqrt(numDivisors); i++) {
if (numDivisors % i === 0) return false ;
}
return true ;
}
let n = 13;
if (primeCountDivisors(n)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Output:
Yes
Time Complexity: O(sqrt(N)), as we are using a loop to traverse sqrt (N) times. Where n is the integer given as input.
Auxiliary Space: O(LogN), as we are not using any extra space.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!