📜  四元质数

📅  最后修改于: 2021-04-21 23:17:37             🧑  作者: Mango

四元素数是质数,也是四元数。

查找小于N的四元素数

给定数字N ,任务是打印所有小于或等于N的四面体素数。

例子:

方法:想法是生成所有小于或等于给定数N的质数,并检查每个质数是否为四元数。

  • 使用筛网筛法查找给定数字是否为质数。
  • 要检查给定的数字是否为四进制数,请检查该数字是否为回文数,并且仅包含数字0、1、8。

下面是上述算法的实现:

C++
// C++ implementation to print all
// Tetradic primes smaller than or
// equal to N.
#include 
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


输出:
11 101 181