Check whether a number is semiprime or not (original) (raw)

Last Updated : 9 Dec, 2023

Given a positive integer n. Find whether a number is a **semiprime or not. Print **True if number is semiprime else **False. A semiprime is a natural number that is a product of two prime numbers.
**Examples :

**Input: 6
**Output: True
**Explanation
6 is a semiprime number as it is a
product of two prime numbers 2 and 3.
**Input: 9
**Output: True
**Input: 8
**Output: False

**Approach: The approach is simple, factorize the given number by dividing it with the divisor of a number to remove the composite number. Meanwhile, keep updating the count variable of the prime number.

Below is the implementation of the above approach:

Try It Yourselfredirect icon

C++ `

// C++ Program to check whether // number is semiprime or not #include <bits/stdc++.h> using namespace std;

// Utility function to check whether // number is semiprime or not int checkSemiprime(int num) { int cnt = 0;

for (int i = 2; cnt < 2 && i * i <= num; ++i)
    while (num % i == 0)
        num /= i, ++cnt; // Increment count
                         // of prime numbers

// If number is greater than 1, add it to
// the count variable as it indicates the
// number remain is prime number
if (num > 1)
    ++cnt;

// Return '1' if count is equal to '2' else
// return '0'
return cnt == 2;

}

// Function to print 'True' or 'False' // according to condition of semiprime void semiprime(int n) { if (checkSemiprime(n)) cout << "True\n"; else cout << "False\n"; }

// Driver code int main() { int n = 6; semiprime(n); n = 8; semiprime(n); return 0; }

// This code is contributed by rutvik_56.

C

// C Program to check whether // number is semiprime or not #include <stdio.h>

// Utility function to check whether // number is semiprime or not int checkSemiprime(int num) { int cnt = 0;

for (int i = 2; cnt < 2 && i * i <= num; ++i)
    while (num % i == 0)
        num /= i, ++cnt; // Increment count
                         // of prime numbers

// If number is greater than 1, add it to
// the count variable as it indicates the
// number remain is prime number
if (num > 1)
    ++cnt;

// Return '1' if count is equal to '2' else
// return '0'
return cnt == 2;

}

// Function to print 'True' or 'False' // according to condition of semiprime void semiprime(int n) { if (checkSemiprime(n)) printf("True\n"); else printf("False\n"); }

// Driver code int main() { int n = 6; semiprime(n);

n = 8;
semiprime(n);
return 0;

}

Java

// Java Program to check whether // number is semiprime or not class GFG{

// Utility function to check whether
// number is semiprime or not
static int checkSemiprime(int num)
{
    int cnt = 0;

    for (int i = 2; cnt < 2 && 
                 i * i <= num; ++i)
                 
        while (num % i == 0){
            num /= i;
            
            // Increment count
            // of prime numbers
            ++cnt;
            }

    // If number is greater than 1, 
    // add it to the count variable 
    // as it indicates the number 
    // remain is prime number
    if (num > 1)
        ++cnt;

    // Return '1' if count is equal 
    // to '2' else return '0'
    return cnt == 2 ? 1 : 0;
}

// Function to print 'True' or 'False'
// according to condition of semiprime
static void semiprime(int n)
{
    if (checkSemiprime(n) != 0)
        System.out.printf("True\n");
    else
        System.out.printf("False\n");
}

// Driver code
public static void main(String[] args)
{
    int n = 6;
    semiprime(n);

    n = 8;
    semiprime(n);
}

}

// This code is contributed by // Smitha Dinesh Semwal

Python3

Python Program to check whether

number is semiprime or not

import math

Utility function to check whether

number is semiprime or not

def checkSemiprime(num): cnt = 0

for i in range(2, int(math.sqrt(num)) + 1):
    while num % i == 0:
        num /= i
        cnt += 1 # Increment count
                # of prime number

    # If count is greater than 2,
    # break loop 
    if cnt >= 2: 
        break
# If number is greater than 1, add it to
# the count variable as it indicates the
# number remain is prime number
if(num > 1):
    cnt += 1

# Return '1' if count is equal to '2' else
# return '0'
return cnt == 2

Function to print 'True' or 'False'

according to condition of semiprime

def semiprime(n): if checkSemiprime(n) == True: print("True") else: print("False")

Driver code

n = 6 semiprime(n)

n = 8 semiprime(n);

C#

// C# Program to check whether // number is semiprime or not using System; class GFG{

// Utility function to check whether
// number is semiprime or not
static int checkSemiprime(int num)
{
    int cnt = 0;

    for (int i = 2; cnt < 2 && 
                i * i <= num; ++i)
                
        while (num % i == 0){
            num /= i;
            
            // Increment count
            // of prime numbers
            ++cnt;
            }

    // If number is greater than 1, 
    // add it to the count variable 
    // as it indicates the number 
    // remain is prime number
    if (num > 1)
        ++cnt;

    // Return '1' if count is equal 
    // to '2' else return '0'
    return cnt == 2 ? 1 : 0;
}

// Function to print 'True' or 'False'
// according to condition of semiprime
static void semiprime(int n)
{
    if (checkSemiprime(n) != 0)
        Console.WriteLine("True");
    else
        Console.WriteLine("False");
}

// Driver code
public static void Main()
{
    int n = 6;
    semiprime(n);

    n = 8;
    semiprime(n);
}

}

// This code is contributed by vt_m.

JavaScript

PHP

i=2;i = 2; i=2;cnt < 2 && i∗i * ii <= num;++num; ++num;++i) while ($num % $i == 0) num/=num /= num/=i; // Increment count of // prime numbers ++$cnt; // If number is greater than 1, // add it to the count variable // as it indicates the number // remain is prime number if ($num > 1) ++$cnt; // Return '1' if count is // equal to '2' // else return '0' return $cnt == 2; } // Function to print 'True' or 'False' // according to condition of semiprime function semiprime($n) { if (checkSemiprime($n)) echo "True\n"; else echo "False\n"; } // Driver code $n = 6; semiprime($n); $n = 8; semiprime($n); // This code is contributed by anuj_67. ?>

`

