📜  Honaker素数

📅  最后修改于: 2021-05-05 03:22:40             🧑  作者: Mango

Honaker素数是素数P,使得P的数字和总和P的指数的数字之和是一个素数。
很少有Honaker素数是:

检查N是否为Honaker素数

给定整数N ,任务是检查N是否为Honaker质数。如果NHonaker质数,则打印“是”,否则打印“否”

例子:

方法:想法是找到给定数字的索引,并检查索引和N的位数之和是否相同。如果相同,则NHonaker质数,并打印“是”,否则打印“否”

C++
// C++ program for the above approach
#include 
#define limit 10000000
using namespace std;
 
int position[limit + 1];
 
// Function to precompute the position
// of every prime number using Sieve
void sieve()
{
    // 0 and 1 are not prime numbers
    position[0] = -1, position[1] = -1;
 
    // Variable to store the position
    int pos = 0;
 
    for (int i = 2; i <= limit; i++) {
 
        if (position[i] == 0) {
 
            // Incrementing the position for
            // every prime number
            position[i] = ++pos;
            for (int j = i * 2; j <= limit; j += i)
                position[j] = -1;
        }
    }
}
 
// Function to get sum of digits
int getSum(int n)
{
    int sum = 0;
    while (n != 0) {
        sum = sum + n % 10;
        n = n / 10;
    }
    return sum;
}
 
// Function to check whether the given number
// is Honaker Prime number or not
bool isHonakerPrime(int n)
{
    int pos = position[n];
    if (pos == -1)
        return false;
    return getSum(n) == getSum(pos);
}
 
// Driver Code
int main()
{
    // Precompute the prime numbers till 10^6
    sieve();
 
    // Given Number
    int N = 121;
 
    // Function Call
    if (isHonakerPrime(N))
        cout << "Yes";
    else
        cout << "No";
}


Java
// Java program for above approach
class GFG{
 
static final int limit = 10000000;
static int []position = new int[limit + 1];
     
// Function to precompute the position
// of every prime number using Sieve
static void sieve()
{
    // 0 and 1 are not prime numbers
    position[0] = -1;
    position[1] = -1;
     
    // Variable to store the position
    int pos = 0;
    for (int i = 2; i <= limit; i++)
    {
        if (position[i] == 0)
        {
     
            // Incrementing the position for
            // every prime number
            position[i] = ++pos;
            for (int j = i * 2; j <= limit; j += i)
                position[j] = -1;
        }
    }
}
 
// Function to get sum of digits
static int getSum(int n)
{
    int sum = 0;
    while (n != 0)
    {
        sum = sum + n % 10;
        n = n / 10;
    }
    return sum;
}
 
// Function to check whether the given number
// is Honaker Prime number or not
static boolean isHonakerPrime(int n)
{
    int pos = position[n];
    if (pos == -1)
        return false;
    return getSum(n) == getSum(pos);
}
 
// Driver code
public static void main(String[] args)
{
    // Precompute the prime numbers till 10^6
    sieve();
 
    // Given Number
    int N = 121;
 
    // Function Call
    if (isHonakerPrime(N))
        System.out.print("Yes\n");
    else
        System.out.print("No\n");
}
}
 
// This code is contributed by shubham


Python3
# Python3 program for the above approach
limit = 10000000
 
position = [0] * (limit + 1)
 
# Function to precompute the position
# of every prime number using Sieve
def sieve():
     
    # 0 and 1 are not prime numbers
    position[0] = -1
    position[1] = -1
 
    # Variable to store the position
    pos = 0
 
    for i in range(2, limit + 1):
        if (position[i] == 0):
             
            # Incrementing the position for
            # every prime number
            pos += 1
            position[i] = pos
             
            for j in range(i * 2, limit + 1, i):
                position[j] = -1
 
# Function to get sum of digits
def getSum(n):
 
    Sum = 0
     
    while (n != 0):
        Sum = Sum + n % 10
        n = n // 10
  
    return Sum
 
# Function to check whether the given
# number is Honaker Prime number or not
def isHonakerPrime(n):
 
    pos = position[n]
     
    if (pos == -1):
        return False
         
    return bool(getSum(n) == getSum(pos))
 
# Driver code
 
# Precompute the prime numbers till 10^6
sieve()
 
# Given Number
N = 121
 
# Function Call
if (isHonakerPrime(N)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by divyeshrabadiya07


C#
// C# program for above approach
using System;
class GFG{
 
static readonly int limit = 10000000;
static int []position = new int[limit + 1];
     
// Function to precompute the position
// of every prime number using Sieve
static void sieve()
{
    // 0 and 1 are not prime numbers
    position[0] = -1;
    position[1] = -1;
     
    // Variable to store the position
    int pos = 0;
    for (int i = 2; i <= limit; i++)
    {
        if (position[i] == 0)
        {
     
            // Incrementing the position for
            // every prime number
            position[i] = ++pos;
            for (int j = i * 2; j <= limit; j += i)
                position[j] = -1;
        }
    }
}
 
// Function to get sum of digits
static int getSum(int n)
{
    int sum = 0;
    while (n != 0)
    {
        sum = sum + n % 10;
        n = n / 10;
    }
    return sum;
}
 
// Function to check whether the given number
// is Honaker Prime number or not
static bool isHonakerPrime(int n)
{
    int pos = position[n];
    if (pos == -1)
        return false;
    return getSum(n) == getSum(pos);
}
 
// Driver code
public static void Main(String[] args)
{
    // Precompute the prime numbers till 10^6
    sieve();
 
    // Given Number
    int N = 121;
 
    // Function Call
    if (isHonakerPrime(N))
        Console.Write("Yes\n");
    else
        Console.Write("No\n");
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
No

参考: https : //oeis.org/A033548