Check whether a number has exactly three distinct factors or not
Last Updated :
21 Aug, 2023
Given a positive integer n(1 <= n <= 1018). Check whether a number has exactly three distinct factors or not. Print “Yes” if it has otherwise “No“.
Examples :
Input : 9
Output: Yes
Explanation
Number 9 has exactly three factors:
1, 3, 9, hence answer is 'Yes'
Input : 10
Output : No
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 ‘3’ or not. Time complexity of this approach is O(sqrt(n)).
Better approach is to use Number theory. According to property of perfect square, “Every perfect square(x2) always have only odd numbers of factors“.
If the square root of given number(say x2) is prime(after conforming that number is perfect square) then it must have exactly three distinct factors i.e.,
- A number 1 of course.
- Square root of a number i.e., x(prime number).
- Number itself i.e., x2.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
bool isThreeDisctFactors( long long n)
{
int sq = ( int ) sqrt (n);
if (1LL * sq * sq != n)
return false ;
return isPrime(sq) ? true : false ;
}
int main()
{
long long num = 9;
if (isThreeDisctFactors(num))
cout << "Yes\n" ;
else
cout << "No\n" ;
num = 15;
if (isThreeDisctFactors(num))
cout << "Yes\n" ;
else
cout << "No\n" ;
num = 12397923568441;
if (isThreeDisctFactors(num))
cout << "Yes\n" ;
else
cout << "No\n" ;
return 0;
}
|
Java
public class GFG {
static boolean isPrime( int n)
{
if (n <= 1 )
return false ;
if (n <= 3 )
return true ;
if (n % 2 == 0 || n % 3 == 0 )
return false ;
for ( int i = 5 ; i * i <= n; i = i + 6 )
if (n % i == 0 || n % (i + 2 ) == 0 )
return false ;
return true ;
}
static boolean isThreeDisctFactors( long n)
{
int sq = ( int )Math.sqrt(n);
if (1L * sq * sq != n)
return false ;
return isPrime(sq) ? true : false ;
}
public static void main(String[] args) {
long num = 9 ;
if (isThreeDisctFactors(num))
System.out.println( "Yes" );
else
System.out.println( "No" );
num = 15 ;
if (isThreeDisctFactors(num))
System.out.println( "Yes" );
else
System.out.println( "No" );
num = 12397923568441L;
if (isThreeDisctFactors(num))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
from math import sqrt
def isPrime(n):
if (n < = 1 ):
return False
if (n < = 3 ):
return True
if (n % 2 = = 0 or n % 3 = = 0 ):
return False
k = int (sqrt(n)) + 1
for i in range ( 5 ,k, 6 ):
if (n % i = = 0 or n % (i + 2 ) = = 0 ):
return False
return True
def isThreeDisctFactors(n):
sq = int (sqrt(n))
if ( 1 * sq * sq ! = n):
return False
if (isPrime(sq)):
return True
else :
return False
if __name__ = = '__main__' :
num = 9
if (isThreeDisctFactors(num)):
print ( "Yes" )
else :
print ( "No" )
num = 15
if (isThreeDisctFactors(num)):
print ( "Yes" )
else :
print ( "No" )
num = 12397923568441
if (isThreeDisctFactors(num)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class GFG {
static bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
static bool isThreeDisctFactors( long n)
{
int sq = ( int )Math.Sqrt(n);
if (1LL * sq * sq != n)
return false ;
return isPrime(sq) ? true : false ;
}
static public void Main()
{
long num = 9;
if (isThreeDisctFactors(num))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
num = 15;
if (isThreeDisctFactors(num))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
num = 12397923568441;
if (isThreeDisctFactors(num))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isPrime(n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for (let i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
function isThreeDisctFactors(n)
{
let sq = parseInt(Math.sqrt(n));
if (sq * sq != n)
return false ;
return isPrime(sq) ? true : false ;
}
let num = 9;
if (isThreeDisctFactors(num))
document.write( "Yes<br>" );
else
document.write( "No<br>" );
num = 15;
if (isThreeDisctFactors(num))
document.write( "Yes<br>" );
else
document.write( "No<br>" );
num = 12397923568441;
if (isThreeDisctFactors(num))
document.write( "Yes<br>" );
else
document.write( "No<br>" );
</script>
|
PHP
<?php
function isPrime( $n )
{
if ( $n <= 1)
return false;
if ( $n <= 3)
return true;
if ( $n % 2 == 0 || $n % 3 == 0)
return false;
for ( $i = 5; $i * $i <= $n ;
$i = $i + 6)
if ( $n % $i == 0 ||
$n % ( $i + 2) == 0)
return false;
return true;
}
function isThreeDisctFactors( $n )
{
$sq = sqrt( $n );
if ( $sq * $sq != $n )
return false;
return isPrime( $sq ) ? true : false;
}
$num = 9;
if (isThreeDisctFactors( $num ))
echo "Yes\n" ;
else
echo "No\n" ;
$num = 15;
if (isThreeDisctFactors( $num ))
echo "Yes\n" ;
else
echo "No\n" ;
$num = 12397923568441;
if (isThreeDisctFactors( $num ))
echo "Yes\n" ;
else
echo "No\n" ;
?>
|
Output :
Yes
No
No
Time complexity : O(n1/4)
Auxiliary space: O(1)
–
Using Brute Force Method in python:
Approach:
In this approach, we check all the numbers from 2 to the number itself. If the number of factors is exactly three, then the number has exactly three distinct factors.
Initialize a counter variable count to 0 to keep track of the number of factors of the given number.
Loop over all the numbers from 2 to n (inclusive) using the range function.
For each number i in the loop, check if n is divisible by i using the modulus operator %. If n is divisible by i, increment the count variable.
If the count variable is greater than 2, then the number n has more than three factors, so we return False.
After the loop, we check if the count variable is equal to 2. If yes, then the number n has exactly three distinct factors and we return True. Otherwise, the number n does not have exactly three distinct factors and we return False.
C++
#include <iostream>
using namespace std;
string hasExactlyThreeFactorsBrute( int n) {
int count = 0;
for ( int i = 1; i <= n; i++) {
if (n % i == 0) {
count++;
}
}
return (count == 3) ? "Yes" : "No" ;
}
int main() {
cout << hasExactlyThreeFactorsBrute(9) << endl;
cout << hasExactlyThreeFactorsBrute(10) << endl;
return 0;
}
|
Java
public class GFG {
public static String hasExactlyThreeFactorsBrute( int n) {
int count = 0 ;
for ( int i = 1 ; i <= n; i++) {
if (n % i == 0 ) {
count++;
}
}
return (count == 3 ) ? "Yes" : "No" ;
}
public static void main(String[] args) {
System.out.println(hasExactlyThreeFactorsBrute( 9 ));
System.out.println(hasExactlyThreeFactorsBrute( 10 ));
}
}
|
Python3
def has_exactly_three_factors_brute(n):
count = 0
for i in range ( 1 , n + 1 ):
if n % i = = 0 :
count + = 1
if count = = 3 :
return "Yes"
else :
return "No"
print (has_exactly_three_factors_brute( 9 ))
print (has_exactly_three_factors_brute( 10 ))
|
C#
using System;
public class GFG
{
public static string HasExactlyThreeFactorsBrute( int n)
{
int count = 0;
for ( int i = 1; i <= n; i++)
{
if (n % i == 0)
{
count++;
}
}
return (count == 3) ? "Yes" : "No" ;
}
public static void Main( string [] args)
{
Console.WriteLine(HasExactlyThreeFactorsBrute(9));
Console.WriteLine(HasExactlyThreeFactorsBrute(10));
}
}
|
Javascript
function hasExactlyThreeFactorsBrute(n) {
let count = 0;
for (let i = 1; i <= n; i++) {
if (n % i === 0) {
count++;
}
}
return (count === 3) ? "Yes" : "No" ;
}
console.log(hasExactlyThreeFactorsBrute(9));
console.log(hasExactlyThreeFactorsBrute(10));
|
Time Complexity: O(n)
Space Complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...