# Check whether a number is semiprime or not

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


## Recommended: Please solve it on PRACTICE first, before moving on to the solution.

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 prime number.

## C

 // C Program to check whether  // number is semiprime or not  #include     // 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.

## PHP

  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.  ?>

Output :

True
False


Time complexity: O()
Auxiliary space: O(1)

Reference: https://en.wikipedia.org/wiki/Semiprime

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

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 : vt_m