Check if a Number is a Semiprime

Last Updated : 28 Jul, 2026

Given a positive integer n, determine whether it is a semiprime or not. A semiprime is a natural number that can be expressed as a product of exactly two prime numbers (not necessarily distinct).

Examples : 

Input: n = 35
Output: true
Explanation: 35 = 7 x 5. So 35 is a semi-prime.

Input: n = 8
Output: false
Explanation: 8 is not a semi prime.

Try It Yourself
redirect icon

[Naive Approach] Check All Possible Factor Pairs - O(sqrt(n) * sqrt(n)) Time and O(1) Space

The idea is to iterate through all possible divisors of n. Whenever a divisor i divides n, the corresponding factor is n / i. If both factors are prime numbers, then n can be expressed as the product of exactly two prime numbers, making it a semiprime. If no such pair exists, then n is not a semiprime.

Working of Approach:

  • Iterate through all integers from 2 to sqrt(n).
  • If the current integer divides n, compute the other factor as n / i.
  • Check whether both factors are prime.
  • If both are prime, return true.
  • If no valid factor pair is found, return false.
C++
#include <iostream>
using namespace std;

bool isPrime(int x) {

    // Numbers less than 2 are not prime
    if (x < 2)
        return false;

    // Check for divisibility up to √x
    for (int i = 2; i * i <= x; i++) {
        if (x % i == 0)
            return false;
    }

    return true;
}

bool checkSemiprime(int n) {

    // Try every possible factor
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {

            // If both factors are prime, n is semiprime
            if (isPrime(i) && isPrime(n / i))
                return true;
        }
    }

    return false;
}

int main() {
    int n = 35;

    cout << (checkSemiprime(n) ? "true" : "false");

    return 0;
}
Java
public class GFG {

    static boolean isPrime(int x) {

        // Numbers less than 2 are not prime
        if (x < 2)
            return false;

        // Check for divisibility up to √x
        for (int i = 2; i * i <= x; i++) {
            if (x % i == 0)
                return false;
        }

        return true;
    }

    static boolean checkSemiprime(int n) {

        // Try every possible factor
        for (int i = 2; i * i <= n; i++) {
            if (n % i == 0) {

                // If both factors are prime, n is semiprime
                if (isPrime(i) && isPrime(n / i))
                    return true;
            }
        }

        return false;
    }

    public static void main(String[] args) {
        int n = 35;

        System.out.println(checkSemiprime(n));
    }
}
Python
def isPrime(x):

    # Numbers less than 2 are not prime
    if x < 2:
        return False

    # Check for divisibility up to √x
    i = 2
    while i * i <= x:
        if x % i == 0:
            return False
        i += 1

    return True


def checkSemiprime(n):

    # Try every possible factor
    i = 2
    while i * i <= n:
        if n % i == 0:

            # If both factors are prime, n is semiprime
            if isPrime(i) and isPrime(n // i):
                return True

        i += 1

    return False


if __name__ == "__main__":
    n = 35

    print(str(checkSemiprime(n)).lower())
C#
using System;

class GFG {

    static bool IsPrime(int x) {

        // Numbers less than 2 are not prime
        if (x < 2)
            return false;

        // Check for divisibility up to √x
        for (int i = 2; i * i <= x; i++) {
            if (x % i == 0)
                return false;
        }

        return true;
    }

    static bool checkSemiprime(int n) {

        // Try every possible factor
        for (int i = 2; i * i <= n; i++) {
            if (n % i == 0) {

                // If both factors are prime, n is semiprime
                if (IsPrime(i) && IsPrime(n / i))
                    return true;
            }
        }

        return false;
    }

    static void Main() {
        int n = 35;

        Console.WriteLine(checkSemiprime(n).ToString().ToLower());
    }
}
JavaScript
function isPrime(x) {

    // Numbers less than 2 are not prime
    if (x < 2)
        return false;

    // Check for divisibility up to √x
    for (let i = 2; i * i <= x; i++) {
        if (x % i === 0)
            return false;
    }

    return true;
}

function checkSemiprime(n) {

    // Try every possible factor
    for (let i = 2; i * i <= n; i++) {
        if (n % i === 0) {

            // If both factors are prime, n is semiprime
            if (isPrime(i) && isPrime(Math.floor(n / i)))
                return true;
        }
    }

    return false;
}

// Driver Code
const n = 35;

console.log(checkSemiprime(n));

Output
true

[Expected Approach] Count Prime Factors by Repeated Division - O(sqrt(n)) Time and O(1) Space

The idea is to repeatedly divide n by its smallest prime factors while counting how many prime factors are obtained. Since every composite number can be uniquely factorized into prime numbers, the total count of prime factors (including repeated factors) determines whether n is a semiprime. If the count becomes exactly 2 and no factors remain, then n is a semiprime.

Working of Approach:

  • Initialize a variable count to store the number of prime factors.
  • Iterate from 2 to sqrt(n) and repeatedly divide n by every factor found.
  • Increment count after every successful division.
  • If count exceeds 2, return false.
  • After the loop, if n is greater than 1, it is a prime factor, so increment count.
  • Return true if count is exactly 2; otherwise, return false.
C++
#include <iostream>
using namespace std;

bool checkSemiprime(int n) {
    int count = 0;

    // Extract all prime factors
    for (int i = 2; i * i <= n; i++) {
        while (n % i == 0) {
            n /= i;
            count++;

            // More than two prime factors
            if (count > 2)
                return false;
        }
    }

    // Remaining n is a prime factor
    if (n > 1)
        count++;

    return count == 2;
}

int main() {
    int n = 35;

    cout << (checkSemiprime(n) ? "true" : "false");

    return 0;
}
Java
public class GFG {

    static boolean checkSemiprime(int n) {
        int count = 0;

        // Extract all prime factors
        for (int i = 2; i * i <= n; i++) {
            while (n % i == 0) {
                n /= i;
                count++;

                // More than two prime factors
                if (count > 2)
                    return false;
            }
        }

        // Remaining n is a prime factor
        if (n > 1)
            count++;

        return count == 2;
    }

    public static void main(String[] args) {
        int n = 35;

        System.out.println(checkSemiprime(n));
    }
}
Python
def checkSemiprime(n):
    count = 0

    # Extract all prime factors
    i = 2
    while i * i <= n:
        while n % i == 0:
            n //= i
            count += 1

            # More than two prime factors
            if count > 2:
                return False

        i += 1

    # Remaining n is a prime factor
    if n > 1:
        count += 1

    return count == 2


if __name__ == "__main__":
    n = 35

    print(str(checkSemiprime(n)).lower())
C#
using System;

class GFG {

    static bool checkSemiprime(int n) {
        int count = 0;

        // Extract all prime factors
        for (int i = 2; i * i <= n; i++) {
            while (n % i == 0) {
                n /= i;
                count++;

                // More than two prime factors
                if (count > 2)
                    return false;
            }
        }

        // Remaining n is a prime factor
        if (n > 1)
            count++;

        return count == 2;
    }

    static void Main() {
        int n = 35;

        Console.WriteLine(checkSemiprime(n).ToString().ToLower());
    }
}
JavaScript
function checkSemiprime(n) {
    let count = 0;

    // Extract all prime factors
    for (let i = 2; i * i <= n; i++) {
        while (n % i === 0) {
            n = Math.floor(n / i);
            count++;

            // More than two prime factors
            if (count > 2)
                return false;
        }
    }

    // Remaining n is a prime factor
    if (n > 1)
        count++;

    return count === 2;
}

// Driver Code
const n = 35;

console.log(checkSemiprime(n));

Output
true
Comment