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
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 <bits/stdc++.h>using namespace std;// Utility function to check whether// number is semiprime or notint 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 semiprimevoid semiprime(int n){ if (checkSemiprime(n)) cout << "True\n"; else cout << "False\n";}// Driver codeint 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 notint 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 semiprimevoid semiprime(int n){ if (checkSemiprime(n)) printf("True\n"); else printf("False\n");}// Driver codeint main(){ int n = 6; semiprime(n); n = 8; semiprime(n); return 0;} |
Java
// Java Program to check whether// number is semiprime or notclass 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 notimport math# Utility function to check whether# number is semiprime or notdef 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 semiprimedef semiprime(n): if checkSemiprime(n) == True: print("True") else: print("False")# Driver coden = 6semiprime(n)n = 8semiprime(n); |
C#
// C# Program to check whether// number is semiprime or notusing 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
<?php// PHP Program to check whether// number is semiprime or not// Utility function to check whether// number is semiprime or notfunction checkSemiprime($num){ $cnt = 0; for ( $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;}// Function to print 'True' or 'False'// according to condition of semiprimefunction 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.?> |
Javascript
<script>// JavaScript Program to check whether// number is semiprime or not // Utility function to check whether // number is semiprime or not function checkSemiprime(num) { let cnt = 0; for (let 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 function semiprime(n) { if (checkSemiprime(n) != 0) document.write("True" + "<br/>"); else document.write("False" + "<br/>"); } // Driver code let n = 6; semiprime(n); n = 8; semiprime(n); </script> |
Output :
True False
Time complexity: O(
)
Auxiliary space: O(1)
Reference: https://en.wikipedia.org/wiki/Semiprime





Please Login to comment...