📜  检查数字N的所有素数因子是否唯一

📅  最后修改于: 2021-04-22 08:48:16             🧑  作者: Mango

给定数字N。任务是检查给定数N是否具有唯一素数。如果是,则打印“是”,否则打印“否”

例子:

方法:

  1. 使用筛网筛查找到给定数N的所有素数。
  2. 如果获得的所有素数的乘积等于N,则所有素数都是唯一的,因此请打印YES。
  3. 其他打印编号

下面是上述方法的实现:

CPP
// C++ program for the above approach
#include 
using namespace std;
  
// Function that returns the all the
// distinct prime factors in a vector
vector primeFactors(int n)
{
    int i, j;
    vector Prime;
  
    // If n is divisible by 2
    if (n % 2 == 0) {
        Prime.push_back(2);
    }
  
    // Divide n till all factors of 2
    while (n % 2 == 0) {
        n = n / 2;
    }
  
    // Check for the prime numbers other
    // than 2
    for (i = 3; i <= sqrt(n); i = i + 2) {
  
        // Store i in Prime[] i is a
        // factor of n
        if (n % i == 0) {
            Prime.push_back(i);
        }
  
        // Divide n till all factors of i
        while (n % i == 0) {
            n = n / i;
        }
    }
  
    // If n is greter than 2, then n is
    // prime number after n divided by
    // all factors
    if (n > 2) {
        Prime.push_back(n);
    }
  
    // Returns the vector Prime
    return Prime;
}
  
// Function that check whether N is the
// product of distinct prime factors
// or not
void checkDistinctPrime(int n)
{
    // Returns the vector to store
    // all the distinct prime factors
    vector Prime = primeFactors(n);
  
    // To find the product of all
    // distinct prime factors
    int product = 1;
  
    // Find the product
    for (auto i : Prime) {
        product *= i;
    }
  
    // If product is equals to N,
    // print YES, else print NO
    if (product == n)
        cout << "YES";
    else
        cout << "NO";
}
  
// Driver Code
int main()
{
    int N = 30;
    checkDistinctPrime(N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
  
class GFG{
   
// Function that returns the all the
// distinct prime factors in a vector
static Vector primeFactors(int n)
{
    int i, j;
    Vector Prime = new Vector();
   
    // If n is divisible by 2
    if (n % 2 == 0) {
        Prime.add(2);
    }
   
    // Divide n till all factors of 2
    while (n % 2 == 0) {
        n = n / 2;
    }
   
    // Check for the prime numbers other
    // than 2
    for (i = 3; i <= Math.sqrt(n); i = i + 2) {
   
        // Store i in Prime[] i is a
        // factor of n
        if (n % i == 0) {
            Prime.add(i);
        }
   
        // Divide n till all factors of i
        while (n % i == 0) {
            n = n / i;
        }
    }
   
    // If n is greter than 2, then n is
    // prime number after n divided by
    // all factors
    if (n > 2) {
        Prime.add(n);
    }
   
    // Returns the vector Prime
    return Prime;
}
   
// Function that check whether N is the
// product of distinct prime factors
// or not
static void checkDistinctPrime(int n)
{
    // Returns the vector to store
    // all the distinct prime factors
    Vector Prime = primeFactors(n);
   
    // To find the product of all
    // distinct prime factors
    int product = 1;
   
    // Find the product
    for (int i : Prime) {
        product *= i;
    }
   
    // If product is equals to N,
    // print YES, else print NO
    if (product == n)
        System.out.print("YES");
    else
        System.out.print("NO");
}
   
// Driver Code
public static void main(String[] args)
{
    int N = 30;
    checkDistinctPrime(N);
}
}
  
// This code is contributed by sapnasingh4991


Python3
# Python3 program for the above approach 
  
# Function that returns the all the 
# distinct prime factors in a vector 
def primeFactors(n) : 
  
    Prime = []; 
  
    # If n is divisible by 2 
    if (n % 2 == 0) :
        Prime.append(2); 
  
    # Divide n till all factors of 2 
    while (n % 2 == 0) :
        n = n // 2; 
      
    # Check for the prime numbers other 
    # than 2 
    for i in range(3, int(n ** (1/2)),2) :
  
        # Store i in Prime[] i is a 
        # factor of n 
        if (n % i == 0) :
            Prime.append(i); 
          
        # Divide n till all factors of i 
        while (n % i == 0) :
            n = n // i; 
  
    # If n is greter than 2, then n is 
    # prime number after n divided by 
    # all factors 
    if (n > 2) :
        Prime.append(n); 
  
    # Returns the vector Prime 
    return Prime; 
  
# Function that check whether N is the 
# product of distinct prime factors 
# or not 
def checkDistinctPrime(n) : 
  
    # Returns the vector to store 
    # all the distinct prime factors 
    Prime = primeFactors(n);
      
    # To find the product of all
    # distinct prime factors
    product = 1; 
  
    # Find the product 
    for i in Prime :
        product *= i; 
  
    # If product is equals to N, 
    # print YES, else print NO 
    if (product == n) :
        print("YES"); 
    else :
        print("NO"); 
  
# Driver Code 
if __name__ == "__main__" : 
  
    N = 30; 
    checkDistinctPrime(N); 
  
# This code is contributed by Yash_R


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
  
class GFG{
    
// Function that returns the all the
// distinct prime factors in a vector
static List primeFactors(int n)
{
    int i;
    List Prime = new List();
    
    // If n is divisible by 2
    if (n % 2 == 0) {
        Prime.Add(2);
    }
    
    // Divide n till all factors of 2
    while (n % 2 == 0) {
        n = n / 2;
    }
    
    // Check for the prime numbers other
    // than 2
    for (i = 3; i <= Math.Sqrt(n); i = i + 2) {
    
        // Store i in Prime[] i is a
        // factor of n
        if (n % i == 0) {
            Prime.Add(i);
        }
    
        // Divide n till all factors of i
        while (n % i == 0) {
            n = n / i;
        }
    }
    
    // If n is greter than 2, then n is
    // prime number after n divided by
    // all factors
    if (n > 2) {
        Prime.Add(n);
    }
    
    // Returns the vector Prime
    return Prime;
}
    
// Function that check whether N is the
// product of distinct prime factors
// or not
static void checkDistinctPrime(int n)
{
    // Returns the vector to store
    // all the distinct prime factors
    List Prime = primeFactors(n);
    
    // To find the product of all
    // distinct prime factors
    int product = 1;
    
    // Find the product
    foreach (int i in Prime) {
        product *= i;
    }
    
    // If product is equals to N,
    // print YES, else print NO
    if (product == n)
        Console.Write("YES");
    else
        Console.Write("NO");
}
    
// Driver Code
public static void Main(String[] args)
{
    int N = 30;
    checkDistinctPrime(N);
}
}
  
// This code is contributed by sapnasingh4991


输出:
YES

时间复杂度: O(N * log(log N)),其中N是给定的数字。