给定整数N ,任务是检查N的因数计数是否为质数。
例子:
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.
方法:请阅读本文以找到数字的除数的数量。因此,求出每个质数p的i的最大值,使得N%(p i )= 0 。因此,除数的数量乘以(i + 1) 。除数的数量为(i 1 +1)*(i 2 +1)*…*(i k +1)。
现在可以看出,最大i只能有一个素数除数,如果N%p i = 0,则(i + 1)应该是素数。素数可以在sqrt(n)时间中检查,素数因子也可以在sqrt(n)时间中找到。因此,总体时间复杂度将为O(sqrt(n)) 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function that returns true
// if n is prime
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 <= 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
bool 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 code
int main()
{
int n = 13;
if (primeCountDivisors(n))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java implementation of the approach
class 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
# Python3 implementation of the approach
from math import sqrt
# Function that returns true
# if n is prime
def 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 divisors
def 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 code
if __name__ == "__main__" :
n = 13;
if (primeCountDivisors(n)) :
print("Yes");
else :
print("No");
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using 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
Javascript
输出:
Yes
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。