Super Prime

Last Updated : 1 Sep, 2026

Super-prime numbers (also known as higher order primes) are the subsequence of prime number sequence that occupy prime-numbered positions within the sequence of all prime numbers. The first few super primes are 3, 5, 11, and 17.

Given a positive integer n and the task is to print all the Super-Primes less than or equal to n.

Examples: 

Input: n = 5
Output: [3, 5]
Explanation: The prime numbers up to 5 are [2, 3, 5]. Their positions are [1, 2, 3]. Since positions 2 and 3 are prime, 3 and 5 are super-primes.

Input: n = 20
Output: [3, 5, 11, 17]
Explanation: The prime numbers up to 20 are [2, 3, 5, 7, 11, 13, 17, 19]. Their positions are [1, 2, 3, 4, 5, 6, 7, 8]. Since positions 2, 3, 5, and 7 are prime, 3, 5, 11, and 17 are super-primes.

Try It Yourself
redirect icon

[Naive Approach] Check Primality Individually - O(n√n) Time and O(n) Space

The idea is to find all prime numbers up to n and store them.

Then, check whether the 1-based position of each prime is also prime. If yes, add that prime to the result.

Working of Approach:

  • Traverse all numbers from 2 to n.
  • Check each number individually to determine whether it is prime.
  • Store all prime numbers in a vector.
  • Traverse the vector and check whether each prime's 1-based position is prime.
  • Add the corresponding prime number to the result.
C++
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

// Function to check whether a number is prime.
bool isPrime(int num)
{
    if (num < 2)
        return false;

    for (int i = 2; i * i <= num; i++)
    {
        if (num % i == 0)
            return false;
    }

    return true;
}

vector<int> superPrimes(int n)
{
    vector<int> primes;
    vector<int> res;

    // Find and store all prime numbers up to n.
    for (int i = 2; i <= n; i++)
    {
        if (isPrime(i))
            primes.push_back(i);
    }

    // Check whether the position of each prime is also prime.
    for (int i = 0; i < primes.size(); i++)
    {
        if (isPrime(i + 1))
            res.push_back(primes[i]);
    }

    return res;
}

int main()
{
    int n = 20;

    vector<int> ans = superPrimes(n);

    cout << "[";
    for (int i = 0; i < ans.size(); i++)
    {
        cout << ans[i];
        if (i != ans.size() - 1)
            cout << ", ";
    }
    cout << "]";

    return 0;
}
Java
import java.util.ArrayList;

class GFG {

    // Function to check whether a number is prime.
    public boolean isPrime(int num)
    {
        if (num < 2)
            return false;

        for (int i = 2; i * i <= num; i++) {
            if (num % i == 0)
                return false;
        }

        return true;
    }

    public ArrayList<Integer> superPrimes(int n)
    {
        ArrayList<Integer> primes = new ArrayList<>();
        ArrayList<Integer> res = new ArrayList<>();

        // Find and store all prime numbers up to n.
        for (int i = 2; i <= n; i++) {
            if (isPrime(i))
                primes.add(i);
        }

        // Check whether the position of each prime is also
        // prime.
        for (int i = 0; i < primes.size(); i++) {
            if (isPrime(i + 1))
                res.add(primes.get(i));
        }

        return res;
    }

    public static void main(String[] args)
    {
        int n = 20;

        GFG ob = new GFG();
        ArrayList<Integer> ans = ob.superPrimes(n);

        System.out.println(ans);
    }
}
Python
# Function to check whether a number is prime.
def isPrime(num):
    if num < 2:
        return False

    for i in range(2, int(num ** 0.5) + 1):
        if num % i == 0:
            return False

    return True


def superPrimes(n):
    primes = []
    res = []

    # Find and store all prime numbers up to n.
    for i in range(2, n + 1):
        if isPrime(i):
            primes.append(i)

    # Check whether the position of each prime is also prime.
    for i in range(len(primes)):
        if isPrime(i + 1):
            res.append(primes[i])

    return res


if __name__ == '__main__':
    n = 20

    ans = superPrimes(n)

    print('[', end='')
    for i in range(len(ans)):
        print(ans[i], end='' if i == len(ans) - 1 else ', ')
    print(']')
C#
using System;
using System.Collections.Generic;

// Function to check whether a number is prime.
public class GFG {
    public static bool IsPrime(int num)
    {
        if (num < 2)
            return false;

        for (int i = 2; i * i <= num; i++) {
            if (num % i == 0)
                return false;
        }

        return true;
    }

    public static List<int> superPrimes(int n)
    {
        List<int> primes = new List<int>();
        List<int> res = new List<int>();

        // Find and store all prime numbers up to n.
        for (int i = 2; i <= n; i++) {
            if (IsPrime(i))
                primes.Add(i);
        }

        // Check whether the position of each prime is also
        // prime.
        for (int i = 0; i < primes.Count; i++) {
            if (IsPrime(i + 1))
                res.Add(primes[i]);
        }

        return res;
    }

    public static void Main()
    {
        int n = 20;

        List<int> ans = superPrimes(n);

        Console.Write("[");
        for (int i = 0; i < ans.Count; i++) {
            Console.Write(ans[i]);
            if (i != ans.Count - 1)
                Console.Write(", ");
        }
        Console.Write("]");
    }
}
JavaScript
// Function to check whether a number is prime.
function isPrime(num)
{
    if (num < 2)
        return false;

    for (let i = 2; i * i <= num; i++) {
        if (num % i === 0)
            return false;
    }

    return true;
}

