Honaker Prime Number

Honaker Prime Number is a prime number P such that the sum of digits of P and sum of digits of index of P is a Prime Number.
Few Honaker Prime Numbers are: 

131, 263, 457, 1039, 1049, 1091, 1301, 1361, 1433, 1571, 1913, 1933, 2141, 2221,… 

Check if N is a Honaker Prime Number

Given an integer N, the task is to check if N is a Honaker Prime Number or not. If N is an Honaker Prime Number then print “Yes” else print “No”.

Examples: 

Input: N = 131 
Output: Yes 
Explanation: 
Sum of digits of 131 = 1 + 3 + 1 = 5 
Sum of digits of 32 = 3 + 2 = 5



Input: N = 161 
Output: No 

Approach: The idea is to find the index of the given number and check if sum of digits of index and N is the same or not. If it is same then, N is an Honaker Prime Number and print “Yes” else print “No”.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
#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";
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output: 

No



 

Reference: https://oeis.org/A033548
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.