Súper principal

Los números superprimos (también conocidos como números primos de orden superior ) son la subsecuencia de números primos que ocupan posiciones de números primos dentro de la secuencia de todos los números primos. Los primeros Súper-Primos son 3, 5, 11 y 17. 
La tarea es imprimir todos los Súper-Primos menores o iguales al número entero positivo N.
Ejemplos: 
 

Input: 7
Output: 3 5 
3 is super prime because it appears at second
position in list of primes (2, 3, 5, 7, 11, 13, 
17, 19, 23, ...) and 2 is also prime. Similarly
5 appears at third position and 3 is a prime.

Input: 17
Output: 3 5 11 17

La idea es generar todos los números primos menores o iguales al número N dado usando la Criba de Eratóstenes. Una vez que hemos generado todos esos números primos, iteramos a través de todos los números y los almacenamos en la array. Una vez que hemos almacenado todos los primos en la array, iteramos a través de la array e imprimimos todos los números primos que ocupan la posición de números primos en la array. 
 

C++

// C++ program to print super primes less than  or equal to n.
#include <bits/stdc++.h>
using namespace std;
 
// Generate all prime numbers less than n.
bool SieveOfEratosthenes(int n, bool isPrime[])
{
    // Initialize all entries of boolean array as true. A
    // value in isPrime[i] will finally be false if i is Not
    // a prime, else true bool isPrime[n+1];
    isPrime[0] = isPrime[1] = false;
    for (int i = 2; i <= n; i++)
        isPrime[i] = true;
 
    for (int p = 2; p * p <= n; p++) {
        // If isPrime[p] is not changed, then it is  a prime
        if (isPrime[p] == true) {
            // Update all multiples of p
            for (int i = p * 2; i <= n; i += p)
                isPrime[i] = false;
        }
    }
}
 
// Prints all super primes less than or equal to n.
void superPrimes(int n)
{
    // Generating primes using Sieve
    bool isPrime[n + 1];
    SieveOfEratosthenes(n, isPrime);
 
    // Storing all the primes generated in a an array
    // primes[]
    int primes[n + 1], j = 0;
    for (int p = 2; p <= n; p++)
        if (isPrime[p])
            primes[j++] = p;
 
    // Printing all those prime numbers that occupy prime
    // numbered position in sequence of prime numbers.
    for (int k = 0; k < j; k++)
        if (isPrime[k + 1])
            cout << primes[k] << " ";
}
 