function superPrimes(n)
{
    let primes = [];
    let res = [];

    // Find and store all prime numbers up to n.
    for (let i = 2; i <= n; i++) {
        if (isPrime(i))
            primes.push(i);
    }

    // Check whether the position of each prime is also
    // prime.
    for (let i = 0; i < primes.length; i++) {
        if (isPrime(i + 1))
            res.push(primes[i]);
    }

    return res;
}

// Driver Code
let n = 20;

let ans = superPrimes(n);

console.log("[");
for (let i = 0; i < ans.length; i++) {
    process.stdout.write(ans[i].toString());
    if (i !== ans.length - 1)
        process.stdout.write(", ");
}
console.log("]");

Output
[3, 5, 11, 17]

[Expected Approach] Using Sieve of Eratosthenes - O(n log(log n)) Time and O(n) Space

The idea is to generate all prime numbers less than or equal to n using the Sieve of Eratosthenes.

Then, check which prime numbers occupy prime-numbered positions and add them to the result.

Working of Approach:

  • Use the Sieve of Eratosthenes to mark all prime numbers up to n.
  • Store all the generated prime numbers in an array.
  • Traverse the array of prime numbers.
  • For each prime at index k, check whether k + 1 is prime.
  • If its position is prime, add the number to the result.

Let us understand with an example:

  • For n = 20, the Sieve of Eratosthenes finds the prime numbers: [2, 3, 5, 7, 11, 13, 17, 19].
  • Their 1-based positions are 1, 2, 3, 4, 5, 6, 7, 8.
  • Prime positions among them are 2, 3, 5, and 7.
  • Therefore, the prime numbers at these positions are 3, 5, 11, and 17.
  • Hence, the output is [3, 5, 11, 17].
C++
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

// Generate all prime numbers less than n.
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;
        }
    }
}

vector<int> superPrimes(int n)
{
    // Generating primes using Sieve
    vector<int> res;
    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])
            res.push_back(primes[k]);

    return res;
}

int main()
{
    int n = 20;
    vector<int> ans = superPrimes(n);

    cout << "[";
    for (int i = 0; i < ans.size(); i++)
    {
        cout << ans[i];
        if (i != ans.size() - 1)
            cout << ", ";
    }
    cout << "]";

    return 0;
}
Java
import java.util.ArrayList;
import java.util.Arrays;

public 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
        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;
            }
        }
    }

    static ArrayList<Integer> superPrimes(int n)
    {
        // Generating primes using Sieve
        ArrayList<Integer> res = new ArrayList<>();
        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])
                res.add(primes[k]);

        return res;
    }

    public static void main(String[] args)
    {
        int n = 20;
        ArrayList<Integer> ans = superPrimes(n);

        System.out.print("[");
        for (int i = 0; i < ans.size(); i++) {
            System.out.print(ans.get(i));
            if (i != ans.size() - 1)
                System.out.print(", ");
        }
        System.out.print("]");
    }
}
Python
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
    isPrime[0] = isPrime[1] = False
    for i in range(2, n + 1):
        isPrime[i] = True

    for p in range(2, int(n**0.5) + 1):
        # If isPrime[p] is not changed, then it is a prime
        if isPrime[p] == True:
            # Update all multiples of p
            for i in range(p * 2, n + 1, p):
                isPrime[i] = False


def superPrimes(n):
    # Generating primes using Sieve
    res = []
    isPrime = [False] * (n + 1)
    sieveOfEratosthenes(n, isPrime)

    # Storing all the primes generated in a list primes[]
    primes = [0] * (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]:
            res.append(primes[k])

    return res


if __name__ == "__main__":
    n = 20
    ans = superPrimes(n)

    print(ans)
C#
using System;
using System.Collections.Generic;

public class GFG {
    
    // Generate all prime numbers less than n.
    public 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
        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;
            }
        }
    }

    public static List<int> superPrimes(int n)
    {
        // Generating primes using Sieve
        List<int> res = new List<int>();
        bool[] isPrime = new bool[n + 1];
        sieveOfEratosthenes(n, isPrime);

        // Storing all the primes generated in a list
        // 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])
                res.Add(primes[k]);

        return res;
    }

    public static void Main()
    {
        int n = 20;
        List<int> ans = superPrimes(n);

        Console.Write("[");
        for (int i = 0; i < ans.Count; i++) {
            Console.Write(ans[i]);
            if (i != ans.Count - 1)
                Console.Write(", ");
        }
        Console.Write("]");
    }
}
JavaScript
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
    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;
        }
    }
}

function superPrimes(n)
{
    // Generating primes using Sieve
    let res = [];
    let isPrime = Array(n + 1).fill(false);
    sieveOfEratosthenes(n, isPrime);

    // Storing all the primes generated in a array primes[]
    let primes = Array(n + 1).fill(0);
    let j = 0;
    for (let 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 (let k = 0; k < j; k++)
        if (isPrime[k + 1])
            res.push(primes[k]);

    return res;
}

// Driver Code
let n = 20;
let ans = superPrimes(n);

console.log("[" + ans.join(", ") + "]");

Output
[3, 5, 11, 17]
Comment