Open In App

Tetradic Primes

Last Updated : 23 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A Tetradic Primes are prime numbers that are also tetradic number.

A tetradic Number is a palindromic number containing only 0, 1, and 8 as digits in the number. 
 

Find the Tetradic prime numbers less than N

Given a number N, the task is to print all tetradic primes smaller than or equal to N.

Examples:

Input: N = 20 
Output: 11

Input: N = 200 
Output: 11 101 181 

Approach: The idea is to generate all prime numbers smaller than or equal to the given number N and checking every prime number whether it is tetradic or not.

  • To find if a given number is prime or not using sieve-of-eratosthenes method.
  • To check whether the given number is tetradic number or not check that the number is a palindromic or not and only contains digits as 0, 1, and 8.

Below is the implementation of above algorithm:

C++




// C++ implementation to print all
// Tetradic primes smaller than or
// equal to N.
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if the number
// N having all digits lies in
// the set (0, 1, 8)
bool isContaindigit(int n)
{
    while (n > 0)
    {
        if (!(n % 10 == 0 || 
              n % 10 == 1 || 
              n % 10 == 8))
            return false;
             
        n = n / 10;
    }
    return true;
}
   
// Function to check if the number
// N is palindrome
bool ispalindrome(int n)
{
    string temp = to_string(n);
    int l = temp.length();
       
    for(int i = 0; i < l / 2; i++)
    {
        if (temp[i] != temp[l - i - 1])
            return false;
    }
    return true;
}
   
// Function to check if a number
// N is Tetradic
bool isTetradic(int n)
{
    if (ispalindrome(n) && isContaindigit(n))
        return true;
         
    return false;
}
   
// Function to generate all primes and checking
// whether number is Tetradic or not
void printTetradicPrimesLessThanN(int n)
{
     
    // Create a boolean array "prime[0..n]" and
    // initialize all entries it as true. A value
    // in prime[i] will finally be false if i is
    // Not a prime, else true.
    bool prime[n + 1];
    memset(prime, true, sizeof(prime));
       
    int p = 2;
       
    while (p * p <= n)
    {
         
        // If prime[p] is not changed,
        // then it is a prime
        if (prime[p]) 
        {
             
            // Update all multiples of p
            for(int i = p * 2; i < n + 1; i += p)
                prime[i] = false;
        }
        p += 1;
    }
   
    // Print all Tetradic prime numbers
    for(p = 2; p < n + 1; p++)
    {
   
        // Checking whether the given number
        // is prime Tetradic or not
        if (prime[p] && isTetradic(p))
            cout << p << " ";
    }
}
 
// Driver code
int main()
{
    int n = 1000;
    printTetradicPrimesLessThanN(n);
 
    return 0;
}
 
// This code is contributed by divyeshrabadiya07


Java




// Java implementation to print all
// Tetradic primes smaller than or equal to N.
import java.util.*;
 
class GFG{
     
// Function to check if the number
// N having all digits lies in
// the set (0, 1, 8)
public static boolean isContaindigit(int n)
{
    while (n > 0)
    {
        if (!(n % 10 == 0 ||
              n % 10 == 1 ||
              n % 10 == 8))
            return false;
        n = n / 10;
    }
    return true;
}
 
// Function to check if the number
// N is palindrome
public static boolean ispalindrome(int n)
{
    String temp = Integer.toString(n);
    int l = temp.length();
     
    for(int i = 0; i < l / 2; i++)
    {
        if (temp.charAt(i) !=
            temp.charAt(l - i - 1))
            return false;
    }
    return true;
}
 
// Function to check if a number
// N is Tetradic
public static boolean isTetradic(int n)
{
    if (ispalindrome(n) && isContaindigit(n))
        return true;
    return false;
}
 
// Function to generate all primes and checking
// whether number is Tetradic or not
public static void printTetradicPrimesLessThanN(int n)
{
     
    // Create a boolean array "prime[0..n]" and
    // initialize all entries it as true. A value
    // in prime[i] will finally be false if i is
    // Not a prime, else true.
    boolean prime[] = new boolean[n + 1];
    Arrays.fill(prime, true);
     
    int p = 2;
     
    while (p * p <= n)
    {
         
        // If prime[p] is not changed,
        // then it is a prime
        if (prime[p])
        {
             
            // Update all multiples of p
            for(int i = p * 2; i < n + 1; i += p)
                prime[i] = false;
        }
        p += 1;
    }
 
    // Print all Tetradic prime numbers
    for(p = 2; p < n + 1; p++)
    {
 
        // Checking whether the given number
        // is prime Tetradic or not
        if (prime[p] && isTetradic(p))
            System.out.print(p + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 1000;
    printTetradicPrimesLessThanN(n);
}
}
 
// This code is contributed by jrishabh99


Python3




# Python3 implementation to print all
# Tetradic primes smaller than or equal to N. 
        
# Function to check if the number
# N having all digits lies in
# the set (0, 1, 8)
def isContaindigit(n):
    temp = str(n)
    for i in temp:
        if i not in ['0', '1', '8']:
            return False
    return True
  
# Function to check if the number
# N is palindrome
def ispalindrome(n):
    temp = str(n)
    if temp == temp[::-1]:
        return True
    return False
    
# Function to check if a number
# N is Tetradic  
def isTetradic(n):      
    if ispalindrome(n):
        if isContaindigit(n):
            return True
    return False
       
# Function to generate all primes and checking 
# whether number is Tetradic or not 
def printTetradicPrimesLessThanN(n):
       
    # Create a boolean array "prime[0..n]" and 
    # initialize all entries it as true. A value 
    # in prime[i] will finally be false if i is 
    # Not a prime, else true. 
    prime = [True] * (n + 1); 
    p = 2;
    while (p * p <= n):
           
        # If prime[p] is not changed, 
        # then it is a prime 
        if (prime[p]): 
               
            # Update all multiples of p 
            for i in range(p * 2, n + 1, p): 
                prime[i] = False;
        p += 1;
           
    # Print all Tetradic prime numbers 
    for p in range(2, n + 1): 
           
        # checking whether the given number 
        # is prime Tetradic or not 
        if (prime[p] and isTetradic(p)): 
            print(p, end = " "); 
       
# Driver Code 
n = 1000;
printTetradicPrimesLessThanN(n);


C#




// C# implementation to print all
// Tetradic primes smaller than
// or equal to N.
using System;
 
class GFG{
     
// Function to check if the number
// N having all digits lies in
// the set (0, 1, 8)
static bool isContaindigit(int n)
{
    while (n > 0)
    {
        if (!(n % 10 == 0 || 
              n % 10 == 1 || 
              n % 10 == 8))
            return false;
             
        n = n / 10;
    }
    return true;
}
   
// Function to check if the number
// N is palindrome
static bool ispalindrome(int n)
{
    string temp = n.ToString();
    int l = temp.Length;
       
    for(int i = 0; i < l / 2; i++)
    {
        if (temp[i] != temp[l - i - 1])
            return false;
    }
    return true;
}
   
// Function to check if a number
// N is Tetradic
static bool isTetradic(int n)
{
    if (ispalindrome(n) &&
        isContaindigit(n))
        return true;
         
    return false;
}
   
// Function to generate all primes and checking
// whether number is Tetradic or not
static void printTetradicPrimesLessThanN(int n)
{
     
    // Create a boolean array "prime[0..n]" and
    // initialize all entries it as true. A value
    // in prime[i] will finally be false if i is
    // Not a prime, else true.
    bool[] prime = new bool[n + 1];
    Array.Fill(prime, true);
       
    int p = 2;
       
    while (p * p <= n)
    {
         
        // If prime[p] is not changed,
        // then it is a prime
        if (prime[p]) 
        {
             
            // Update all multiples of p
            for(int i = p * 2; i < n + 1; i += p)
                prime[i] = false;
        }
        p += 1;
    }
   
    // Print all Tetradic prime numbers
    for(p = 2; p < n + 1; p++)
    {
         
        // Checking whether the given number
        // is prime Tetradic or not
        if (prime[p] && isTetradic(p))
            Console.Write(p + " ");
    }
}
 
// Driver code
static void Main()
{
    int n = 1000;
     
    printTetradicPrimesLessThanN(n);
}
}
 
// This code is contributed by divyesh072019


Javascript




<script>
 
    // Javascript implementation to print all
    // Tetradic primes smaller than
    // or equal to N.
     
    // Function to check if the number
    // N having all digits lies in
    // the set (0, 1, 8)
    function isContaindigit(n)
    {
        while (n > 0)
        {
            if (!(n % 10 == 0 ||
                  n % 10 == 1 ||
                  n % 10 == 8))
                return false;
 
            n = parseInt(n / 10, 10);
        }
        return true;
    }
 
    // Function to check if the number
    // N is palindrome
    function ispalindrome(n)
    {
        let temp = n.toString();
        let l = temp.length;
 
        for(let i = 0; i < parseInt(l / 2, 10); i++)
        {
            if (temp[i] != temp[l - i - 1])
                return false;
        }
        return true;
    }
 
    // Function to check if a number
    // N is Tetradic
    function isTetradic(n)
    {
        if (ispalindrome(n) &&
            isContaindigit(n))
            return true;
 
        return false;
    }
 
    // Function to generate all primes and checking
    // whether number is Tetradic or not
    function printTetradicPrimesLessThanN(n)
    {
 
        // Create a boolean array "prime[0..n]" and
        // initialize all entries it as true. A value
        // in prime[i] will finally be false if i is
        // Not a prime, else true.
        let prime = new Array(n + 1);
        prime.fill(true);
 
        let p = 2;
 
        while (p * p <= n)
        {
 
            // If prime[p] is not changed,
            // then it is a prime
            if (prime[p])
            {
 
                // Update all multiples of p
                for(let i = p * 2; i < n + 1; i += p)
                    prime[i] = false;
            }
            p += 1;
        }
 
        // Print all Tetradic prime numbers
        for(p = 2; p < n + 1; p++)
        {
 
            // Checking whether the given number
            // is prime Tetradic or not
            if (prime[p] && isTetradic(p))
                document.write(p + " ");
        }
    }
     
    let n = 1000;
      
    printTetradicPrimesLessThanN(n);
     
</script>


Output

11 101 181












Approach 2:

Another approach to find all tetradic primes less than or equal to N is to iterate through all odd numbers from 3 to N, and for each odd number check whether it is a prime and whether it is a tetradic number. To check whether a number is a prime, we can use a simple primality test such as trial division by odd numbers up to the square root of the number. To check whether a number is tetradic, we can convert it to a string and check whether all of its digits are 0, 1, or 8, and whether it is a palindrome.

Here are the codes for this approach:

C++




#include <bits/stdc++.h>
#include <cmath>
using namespace std;
 
bool is_prime(int n) {
    if (n < 2) {
        return false;
    }
    if (n == 2) {
        return true;
    }
    if (n % 2 == 0) {
        return false;
    }
    for (int i = 3; i <= sqrt(n); i += 2) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}
 
bool is_tetradic(int n) {
    string s = to_string(n);
    for (char c : s) {
        if (c != '0' && c != '1' && c != '8') {
            return false;
        }
    }
    string rev_s = s;
    reverse(rev_s.begin(), rev_s.end());
    return s == rev_s;
}
 
void print_tetradic_primes(int n) {
    for (int i = 3; i <= n; i += 2) {
        if (is_prime(i) && is_tetradic(i)) {
            cout << i << " ";
        }
    }
    cout << endl;
}
 
int main() {
    int n = 1000;
    print_tetradic_primes(n);
    return 0;
}


Java




import java.util.ArrayList;
import java.util.List;
 
public class GFG {
 
    private static boolean isPrime(int n) {
        if (n < 2) {
            return false;
        }
        if (n == 2) {
            return true;
        }
        if (n % 2 == 0) {
            return false;
        }
        for (int i = 3; i <= Math.sqrt(n); i += 2) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }
 
    private static boolean isTetradic(int n) {
        String s = Integer.toString(n);
        for (char c : s.toCharArray()) {
            if (c != '0' && c != '1' && c != '8') {
                return false;
            }
        }
        String rev_s = new StringBuilder(s).reverse().toString();
        return s.equals(rev_s);
    }
 
    private static void printTetradicPrimes(int n) {
        for (int i = 3; i <= n; i += 2) {
            if (isPrime(i) && isTetradic(i)) {
                System.out.print(i + " ");
            }
        }
        System.out.println();
    }
 
    public static void main(String[] args) {
        int n = 1000;
        printTetradicPrimes(n);
    }
}


Python3




import math
 
# Function to check if a number is prime
def is_prime(n):
    if n < 2:
        return False
    if n == 2:
        return True
    if n % 2 == 0:
        return False
    for i in range(3, int(math.sqrt(n)) + 1, 2):
        if n % i == 0:
            return False
    return True
 
# Function to check if a number is tetradic
def is_tetradic(n):
    s = str(n)
    for c in s:
        if c not in ['0', '1', '8']:
            return False
    rev_s = s[::-1]
    return s == rev_s
 
# Function to print tetradic prime numbers up to n
def print_tetradic_primes(n):
    for i in range(3, n + 1, 2):
        if is_prime(i) and is_tetradic(i):
 
            # Print tetradic prime number
            print(i, end=' ')
    print()
 
 
# Define the upper limit
n = 1000
 
# Call the function to print tetradic prime numbers
print_tetradic_primes(n)


C#




using System;
 
class GFG {
    public static bool IsPrime(int n) {
        if (n < 2) {
            return false;
        }
        if (n == 2) {
            return true;
        }
        if (n % 2 == 0) {
            return false;
        }
        for (int i = 3; i <= Math.Sqrt(n); i += 2) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }
 
    public static bool IsTetradic(int n) {
        string s = n.ToString();
        foreach (char c in s) {
            if (c != '0' && c != '1' && c != '8') {
                return false;
            }
        }
        char[] arr = s.ToCharArray();
        Array.Reverse(arr);
        string rev_s = new string(arr);
        return s == rev_s;
    }
 
    public static void PrintTetradicPrimes(int n) {
        for (int i = 3; i <= n; i += 2) {
            if (IsPrime(i) && IsTetradic(i)) {
                Console.Write(i + " ");
            }
        }
        Console.WriteLine();
    }
 
    public static void Main(string[] args) {
        int n = 1000;
        PrintTetradicPrimes(n);
    }
}


Javascript




function isPrime(n) {
    if (n < 2) {
        return false;
    }
    if (n === 2) {
        return true;
    }
    if (n % 2 === 0) {
        return false;
    }
    for (let i = 3; i <= Math.sqrt(n); i += 2) {
        if (n % i === 0) {
            return false;
        }
    }
    return true;
}
 
function isTetradic(n) {
    const s = n.toString();
    for (const c of s) {
        if (c !== '0' && c !== '1' && c !== '8') {
            return false;
        }
    }
    const revS = s.split('').reverse().join('');
    return s === revS;
}
 
function printTetradicPrimes(limit) {
    const result = [];
    for (let i = 3; i <= limit; i += 2) {
        if (isPrime(i) && isTetradic(i)) {
            result.push(i);
        }
    }
    console.log(result.join(' '));
}
 
const n = 1000;
printTetradicPrimes(n);


Output

11 101 181 












Time complexity: O(N^(3/2)), because we iterate through all odd numbers up to N .
Space complexity: O(log(N)), because we need to store the string representation of the number to check whether it is tetradic.’

Approach: Constant Space:

In this Approach the boolean array prime[ ] has been removed. Instead, each number is checked for primality using a loop that iterates from 2 to the square root of the number. The space complexity remains constant because no additional data structures are used, and the calculations are performed using a fixed number of variables.

Here is the code of above approach:

C++




#include <iostream>
#include <cmath>
using namespace std;
 
bool isContaindigit(int n)
{
    while (n > 0)
    {
        if (!(n % 10 == 0 || n % 10 == 1 || n % 10 == 8))
            return false;
             
        n = n / 10;
    }
    return true;
}
 
bool ispalindrome(int n)
{
    int temp = n;
    int reverse = 0;
     
    while (temp > 0)
    {
        reverse = reverse * 10 + temp % 10;
        temp = temp / 10;
    }
     
    return (n == reverse);
}
 
bool isTetradic(int n)
{
    return (ispalindrome(n) && isContaindigit(n));
}
 
void printTetradicPrimesLessThanN(int n)
{
    for (int p = 2; p <= n; p++)
    {
        if (isTetradic(p))
        {
            bool isPrime = true;
             
            for (int i = 2; i <= sqrt(p); i++)
            {
                if (p % i == 0)
                {
                    isPrime = false;
                    break;
                }
            }
             
            if (isPrime)
                cout << p << " ";
        }
    }
}
 
int main()
{
    int n = 1000;
    printTetradicPrimesLessThanN(n);
 
    return 0;
}


Java




import java.util.Scanner;
 
public class TetradicPrimes {
 
    // Function to check if a number contains only the
    // digits 0, 1, or 8
    private static boolean isContainDigit(int n)
    {
        while (n > 0) {
            if (!(n % 10 == 0 || n % 10 == 1
                  || n % 10 == 8)) {
                return false;
            }
            n /= 10;
        }
        return true;
    }
 
    // Function to check if a number is a palindrome
    private static boolean isPalindrome(int n)
    {
        int temp = n;
        int reverse = 0;
        while (temp > 0) {
            reverse = reverse * 10 + temp % 10;
            temp /= 10;
        }
        return n == reverse;
    }
 
    // Function to check if a number is Tetradic (palindrome
    // and contains only 0, 1, or 8)
    private static boolean isTetradic(int n)
    {
        return isPalindrome(n) && isContainDigit(n);
    }
 
    // Function to print Tetradic Prime numbers less than a
    // given value 'n'
    private static void printTetradicPrimesLessThanN(int n)
    {
        for (int p = 2; p <= n; p++) {
            if (isTetradic(p)) {
                boolean isPrime = true;
                // Checking for prime numbers
                for (int i = 2; i <= Math.sqrt(p); i++) {
                    if (p % i == 0) {
                        isPrime = false;
                        break;
                    }
                }
                // If the number is prime, print it
                if (isPrime) {
                    System.out.print(p + " ");
                }
            }
        }
    }
 
    public static void main(String[] args)
    {
        // Define the value of 'n'
        int n = 1000;
        // Call the function to print Tetradic Prime numbers
        // less than 'n'
        printTetradicPrimesLessThanN(n);
    }
}


Python3




# Function to check if a number contains only the digits 0, 1, or 8
def isContaindigit(n):
    while n > 0:
        if not (n % 10 == 0 or n % 10 == 1 or n % 10 == 8):
            return False
        n //= 10
    return True
 
# Function to check if a number is a palindrome
def ispalindrome(n):
    temp = n
    reverse = 0
    while temp > 0:
        reverse = reverse * 10 + temp % 10
        temp //= 10
    return n == reverse
 
# Function to check if a number is Tetradic (palindrome and contains only 0, 1, or 8)
def isTetradic(n):
    return ispalindrome(n) and isContaindigit(n)
 
# Function to print Tetradic Prime numbers less than a given value 'n'
def printTetradicPrimesLessThanN(n):
    for p in range(2, n + 1):
        if isTetradic(p):
            isPrime = True
            # Checking for prime numbers
            for i in range(2, int(p ** 0.5) + 1):
                if p % i == 0:
                    isPrime = False
                    break
            # If the number is prime, print it
            if isPrime:
                print(p, end=" ")
 
# Define the value of 'n'
n = 1000
# Call the function to print Tetradic Prime numbers less than 'n'
printTetradicPrimesLessThanN(n)


C#




using System;
 
class Program
{
    static bool IsContainDigit(int n)
    {
        while (n > 0)
        {
            if (!(n % 10 == 0 || n % 10 == 1 || n % 10 == 8))
                return false;
 
            n = n / 10;
        }
        return true;
    }
 
    static bool IsPalindrome(int n)
    {
        int temp = n;
        int reverse = 0;
 
        while (temp > 0)
        {
            reverse = reverse * 10 + temp % 10;
            temp = temp / 10;
        }
 
        return (n == reverse);
    }
 
    static bool IsTetradic(int n)
    {
        return (IsPalindrome(n) && IsContainDigit(n));
    }
 
    static void PrintTetradicPrimesLessThanN(int n)
    {
        for (int p = 2; p <= n; p++)
        {
            if (IsTetradic(p))
            {
                bool isPrime = true;
 
                for (int i = 2; i <= Math.Sqrt(p); i++)
                {
                    if (p % i == 0)
                    {
                        isPrime = false;
                        break;
                    }
                }
 
                if (isPrime)
                    Console.Write(p + " ");
            }
        }
    }
 
    static void Main()
    {
        int n = 1000;
        PrintTetradicPrimesLessThanN(n);
 
        Console.ReadLine(); // To keep the console window open
    }
}


Javascript




// Function to check if a number contains only the digits 0, 1, or 8
function isContaindigit(n) {
    while (n > 0) {
        if (!(n % 10 === 0 || n % 10 === 1 || n % 10 === 8)) {
            return false;
        }
        n = Math.floor(n / 10);
    }
    return true;
}
 
// Function to check if a number is a palindrome
function ispalindrome(n) {
    let temp = n;
    let reverse = 0;
    while (temp > 0) {
        reverse = reverse * 10 + temp % 10;
        temp = Math.floor(temp / 10);
    }
    return n === reverse;
}
 
// Function to check if a number is Tetradic (palindrome and contains only 0, 1, or 8)
function isTetradic(n) {
    return ispalindrome(n) && isContaindigit(n);
}
 
// Function to print Tetradic Prime numbers less than a given value 'n'
function printTetradicPrimesLessThanN(n) {
    for (let p = 2; p <= n; p++) {
        if (isTetradic(p)) {
            let isPrime = true;
            // Checking for prime numbers
            for (let i = 2; i <= Math.floor(Math.sqrt(p)); i++) {
                if (p % i === 0) {
                    isPrime = false;
                    break;
                }
            }
            // If the number is prime, print it
            if (isPrime) {
                process.stdout.write(p + " ");
            }
        }
    }
}
 
// Define the value of 'n'
let n = 1000;
// Call the function to print Tetradic Prime numbers less than 'n'
printTetradicPrimesLessThanN(n);


Output

11 101 181 












Time complexity: O(N^(3/2)), because we iterate through all odd numbers up to N .
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads