📌  相关文章
📜  检查整数是否可以表示为两个半素数之和

📅  最后修改于: 2021-06-26 21:54:26             🧑  作者: Mango

给定一个正整数N,检查它是否可以表示为两个半素数之和。
半素数 如果数字可以表示为两个素数(不一定是互斥数)的乘积,则称该数字为半素数。
1 -100范围内的半素是:

例子:

Input : N = 30
Output: YES
Explanation: 30 can be expressed as '15 + 15' 
15 is semi-primes as 15 is a product of two primes 3 and 5.

Input : N = 45
Output : YES
Explanation: 45 can be expressed as '35 + 10'
35 and 10 are also  semi-primes as it can a be expressed 
as product of two primes:
       35 = 5 * 7
       10 = 2 * 5   

先决条件

  • 半素数

一个简单的解决方案是遍历形式i = 1到N  并检查i和Ni是否为半素数。如果是,请打印i和ni。
一个有效的解决方案是预先计算数组中的半素数到给定范围,然后遍历该半素数数组,并检查n-arr [i]是否为半素数。因为,如果n-arr [i]也是半素数,则arr [i]已经是一个半素数,则n可以表示为两个半素数的总和。
下面是上述方法的实现:

C++
// CPP Code to check if an integer
// can be expressed as sum of
// two semi-primes
 
#include 
using namespace std;
#define MAX 1000000
 
vector arr;
bool sprime[MAX];
 
// Utility function to compute
// semi-primes in a range
void computeSemiPrime()
{
    memset(sprime, false, sizeof(sprime));
 
    for (int i = 2; i < MAX; i++) {
 
        int cnt = 0;
        int num = i;
        for (int j = 2; cnt < 2 && j * j <= num; ++j) {
            while (num % j == 0) {
                num /= j, ++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;
 
        // if count is equal to '2' then
        // number is semi-prime
 
        if (cnt == 2) {
 
            sprime[i] = true;
            arr.push_back(i);
        }
    }
}
 
// Utility function to check
// if a number sum of two
// semi-primes
bool checkSemiPrime(int n)
{
    int i = 0;
 
    while (arr[i] <= n / 2) {
 
        // arr[i] is already a semi-prime
        // if n-arr[i] is also a semi-prime
        // then we a number can be expressed as
        // sum of two semi-primes
 
        if (sprime[n - arr[i]]) {
            return true;
        }
 
        i++;
    }
 
    return false;
}
 
// Driver code
int main()
{
    computeSemiPrime();
 
    int n = 30;
    if (checkSemiPrime(n))
        cout << "YES";
    else
        cout << "NO";
 
    return 0;
}


Java
// Java Code to check if an integer
// can be expressed as sum of
// two semi-primes
 
import java.util.*;
 
class GFG {
 
    static final int MAX = 1000000;
    static Vector arr = new Vector<>();
    static boolean[] sprime = new boolean[MAX];
 
    // Utility function to compute
    // semi-primes in a range
    static void computeSemiPrime()
    {
 
        for (int i = 0; i < MAX; i++)
            sprime[i] = false;
 
        for (int i = 2; i < MAX; i++) {
 
            int cnt = 0;
            int num = i;
            for (int j = 2; cnt < 2 && j * j <= num; ++j) {
                while (num % j == 0) {
                    num /= j;
                    ++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;
 
            // if count is equal to '2' then
            // number is semi-prime
 
            if (cnt == 2) {
 
                sprime[i] = true;
                arr.add(i);
            }
        }
    }
 
    // Utility function to check
    // if a number is sum of two
    // semi-primes
    static boolean checkSemiPrime(int n)
    {
        int i = 0;
 
        while (arr.get(i) <= n / 2) {
 
            // arr[i] is already a semi-prime
            // if n-arr[i] is also a semi-prime
            // then  a number can be expressed as
            // sum of two semi-primes
 
            if (sprime[n - arr.get(i)]) {
                return true;
            }
 
            i++;
        }
 
        return false;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        computeSemiPrime();
 
        int n = 30;
        if (checkSemiPrime(n))
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}


Python3
# Python3 Code to check if an integer can
# be expressed as sum of two semi-primes
MAX = 10000
 
arr = []
sprime = [False] * (MAX)
 
# Utility function to compute
# semi-primes in a range
def computeSemiPrime():
 
    for i in range(2, MAX):
 
        cnt, num, j = 0, i, 2
        while cnt < 2 and j * j <= num:
            while num % j == 0:
                num /= j
                 
                # Increment count of prime numbers
                cnt += 1
                 
            j += 1
 
        # 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
 
        # if count is equal to '2' then
        # number is semi-prime
        if cnt == 2:
 
            sprime[i] = True
            arr.append(i)
 
# Utility function to check
# if a number sum of two
# semi-primes
def checkSemiPrime(n):
 
    i = 0
    while arr[i] <= n // 2:
 
        # arr[i] is already a semi-prime
        # if n-arr[i] is also a semi-prime
        # then a number can be expressed as
        # sum of two semi-primes
        if sprime[n - arr[i]] == True:
            return True
 
        i += 1
     
    return False
 
# Driver code
if __name__ == "__main__":
 
    computeSemiPrime()
 
    n = 30
    if checkSemiPrime(n) == True:
        print("YES")
    else:
        print("NO")
 
# This code is contributed by
# Rituraj Jain


C#
// C# Code to check if an integer
// can be expressed as sum of
// two semi-primes
using System.Collections.Generic;
 
class GFG
{
 
static int MAX = 1000000;
static List arr = new List();
static bool[] sprime = new bool[MAX];
 
// Utility function to compute
// semi-primes in a range
static void computeSemiPrime()
{
 
    for (int i = 0; i < MAX; i++)
        sprime[i] = false;
 
    for (int i = 2; i < MAX; i++)
    {
 
        int cnt = 0;
        int num = i;
        for (int j = 2; cnt < 2 && j * j <= num; ++j)
        {
            while (num % j == 0)
            {
                num /= j;
                ++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;
 
        // if count is equal to '2' then
        // number is semi-prime
 
        if (cnt == 2)
        {
            sprime[i] = true;
            arr.Add(i);
        }
    }
}
 
// Utility function to check
// if a number is sum of two
// semi-primes
static bool checkSemiPrime(int n)
{
    int i = 0;
 
    while (arr[i] <= n / 2)
    {
 
        // arr[i] is already a semi-prime
        // if n-arr[i] is also a semi-prime
        // then a number can be expressed as
        // sum of two semi-primes
 
        if (sprime[n - arr[i]])
        {
            return true;
        }
 
        i++;
    }
 
    return false;
}
 
// Driver code
public static void Main()
{
    computeSemiPrime();
 
    int n = 30;
    if (checkSemiPrime(n))
        System.Console.WriteLine("YES");
    else
        System.Console.WriteLine("NO");
}
}
 
// This code is contributed by mits


Javascript


输出:
YES