Queries to find whether a number has exactly four distinct factors or not
Given a positive integers ‘q’ and ‘n’. For each query ‘q’ find whether a number ‘n’ have exactly four distinct divisors or not. If the number have exactly four divisors then print ‘Yes’ else ‘No’.1 <= q, n <= 106
Input:
2
10
12
Output:
Yes
No
Explanation:
For 1st query, n = 10 has exactly four divisor i.e., 1, 2, 5, 10.
For 2nd query, n = 12 has exactly six divisor i.e., 1, 2, 3, 4, 6, 12.
Simple approach is to count factors by generating all divisors of a number by using this approach, after that check whether the count of all factors are equal to ‘4’ or not. Time complexity of this approach is O(sqrt(n)).
Better approach is to use Number theory. For a number to be have four factors, it must satisfy the following conditions:-
- If number is a product of exactly two prime numbers(say p, q). Thus we can assure that it will have four factors i.e, 1, p, q, n.
- If a number is cube of a prime number (or cube root of the number is prime). For example, let’s say n = 8, cube root = 2 that means ‘8’ can be written as 2*2*2 hence four factors are:- 1, 2, 4 and 8.
We can use sieve of Eratosthenes such that we will pre-calculate all the prime factor from 1 to 106. Now we will mark all numbers which are the product of two prime number by using two ‘for loops’ i.e, mark[p * q] =true. Meanwhile we will also mark all numbers(cube root) by taking cube of number i.e, mark[p * p * p] = true.
After that we can easily answer each query in O(1) time.
Below is pseudo code, have a look for better understanding
C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
bool fourDiv[N + 1];
void fourDistinctFactors()
{
bool primeAll[N + 1];
memset (primeAll, true , sizeof (primeAll));
for ( int p = 2; p * p <= N; p++) {
if (primeAll[p] == true ) {
for ( int i = p * 2; i <= N; i += p)
primeAll[i] = false ;
}
}
vector< int > prime;
for ( int p = 2; p <= N; p++)
if (primeAll[p])
prime.push_back(p);
memset (fourDiv, false , sizeof (fourDiv));
for ( int i = 0; i < prime.size(); ++i) {
int p = prime[i];
if (1LL * p * p * p <= N)
fourDiv[p * p * p] = true ;
for ( int j = i + 1; j < prime.size(); ++j) {
int q = prime[j];
if (1LL * p * q > N)
break ;
fourDiv[p * q] = true ;
}
}
}
int main()
{
fourDistinctFactors();
int num = 10;
if (fourDiv[num])
cout << "Yes\n" ;
else
cout << "No\n" ;
num = 12;
if (fourDiv[num])
cout << "Yes\n" ;
else
cout << "No\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int N = ( int )1E6;
static boolean [] fourDiv= new boolean [N + 1 ];
static void fourDistinctFactors()
{
boolean [] primeAll= new boolean [N + 1 ];
for ( int p = 2 ; p * p <= N; p++) {
if (primeAll[p] == false ) {
for ( int i = p * 2 ; i <= N; i += p)
primeAll[i] = true ;
}
}
ArrayList<Integer> prime= new ArrayList<Integer>();
for ( int p = 2 ; p <= N; p++)
if (!primeAll[p])
prime.add(p);
for ( int i = 0 ; i < prime.size(); ++i) {
int p = prime.get(i);
if (1L * p * p * p <= N)
fourDiv[p * p * p] = true ;
for ( int j = i + 1 ; j < prime.size(); ++j) {
int q = prime.get(j);
if (1L * p * q > N)
break ;
fourDiv[p * q] = true ;
}
}
}
public static void main(String[] args)
{
fourDistinctFactors();
int num = 10 ;
if (fourDiv[num])
System.out.println( "Yes" );
else
System.out.println( "No" );
num = 12 ;
if (fourDiv[num])
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
N = 1000001 ;
fourDiv = [ False ] * (N + 1 );
def fourDistinctFactors():
primeAll = [ True ] * (N + 1 );
p = 2 ;
while (p * p < = N):
if (primeAll[p] = = True ):
i = p * 2 ;
while (i < = N):
primeAll[i] = False ;
i + = p;
p + = 1 ;
prime = [];
for p in range ( 2 , N + 1 ):
if (primeAll[p]):
prime.append(p);
for i in range ( len (prime)):
p = prime[i];
if ( 1 * p * p * p < = N):
fourDiv[p * p * p] = True ;
for j in range (i + 1 , len (prime)):
q = prime[j];
if ( 1 * p * q > N):
break ;
fourDiv[p * q] = True ;
fourDistinctFactors();
num = 10 ;
if (fourDiv[num]):
print ( "Yes" );
else :
print ( "No" );
num = 12 ;
if (fourDiv[num]):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
using System.Collections;
class GFG
{
static int N = ( int )1E6;
static bool [] fourDiv = new bool [N + 1];
static void fourDistinctFactors()
{
bool [] primeAll = new bool [N + 1];
for ( int p = 2; p * p <= N; p++)
{
if (primeAll[p] == false )
{
for ( int i = p * 2; i <= N; i += p)
primeAll[i] = true ;
}
}
ArrayList prime = new ArrayList();
for ( int p = 2; p <= N; p++)
if (!primeAll[p])
prime.Add(p);
for ( int i = 0; i < prime.Count; ++i)
{
int p = ( int )prime[i];
if (1L * p * p * p <= N)
fourDiv[p * p * p] = true ;
for ( int j = i + 1; j < prime.Count; ++j)
{
int q = ( int )prime[j];
if (1L * p * q > N)
break ;
fourDiv[p * q] = true ;
}
}
}
public static void Main()
{
fourDistinctFactors();
int num = 10;
if (fourDiv[num])
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
num = 12;
if (fourDiv[num])
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
$N = 1000001;
$fourDiv = array_fill (0,
$N + 1, false);
function fourDistinctFactors()
{
global $N ;
global $fourDiv ;
$primeAll = array_fill (0, $N + 1, true);
for ( $p = 2;
$p * $p <= $N ; $p ++)
{
if ( $primeAll [ $p ] == true)
{
for ( $i = $p * 2;
$i <= $N ; $i += $p )
$primeAll [ $i ] = false;
}
}
$prime ;
$x = 0;
for ( $p = 2; $p <= $N ; $p ++)
if ( $primeAll [ $p ])
$prime [ $x ++] = $p ;
for ( $i = 0; $i < $x ; ++ $i )
{
$p = $prime [ $i ];
if (1 * $p * $p * $p <= $N )
$fourDiv [ $p * $p * $p ] = true;
for ( $j = $i + 1;
$j < $x ; ++ $j )
{
$q = $prime [ $j ];
if (1 * $p * $q > $N )
break ;
$fourDiv [ $p * $q ] = true;
}
}
}
fourDistinctFactors();
$num = 10;
if ( $fourDiv [ $num ])
echo "Yes\n" ;
else
echo "No\n" ;
$num = 12;
if ( $fourDiv [ $num ])
echo "Yes\n" ;
else
echo "No\n" ;
?>
|
Javascript
<script>
var N = 1000000;
var fourDiv = Array(N+1).fill( false );
function fourDistinctFactors()
{
var primeAll = Array(N+1).fill( false );
for ( var p = 2; p * p <= N; p++)
{
if (primeAll[p] == false )
{
for ( var i = p * 2; i <= N; i += p)
primeAll[i] = true ;
}
}
var prime = [];
for ( var p = 2; p <= N; p++)
if (!primeAll[p])
prime.push(p);
for ( var i = 0; i < prime.length; ++i)
{
var p = prime[i];
if (p * p * p <= N)
fourDiv[p * p * p] = true ;
for ( var j = i + 1; j < prime.length; ++j)
{
var q = prime[j];
if (p * q > N)
break ;
fourDiv[p * q] = true ;
}
}
}
fourDistinctFactors();
var num = 10;
if (fourDiv[num])
document.write( "Yes<br>" );
else
document.write( "No<br>" );
num = 12;
if (fourDiv[num])
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Output:
Yes
No
Time Complexity: O(n log(log n))
Auxiliary Space: O(n)
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...