Imprimir todos los números semiprimos menores o iguales a N

Dado un número entero N , la tarea es imprimir todos los números semiprimos ≤ N .
Un número semiprimo es un número entero que se puede expresar como el producto de dos números primos distintos. 
Por ejemplo, 15 = 3 * 5 es un número semiprimo pero 9 = 3 * 3 no lo es .

Ejemplos: 

Entrada: N = 20 
Salida: 6 10 14 15

Entrada: N = 50 
Salida: 6 10 14 15 21 22 26 33 34 35 38 39 46 

requisitos previos:  

Enfoque: Para cada número < N , cuente el número de factores primos que tiene. Si el número de factores primos es 2 , entonces el número es un número semiprimo ya que todos los números semiprimos tienen solo 2 factores primos.

A continuación se muestra la implementación del enfoque anterior:  

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to create Sieve for Semi Prime Numbers
vector<int> 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<int> 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<int> 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<Integer> 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<Integer> 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<Integer> 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 */

Python3

# 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

PHP

<?php
// PHP implementation of the approach
 
// Function to create Sieve for Semi Prime Numbers
function createSemiPrimeSieve($n)
{
    $v = array_fill(0, $n + 1, 0);
 
    // 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 = 1; $i <= $n; $i++)
        $v[$i] = $i;
 
    $countDivision = array_fill(0, $n + 1, 0);
 
    for ($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 ($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 ($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
    $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
?>

Javascript

<script>
// Javascript implementation of the approach
 
// Function to create Sieve for Semi Prime Numbers
function createSemiPrimeSieve(n)
{
    let v = new Array(n + 1).fill(0);
 
    // 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 (let i = 1; i <= n; i++)
        v[i] = i;
 
    let countDivision = new Array(n + 1).fill(0);
 
    for (let 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 (let 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 (let 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
    let res = new Array();
 
    for (let 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(i);
    }
 
    return res;
}
 
// Driver code
let n = 16;
let semiPrime= new Array();
semiPrime = createSemiPrimeSieve(n);
 
// Print all semi-primes
for (let i = 0; i < semiPrime.length; i++)
    document.write(semiPrime[i] + " ");
 
// This code is contributed by _saurabh_jaiswal
 
</script>
Producción: 

6 10 14 15

 

Publicación traducida automáticamente

Artículo escrito por RishabhKejariwal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *