primos tetradicos

Los primos tetrádicos son números primos que también son números tetrádicos.

Un número tetrádico es un número palindrómico que contiene solo 0, 1 y 8 como dígitos en el número. 
 

Encuentra los números primos tetradicos menores que N

Dado un número N , la tarea es imprimir todos los números primos tetrádicos menores o iguales que N.

Ejemplos:

Entrada: N = 20 
Salida: 11

Entrada: N = 200 
Salida: 11 101 181 

Enfoque: La idea es generar todos los números primos menores o iguales que el número N dado y verificar cada número primo si es tetrádico o no.

  • Para encontrar si un número dado es primo o no usando el método de criba de eratóstenes.
  • Para verificar si el número dado es un número tetrádico o no, verifique que el número sea un palindrómico o no y que solo contenga dígitos como 0, 1 y 8.

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

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>
Producción: 

11 101 181

 

Publicación traducida automáticamente

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