Check if all Prime factors of number N are unique or not

Given a number N. The task is to check whether the given number N has unique prime factors or not. If yes then print YES else print NO.

Examples:

Input: N = 30
Output: YES
Explanation:
N = 30 = 2*3*5
As all the prime factors of 30 are unique.



Input: N = 100
Output: NO
Explanation:
N = 100 = 2*2*5*5
As all the prime factors of 100 are not unique because 2 and 5 are repeated twice.

Approach:

  1. Find all the prime factors of the given number N using Sieve Of Eratosthenes.
  2. If the product of all the prime factors obtained is equals to N then all prime factors are unique, so print YES.
  3. Else print NO.

Below is the implementation of the above approach:

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that returns the all the
// distinct prime factors in a vector
vector<int> primeFactors(int n)
{
    int i, j;
    vector<int> 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<int> 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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<Integer> primeFactors(int n)
{
    int i, j;
    Vector<Integer> Prime = new Vector<Integer>();
   
    // 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<Integer> 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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<int> primeFactors(int n)
{
    int i;
    List<int> Prime = new List<int>();
    
    // 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<int> 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

chevron_right


Output:

YES

Time Complexity: O(N*log(log N)), where N is the given number.

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : sapnasingh4991, Yash_R