Super-prime numbers (also known as higher order primes) are the subsequence of prime numbers that occupy prime-numbered positions within the sequence of all prime numbers. First few Super-Primes are 3, 5, 11 and 17.
The task is to print all the Super-Primes less than or equal to the given positive integer N.
Examples:
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
The idea is to generate all the primes less than or equal to the given number N using Sieve of Eratosthenes. Once we have generated all such primes, we iterate through all numbers and store it in the array. Once we have stored all the primes in the array, we iterate through the array and print all prime number which occupies prime number position in the array.
C++
#include <bits/stdc++.h>
using namespace std;
bool SieveOfEratosthenes( int n, bool isPrime[])
{
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] == true ) {
for ( int i = p * 2; i <= n; i += p)
isPrime[i] = false ;
}
}
}
void superPrimes( int n)
{
bool isPrime[n + 1];
SieveOfEratosthenes(n, isPrime);
int primes[n + 1], j = 0;
for ( int p = 2; p <= n; p++)
if (isPrime[p])
primes[j++] = p;
for ( int k = 0; k < j; k++)
if (isPrime[k + 1])
cout << primes[k] << " " ;
}
int main()
{
int n = 241;
cout << "Super-Primes less than or equal to " << n
<< " are :" << endl;
superPrimes(n);
return 0;
}
|
C
#include <stdbool.h>
#include <stdio.h>
bool SieveOfEratosthenes( int n, bool isPrime[])
{
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] == true ) {
for ( int i = p * 2; i <= n; i += p)
isPrime[i] = false ;
}
}
}
void superPrimes( int n)
{
bool isPrime[n + 1];
SieveOfEratosthenes(n, isPrime);
int primes[n + 1], j = 0;
for ( int p = 2; p <= n; p++)
if (isPrime[p])
primes[j++] = p;
for ( int k = 0; k < j; k++)
if (isPrime[k + 1])
printf ( "%d " , primes[k]);
}
int main()
{
int n = 241;
printf ( "Super-Primes less than or equal to %d are :\n" , n);
superPrimes(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void SieveOfEratosthenes( int n, boolean isPrime[])
{
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] == true ) {
for ( int i = p * 2 ; i <= n; i += p)
isPrime[i] = false ;
}
}
}
static void superPrimes( int n)
{
boolean isPrime[] = new boolean [n + 1 ];
SieveOfEratosthenes(n, isPrime);
int primes[] = new int [n + 1 ];
int j = 0 ;
for ( int p = 2 ; p <= n; p++)
if (isPrime[p])
primes[j++] = p;
for ( int k = 0 ; k < j; k++)
if (isPrime[k + 1 ])
System.out.print(primes[k] + " " );
}
public static void main(String args[])
{
int n = 241 ;
System.out.println(
"Super-Primes less than or equal to " + n + " are :" );
superPrimes(n);
}
}
|
Python3
def SieveOfEratosthenes(n, isPrime):
isPrime[ 0 ] = isPrime[ 1 ] = False
for i in range ( 2 ,n + 1 ):
isPrime[i] = True
for p in range ( 2 ,n + 1 ):
if (p * p< = n and isPrime[p] = = True ):
for i in range (p * 2 ,n + 1 ,p):
isPrime[i] = False
p + = 1
def superPrimes(n):
isPrime = [ 1 for i in range (n + 1 )]
SieveOfEratosthenes(n, isPrime)
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
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)
|
C#
using System;
class GFG {
static void SieveOfEratosthenes( int n, bool [] isPrime)
{
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] == true ) {
for ( int i = p * 2; i <= n; i += p)
isPrime[i] = false ;
}
}
}
static void superPrimes( int n)
{
bool [] isPrime = new bool [n + 1];
SieveOfEratosthenes(n, isPrime);
int [] primes = new int [n + 1];
int j = 0;
for ( int p = 2; p <= n; p++)
if (isPrime[p])
primes[j++] = p;
for ( int k = 0; k < j; k++)
if (isPrime[k + 1])
Console.Write(primes[k] + " " );
}
public static void Main()
{
int n = 241;
Console.WriteLine( "Super-Primes less than or equal to "
+ n + " are :" );
superPrimes(n);
}
}
|
PHP
<?php
function SieveOfEratosthenes( $n , & $isPrime )
{
$isPrime [0] = $isPrime [1] = false;
for ( $i = 2; $i <= $n ; $i ++)
$isPrime [ $i ] = true;
for ( $p = 2; $p * $p <= $n ; $p ++)
{
if ( $isPrime [ $p ] == true)
{
for ( $i = $p * 2; $i <= $n ; $i += $p )
$isPrime [ $i ] = false;
}
}
}
function superPrimes( $n )
{
$isPrime = array_fill (0, $n + 1, false);
SieveOfEratosthenes( $n , $isPrime );
$primes = array_fill (0, $n + 1, 0);
$j = 0;
for ( $p = 2; $p <= $n ; $p ++)
if ( $isPrime [ $p ])
$primes [ $j ++] = $p ;
for ( $k = 0; $k < $j ; $k ++)
if ( $isPrime [ $k + 1])
echo $primes [ $k ] . " " ;
}
$n = 241;
echo "Super-Primes less than or equal to " .
$n . " are :\n" ;
superPrimes( $n );
?>
|
Javascript
<script>
function SieveOfEratosthenes
(n, isPrime)
{
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] == true )
{
for (let i = p * 2; i <= n; i += p)
isPrime[i] = false ;
}
}
}
function superPrimes(n)
{
let isPrime = [];
SieveOfEratosthenes(n, isPrime);
let primes = [];
let j = 0;
for (let p = 2; p <= n; p++)
if (isPrime[p] != 0)
primes[j++] = p;
for (let k = 0; k < j; k++)
if (isPrime[k + 1])
document.write(primes[k]+ " " );
}
let n = 241;
document.write( "Super-Primes less than or equal to "
+n+ " are :" + "<br/>" );
superPrimes(n);
</script>
|
Output:
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
Time complexity : – O(n*log(log(n)))
Auxiliary Space:- O(N)
References: https://en.wikipedia.org/wiki/Super-prime
This article is contributed by Rahul Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.-