原文:

给定一个整数 n ,任务是打印所有的 ≤ n 。 半素数是可以表示为两个不同素数乘积的整数。 比如 15 = 3 * 5半素数但是 9 = 3 * 3 不是

示例:

输入:n = 20 t3】输出: 6 10 14 15

输入:n = 50 t3】输出: 6 10 14 15 21 22 26 33 34 35 38 39 46

先决条件:

方法:对于每一个数字 < n ,计算它所具有的质因数的数量。如果素数的个数是 2 ,那么这个数就是一个半素数,因为所有的半素数只有 2 个素数。

下面是上述方法的实现:

c

// c   implementation of the approach
#include 
using namespace std;
// function to create sieve for semi prime numbers
vector createsemiprimesieve(int n)
{
    int v[n   1];
    // this array will initially store the indexes
    // after performing below operations if any
    // element of array becomes 1 this means
    // that the given index is a semi-prime number
    // storing indices in each element of vector
    for (int i = 1; i <= n; i  )
        v[i] = i;
    int countdivision[n   1];
    for (int i = 0; i < n   1; i  )
        countdivision[i] = 2;
    // this array will initially be initialized by 2 and
    // will just count the divisions of a number
    // as a semiprime number has only 2 prime factors
    // which means after dividing by the 2 prime numbers
    // if the index countdivision[x] = 0 and v[x] = 1
    // this means that x is a semiprime number
    // if number a is prime then its
    // countdivision[a] = 2 and v[a] = a
    for (int i = 2; i <= n; i  ) {
        // if v[i] != i this means that it is
        // not a prime number as it contains
        // a divisor which has already divided it
        // same reason if countdivision[i] != 2
        if (v[i] == i && countdivision[i] == 2) {
            // j goes for each factor of i
            for (int j = 2 * i; j <= n; j  = i) {
                if (countdivision[j] > 0) {
                    // dividing the number by i
                    // and storing the dividend
                    v[j] = v[j] / i;
                    // decreasing the countdivision
                    countdivision[j]--;
                }
            }
        }
    }
    // a new vector to store all semi primes
    vector res;
    for (int i = 2; i <= n; i  ) {
        // if a number becomes one and
        // its countdivision becomes 0
        // it means the number has
        // two prime divisors
        if (v[i] == 1 && countdivision[i] == 0)
            res.push_back(i);
    }
    return res;
}
// driver code
int main()
{
    int n = 16;
    vector semiprime = createsemiprimesieve(n);
    // print all semi-primes
    for (int i = 0; i < semiprime.size(); i  )
        cout << semiprime[i] << " ";
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

import java.util.*;
// java implementation of the approach
class gfg
{
    // function to create sieve for semi prime numbers
    static vector createsemiprimesieve(int n)
    {
        int v[] = new int[n   1];
        // this array will initially store the indexes
        // after performing below operations if any
        // element of array becomes 1 this means
        // that the given index is a semi-prime number
        // storing indices in each element of vector
        for (int i = 1; i <= n; i  )
        {
            v[i] = i;
        }
        int countdivision[] = new int[n   1];
        for (int i = 0; i < n   1; i  )
        {
            countdivision[i] = 2;
        }
        // this array will initially be initialized by 2 and
        // will just count the divisions of a number
        // as a semiprime number has only 2 prime factors
        // which means after dividing by the 2 prime numbers
        // if the index countdivision[x] = 0 and v[x] = 1
        // this means that x is a semiprime number
        // if number a is prime then its
        // countdivision[a] = 2 and v[a] = a
        for (int i = 2; i <= n; i  )
        {
            // if v[i] != i this means that it is
            // not a prime number as it contains
            // a divisor which has already divided it
            // same reason if countdivision[i] != 2
            if (v[i] == i && countdivision[i] == 2)
            {
                // j goes for each factor of i
                for (int j = 2 * i; j <= n; j  = i)
                {
                    if (countdivision[j] > 0)
                    {
                        // dividing the number by i
                        // and storing the dividend
                        v[j] = v[j] / i;
                        // decreasing the countdivision
                        countdivision[j]--;
                    }
                }
            }
        }
        // a new vector to store all semi primes
        vector res = new vector<>();
        for (int i = 2; i <= n; i  )
        {
            // if a number becomes one and
            // its countdivision becomes 0
            // it means the number has
            // two prime divisors
            if (v[i] == 1 && countdivision[i] == 0) {
                res.add(i);
            }
        }
        return res;
    }
    // driver code
    public static void main(string[] args)
    {
        int n = 16;
        vector semiprime = createsemiprimesieve(n);
        // print all semi-primes
        for (int i = 0; i < semiprime.size(); i  )
        {
            system.out.print(semiprime.get(i)   " ");
        }
    }
}
/* this code contributed by princiraj1992 */

python 3

# python 3 implementation of the approach
# function to create sieve for semi prime numbers
def createsemiprimesieve(n):
    v = [0 for i in range(n   1)]
    # this array will initially store the indexes
    # after performing below operations if any
    # element of array becomes 1 this means
    # that the given index is a semi-prime number
    # storing indices in each element of vector
    for i in range(1, n   1):
        v[i] = i
    countdivision = [0 for i in range(n   1)]
    for i in range(n   1):
        countdivision[i] = 2
    # this array will initially be initialized by 2 and
    # will just count the divisions of a number
    # as a semiprime number has only 2 prime factors
    # which means after dividing by the 2 prime numbers
    # if the index countdivision[x] = 0 and v[x] = 1
    # this means that x is a semiprime number
    # if number a is prime then its
    # countdivision[a] = 2 and v[a] = a
    for i in range(2, n   1, 1):
        # if v[i] != i this means that it is
        # not a prime number as it contains
        # a divisor which has already divided it
        # same reason if countdivision[i] != 2
        if (v[i] == i and countdivision[i] == 2):
            # j goes for each factor of i
            for j in range(2 * i, n   1, i):
                if (countdivision[j] > 0):
                    # dividing the number by i
                    # and storing the dividend
                    v[j] = int(v[j] / i)
                    # decreasing the countdivision
                    countdivision[j] -= 1
    # a new vector to store all semi primes
    res = []
    for i in range(2, n   1, 1):
        # if a number becomes one and
        # its countdivision becomes 0
        # it means the number has
        # two prime divisors
        if (v[i] == 1 and countdivision[i] == 0):
            res.append(i)
    return res
# driver code
if __name__ == '__main__':
    n = 16
    semiprime = createsemiprimesieve(n)
    # print all semi-primes
    for i in range(len(semiprime)):
        print(semiprime[i], end = " ")
# this code is contributed by
# surendra_gangwar

c

// c# implementation of the approach
using system;
using system.collections;
class gfg
{
// function to create sieve for semi prime numbers
static arraylist createsemiprimesieve(int n)
{
    int[] v = new int[n   1];
    // this array will initially store the indexes
    // after performing below operations if any
    // element of array becomes 1 this means
    // that the given index is a semi-prime number
    // storing indices in each element of vector
    for (int i = 1; i <= n; i  )
        v[i] = i;
    int[] countdivision = new int[n   1];
    for (int i = 0; i < n   1; i  )
        countdivision[i] = 2;
    // this array will initially be initialized by 2 and
    // will just count the divisions of a number
    // as a semiprime number has only 2 prime factors
    // which means after dividing by the 2 prime numbers
    // if the index countdivision[x] = 0 and v[x] = 1
    // this means that x is a semiprime number
    // if number a is prime then its
    // countdivision[a] = 2 and v[a] = a
    for (int i = 2; i <= n; i  )
    {
        // if v[i] != i this means that it is
        // not a prime number as it contains
        // a divisor which has already divided it
        // same reason if countdivision[i] != 2
        if (v[i] == i && countdivision[i] == 2)
        {
            // j goes for each factor of i
            for (int j = 2 * i; j <= n; j  = i)
            {
                if (countdivision[j] > 0)
                {
                    // dividing the number by i
                    // and storing the dividend
                    v[j] = v[j] / i;
                    // decreasing the countdivision
                    countdivision[j]--;
                }
            }
        }
    }
    // a new vector to store all semi primes
    arraylist res = new arraylist();
    for (int i = 2; i <= n; i  )
    {
        // if a number becomes one and
        // its countdivision becomes 0
        // it means the number has
        // two prime divisors
        if (v[i] == 1 && countdivision[i] == 0)
            res.add(i);
    }
    return res;
}
// driver code
static void main()
{
    int n = 16;
    arraylist semiprime = createsemiprimesieve(n);
    // print all semi-primes
    for (int i = 0; i < semiprime.count; i  )
        console.write((int)semiprime[i] " ");
}
}
// this code is contributed by mits

服务器端编程语言(professional hypertext preprocessor 的缩写)

 0)
                {
                    // dividing the number by i
                    // and storing the dividend
                    $v[$j] = $v[$j] / $i;
                    // decreasing the countdivision
                    $countdivision[$j]--;
                }
            }
        }
    }
    // a new vector to store all semi primes
    $res = array();
    for ($i = 2; $i <= $n; $i  )
    {
        // if a number becomes one and
        // its countdivision becomes 0
        // it means the number has
        // two prime divisors
        if ($v[$i] == 1 && $countdivision[$i] == 0)
            array_push($res, $i);
    }
    return $res;
}
// driver code
$n = 16;
$semiprime= array();
$semiprime = createsemiprimesieve($n);
// print all semi-primes
for ($i = 0; $i < count($semiprime); $i  )
    echo $semiprime[$i], " ";
// this code is contributed by ihritik
?>

java 描述语言


output: 

6 10 14 15