Check if all Prime factors of number N are unique or not
Last Updated :
26 Nov, 2021
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:
- Find all the prime factors of the given number N using Sieve Of Eratosthenes.
- If the product of all the prime factors obtained is equaled to N then all prime factors are unique, so print YES.
- Else print NO.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
vector< int > primeFactors( int n)
{
int i, j;
vector< int > Prime;
if (n % 2 == 0) {
Prime.push_back(2);
}
while (n % 2 == 0) {
n = n / 2;
}
for (i = 3; i <= sqrt (n); i = i + 2) {
if (n % i == 0) {
Prime.push_back(i);
}
while (n % i == 0) {
n = n / i;
}
}
if (n > 2) {
Prime.push_back(n);
}
return Prime;
}
void checkDistinctPrime( int n)
{
vector< int > Prime = primeFactors(n);
int product = 1;
for ( auto i : Prime) {
product *= i;
}
if (product == n)
cout << "YES" ;
else
cout << "NO" ;
}
int main()
{
int N = 30;
checkDistinctPrime(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static Vector<Integer> primeFactors( int n)
{
int i, j;
Vector<Integer> Prime = new Vector<Integer>();
if (n % 2 == 0 ) {
Prime.add( 2 );
}
while (n % 2 == 0 ) {
n = n / 2 ;
}
for (i = 3 ; i <= Math.sqrt(n); i = i + 2 ) {
if (n % i == 0 ) {
Prime.add(i);
}
while (n % i == 0 ) {
n = n / i;
}
}
if (n > 2 ) {
Prime.add(n);
}
return Prime;
}
static void checkDistinctPrime( int n)
{
Vector<Integer> Prime = primeFactors(n);
int product = 1 ;
for ( int i : Prime) {
product *= i;
}
if (product == n)
System.out.print( "YES" );
else
System.out.print( "NO" );
}
public static void main(String[] args)
{
int N = 30 ;
checkDistinctPrime(N);
}
}
|
Python3
def primeFactors(n) :
Prime = [];
if (n % 2 = = 0 ) :
Prime.append( 2 );
while (n % 2 = = 0 ) :
n = n / / 2 ;
for i in range ( 3 , int (n * * ( 1 / 2 )), 2 ) :
if (n % i = = 0 ) :
Prime.append(i);
while (n % i = = 0 ) :
n = n / / i;
if (n > 2 ) :
Prime.append(n);
return Prime;
def checkDistinctPrime(n) :
Prime = primeFactors(n);
product = 1 ;
for i in Prime :
product * = i;
if (product = = n) :
print ( "YES" );
else :
print ( "NO" );
if __name__ = = "__main__" :
N = 30 ;
checkDistinctPrime(N);
|
C#
using System;
using System.Collections.Generic;
class GFG{
static List< int > primeFactors( int n)
{
int i;
List< int > Prime = new List< int >();
if (n % 2 == 0) {
Prime.Add(2);
}
while (n % 2 == 0) {
n = n / 2;
}
for (i = 3; i <= Math.Sqrt(n); i = i + 2) {
if (n % i == 0) {
Prime.Add(i);
}
while (n % i == 0) {
n = n / i;
}
}
if (n > 2) {
Prime.Add(n);
}
return Prime;
}
static void checkDistinctPrime( int n)
{
List< int > Prime = primeFactors(n);
int product = 1;
foreach ( int i in Prime) {
product *= i;
}
if (product == n)
Console.Write( "YES" );
else
Console.Write( "NO" );
}
public static void Main(String[] args)
{
int N = 30;
checkDistinctPrime(N);
}
}
|
Javascript
<script>
function primeFactors(n)
{
let i, j;
let Prime = new Array();
if (n % 2 == 0) {
Prime.push(2);
}
while (n % 2 == 0) {
n = n / 2;
}
for (i = 3; i <= Math.sqrt(n); i = i + 2) {
if (n % i == 0) {
Prime.push(i);
}
while (n % i == 0) {
n = n / i;
}
}
if (n > 2) {
Prime.push(n);
}
return Prime;
}
function checkDistinctPrime(n)
{
let Prime = primeFactors(n);
let product = 1;
for (let i of Prime) {
product *= i;
}
if (product == n)
document.write( "YES" );
else
document.write( "NO" );
}
let N = 30;
checkDistinctPrime(N);
</script>
|
Time Complexity: O(N*log(log N)), where N is the given number.
Auxiliary Space: O(sqrt(n))
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...