// Driven program
int main()
{
    int n = 241;
    cout << "Super-Primes less than or equal to " << n
         << " are :" << endl;
    superPrimes(n);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

C

// C program to print super primes less than  or equal to n.
#include <stdbool.h>
#include <stdio.h>
 
// Generate all prime numbers less than n.
bool SieveOfEratosthenes(int n, bool isPrime[])
{
    // Initialize all entries of boolean array as true. A
    // value in isPrime[i] will finally be false if i is Not
    // a prime, else true bool isPrime[n+1];
    isPrime[0] = isPrime[1] = false;
    for (int i = 2; i <= n; i++)
        isPrime[i] = true;
 
    for (int p = 2; p * p <= n; p++) {
        // If isPrime[p] is not changed, then it is  a prime
        if (isPrime[p] == true) {
            // Update all multiples of p
            for (int i = p * 2; i <= n; i += p)
                isPrime[i] = false;
        }
    }
}
 
// Prints all super primes less than or equal to n.
void superPrimes(int n)
{
    // Generating primes using Sieve
    bool isPrime[n + 1];
    SieveOfEratosthenes(n, isPrime);
 
    // Storing all the primes generated in a an array
    // primes[]
    int primes[n + 1], j = 0;
    for (int p = 2; p <= n; p++)
        if (isPrime[p])
            primes[j++] = p;
 
    // Printing all those prime numbers that occupy prime
    // numbered position in sequence of prime numbers.
    for (int k = 0; k < j; k++)
        if (isPrime[k + 1])
            printf("%d ", primes[k]);
}
 
// Driven program
int main()
{
    int n = 241;
    printf("Super-Primes less than or equal to %d are :\n", n);
    superPrimes(n);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Java

// Java program to print super primes less than or equal to n.
import java.io.*;
 
class GFG {
 
    // Generate all prime numbers less than n.
    static void SieveOfEratosthenes(int n, boolean isPrime[])
    {
        // Initialize all entries of boolean array as true.
        // A value in isPrime[i] will finally be false if i
        // is Not a prime, else true bool isPrime[n+1];
        isPrime[0] = isPrime[1] = false;
        for (int i = 2; i <= n; i++)
            isPrime[i] = true;
 
        for (int p = 2; p * p <= n; p++) {
            // If isPrime[p] is not changed, then it is a prime
            if (isPrime[p] == true) {
                // Update all multiples of p
                for (int i = p * 2; i <= n; i += p)
                    isPrime[i] = false;
            }
        }
    }
 
    // Prints all super primes less than or equal to n.
    static void superPrimes(int n)
    {
 
        // Generating primes using Sieve
        boolean isPrime[] = new boolean[n + 1];
        SieveOfEratosthenes(n, isPrime);
 
        // Storing all the primes generated in a an array primes[]
        int primes[] = new int[n + 1];
        int j = 0;
 
        for (int p = 2; p <= n; p++)
            if (isPrime[p])
                primes[j++] = p;
 
        // Printing all those prime numbers that occupy prime
        // numbered position in sequence of prime numbers.
        for (int k = 0; k < j; k++)
            if (isPrime[k + 1])
                System.out.print(primes[k] + " ");
    }
 
    // Driven program
    public static void main(String args[])
    {
        int n = 241;
        System.out.println(
            "Super-Primes less than or equal to " + n + " are :");
        superPrimes(n);
    }
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Python3

# Python program to print super primes less than
# or equal to n.
 
# Generate all prime numbers less than n.
def SieveOfEratosthenes(n, isPrime):
    # Initialize all entries of boolean array
    # as true. A value in isPrime[i] will finally
    # be false if i is Not a prime, else true
    # bool isPrime[n+1]
    isPrime[0] = isPrime[1] = False
    for i in range(2,n+1):
        isPrime[i] = True
  
    for p in range(2,n+1):
        # If isPrime[p] is not changed, then it is
        # a prime
        if (p*p<=n and isPrime[p] == True):
            # Update all multiples of p
            for i in range(p*2,n+1,p):
                isPrime[i] = False
                p += 1
def superPrimes(n):
     
    # Generating primes using Sieve
    isPrime = [1 for i in range(n+1)]
    SieveOfEratosthenes(n, isPrime)
  
    # Storing all the primes generated in a
    # an array primes[]
    primes = [0 for i in range(2,n+1)]
    j = 0
    for p in range(2,n+1):
       if (isPrime[p]):
           primes[j] = p
           j += 1
  
    # Printing all those prime numbers that
    # occupy prime numbered position in
    # sequence of prime numbers.
    for k in range(j):
        if (isPrime[k+1]):
            print (primes[k],end=" ")
 
n = 241
print ("\nSuper-Primes less than or equal to ", n, " are :",)
superPrimes(n)
# Contributed by: Afzal

C#

// Program to print super primes
// less than or equal to n.
using System;
 
class GFG {
 
    // Generate all prime
    // numbers less than n.
    static void SieveOfEratosthenes(int n, bool[] isPrime)
    {
        // Initialize all entries of boolean
        // array as true. A value in isPrime[i]
        // will finally be false if i is Not
        // a prime, else true bool isPrime[n+1];
        isPrime[0] = isPrime[1] = false;
 
        for (int i = 2; i <= n; i++)
            isPrime[i] = true;
 
        for (int p = 2; p * p <= n; p++) {
            // If isPrime[p] is not changed,
            // then it is a prime
            if (isPrime[p] == true) {
                // Update all multiples of p
                for (int i = p * 2; i <= n; i += p)
                    isPrime[i] = false;
            }
        }
    }
 
    // Prints all super primes less
    // than or equal to n.
    static void superPrimes(int n)
    {
 
        // Generating primes using Sieve
        bool[] isPrime = new bool[n + 1];
        SieveOfEratosthenes(n, isPrime);
 
        // Storing all the primes generated
        // in a an array primes[]
        int[] primes = new int[n + 1];
        int j = 0;
 
        for (int p = 2; p <= n; p++)
            if (isPrime[p])
                primes[j++] = p;
 
        // Printing all those prime numbers
        // that occupy prime number position
        // in sequence of prime numbers.
        for (int k = 0; k < j; k++)
            if (isPrime[k + 1])
                Console.Write(primes[k] + " ");
    }
 
    // Driven program
    public static void Main()
    {
        int n = 241;
        Console.WriteLine("Super-Primes less than or equal to "
                          + n + " are :");
        superPrimes(n);
    }
}
 
// This code is contributed by Anant Agarwal.

PHP

<?php
// PHP program to print super primes
// less than or equal to n.
 
// Generate all prime numbers less than n.
function SieveOfEratosthenes($n, &$isPrime)
{
    // Initialize all entries of boolean array
    // as true. A value in isPrime[i] will
    // finally be false if i is Not a prime,
    // else true bool isPrime[n+1];
    $isPrime[0] = $isPrime[1] = false;
    for ($i = 2; $i <= $n; $i++)
        $isPrime[$i] = true;
 
    for ($p = 2; $p * $p <= $n; $p++)
    {
        // If isPrime[p] is not changed,
        // then it is a prime
        if ($isPrime[$p] == true)
        {
            // Update all multiples of p
            for ($i = $p * 2; $i <= $n; $i += $p)
                $isPrime[$i] = false;
        }
    }
}
 
// Prints all super primes less
// than or equal to n.
function superPrimes($n)
{
    // Generating primes using Sieve
    $isPrime = array_fill(0, $n + 1, false);
    SieveOfEratosthenes($n, $isPrime);
 
    // Storing all the primes generated
    // in a an array primes[]
    $primes = array_fill(0, $n + 1, 0);
    $j = 0;
    for ($p = 2; $p <= $n; $p++)
    if ($isPrime[$p])
        $primes[$j++] = $p;
 
    // Printing all those prime numbers
    // that occupy prime numbered position
    // in sequence of prime numbers.
    for ($k = 0; $k < $j; $k++)
        if ($isPrime[$k + 1])
            echo $primes[$k] . " ";
}
 
// Driver Code
$n = 241;
echo "Super-Primes less than or equal to " .
                            $n . " are :\n";
superPrimes($n);
 
// This code is contributed by mits
?>

Javascript

<script>
 
// JavaScript program to print super
// primes less than or equal to n.
 
    // Generate all prime
    // numbers less than n.
    function SieveOfEratosthenes
                (n, isPrime)
    {
        // Initialize all entries of boolean
        // array as true. A value in isPrime[i]
        // will finally be false if i is Not
        // a prime, else true bool isPrime[n+1];
        isPrime[0] = isPrime[1] = false;
        for (let i = 2; i <= n; i++)
            isPrime[i] = true;
       
        for (let p = 2; p * p <= n; p++)
        {
            // If isPrime[p] is not changed,
            // then it is a prime
            if (isPrime[p] == true)
            {
                // Update all multiples of p
                for (let i = p * 2; i <= n; i += p)
                    isPrime[i] = false;
            }
        }
    }
       
    // Prints all super primes less
    // than or equal to n.
    function superPrimes(n)
    {
           
        // Generating primes using Sieve
        let isPrime = [];
        SieveOfEratosthenes(n, isPrime);
       
        // Storing all the primes generated
        // in a an array primes[]
        let primes = [];
        let j = 0;
           
        for (let p = 2; p <= n; p++)
            if (isPrime[p] != 0)
                primes[j++] = p;
       
        // Printing all those prime numbers that
        // occupy prime numbered position in
        // sequence of prime numbers.
        for (let k = 0; k < j; k++)
            if (isPrime[k + 1])
                document.write(primes[k]+ " ");
    }
 
// Driver Code
        let n = 241;
        document.write("Super-Primes less than or equal to "
                            +n+" are :" + "<br/>");
        superPrimes(n);
 
// This code is contributed by code_hunt.
</script>

Producción: 
 

Super-Primes less than or equal to 241 are :
3 5 11 17 31 41 59 67 83 109 127 157 179 191 211 241 

Referencias: https://en.wikipedia.org/wiki/Super-prime
Este artículo es una contribución de Rahul Agrawal . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.-
 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 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 *