Check whether a number is semiprime or not
Last Updated :
09 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:
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
<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>
PHP
<?php
// PHP Program to check whether
// number is semiprime or not
// Utility function to check whether
// number is semiprime or not
function 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 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:
- Take input a positive integer N.
- 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 <iostream>
#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)
Similar Reads
Check whether N is a Dihedral Prime Number or not Given an integer N, the task is to check if N is a Dihedral prime number or not. A Dihedral prime is a prime number that can be read as itself or as another prime number when read in a seven-segment display, regardless of different orientation and surface. Examples: Input: N = 108881 Output: Yes Inp
10 min read
Check whether a number is Quasiperfect number or not. A Quasiperfect number is a positive integer that is equal to the sum of all its proper divisors plus or minus one. In other words, a number n is quasiperfect if the sum of all its proper divisors is either n+1 or n-1. Examples: Input: 20Output: NOExplanation: The divisors of 20 are 1, 2, 4, 5, 10 an
7 min read
Check if a number is Quartan Prime or not Given a positive integer N, check if it is Quartan prime or not. Print 'Yes' if it is a Quartan prime otherwise Print 'No'.Quartan Prime : A prime number of the form x4 + y4 where x > 0, y > 0, and x and y are integers is a Quartan Prime. Quartan Prime in the range 1 - 100 are: 2, 17, 97 Examp
6 min read
Check whether a given number is Polydivisible or Not Given an integer n, find whether n is a Polydivisible or not. In mathematics, a number is called Polydivisible if it follows some unique properties. The number should not have any leading zeroes. The number formed by first i digits of the input number should be divisible by i, where i > 1 ~and ~i
5 min read
Check whether a number is Emirpimes or not Given a number 'n', check whether it is an emirpimes or not. An emirpimes("semiprime" when spelled backwards) derives its definition from the way it is spelt. So, an emirpimes is a number that is a semiprime (product of two prime numbers) itself, and the reversal of its digits gives another new numb
10 min read
Check whether a number is Emirpimes or not Given a number 'n', check whether it is an emirpimes or not. An emirpimes("semiprime" when spelled backwards) derives its definition from the way it is spelt. So, an emirpimes is a number that is a semiprime (product of two prime numbers) itself, and the reversal of its digits gives another new numb
10 min read