**Time Complexity: O(\sqrt n )
**Auxiliary space: O(1)

**Another Approach: To check whether a number is a semiprime or not, the idea is to factorize the given number into its prime factors. If the number has exactly two prime factors, then it is a semiprime. Below are the steps:

  1. Take input a positive integer **N.
  2. Iterate from **[2, N/2] and check if **N is divisible by any of the numbers in the range then check if both the divisor and quotient are prime numbers. If yes, then the number is semiprime. Otherwise, it is not a semiprime number.

Below is the implementation of the above approach:

C++ `

#include #include <math.h>

using namespace std;

// Function to check if the number is prime or not bool IsPrime(int num) { if (num <= 1) { return false; } for (int i = 2; i <= sqrt(num); i++) { if (num % i == 0) { return false; } } return true; }

// Function to check if the number is semi prime or not bool IsSemiPrime(int n) { for (int i = 2; i <= n / 2; i++) { if (n % i == 0) { int quotient = n / i; if (IsPrime(i) && IsPrime(quotient)) { return true; } } } return false; }

// Driver Code int main() { int N = 6; cout << boolalpha << IsSemiPrime(N) << endl; return 0; }

Java

import java.io.*;

class GFG { // Function to check if the number is prime or not static boolean IsPrime(int num) { if (num <= 1) { return false; } for (int i = 2; i <= Math.sqrt(num); i++) { if (num % i == 0) { return false; } } return true; }

// Function to check if the number is semi prime or not
static boolean IsSemiPrime(int n)
{
    for (int i = 2; i <= n / 2; i++) {
        if (n % i == 0) {
            int quotient = n / i;
            if (IsPrime(i) && IsPrime(quotient)) {
                return true;
            }
        }
    }
    return false;
}
public static void main (String[] args) {
    int N = 6;
    System.out.println(IsSemiPrime(N));
}

}

Python3

Python program for the above approach

Function to check if the number is

prime or not

def is_prime(num):

if num <= 1:
    return False
for i in range(2, int(num**0.5)+1):
    if num % i == 0:
        return False
return True

Function to check if the number is

semi prime or not

def is_semiprime(n):

for i in range(2, n//2+1):
    if n % i == 0:
        quotient = n//i
        if is_prime(i) and is_prime(quotient):
            return True
return False

Driver Code

N = 6 print(is_semiprime(N))

C#

using System;

public class Program {

// Function to check if the number is prime or not
public static bool IsPrime(int num)
{
    if (num <= 1) {
        return false;
    }
    for (int i = 2; i <= Math.Sqrt(num); i++) {
        if (num % i == 0) {
            return false;
        }
    }
    return true;
}

// Function to check if the number is semi prime or not
public static bool IsSemiPrime(int n)
{
    for (int i = 2; i <= n / 2; i++) {
        if (n % i == 0) {
            int quotient = n / i;
            if (IsPrime(i) && IsPrime(quotient)) {
                return true;
            }
        }
    }
    return false;
}

// Driver Code
public static void Main()
{
    int N = 6;
    Console.WriteLine(IsSemiPrime(N));
}

}

JavaScript

// Function to check if the number is prime or not function isPrime(num) { // Prime numbers are greater than 1 if (num <= 1) { return false; }

// Check for factors from 2 to the square root of the number
for (let i = 2; i <= Math.sqrt(num); i++) {
    // If the number is divisible by any other number, it's not prime
    if (num % i === 0) {
        return false;
    }
}

// If no factors are found, the number is prime
return true;

}

// Function to check if the number is semi-prime or not function isSemiPrime(n) { // Check for factors from 2 to half of the number for (let i = 2; i <= n / 2; i++) { // If a factor is found if (n % i === 0) { // Calculate the quotient const quotient = n / i;

        // If both the factor and quotient are prime, the number is semi-prime
        if (isPrime(i) && isPrime(quotient)) {
            return true;
        }
    }
}

// If no semi-prime conditions are met, the number is not semi-prime
return false;

}

// Driver Code // Test with N = 6 const N = 6; console.log(isSemiPrime(N));

`

**Time Complexity: O(N*log(log(N)))
**Auxiliary Space: O(1)