Honaker Prime Number
Last Updated :
24 Mar, 2021
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++
#include <bits/stdc++.h>
#define limit 10000000
using namespace std;
int position[limit + 1];
void sieve()
{
position[0] = -1, position[1] = -1;
int pos = 0;
for ( int i = 2; i <= limit; i++) {
if (position[i] == 0) {
position[i] = ++pos;
for ( int j = i * 2; j <= limit; j += i)
position[j] = -1;
}
}
}
int getSum( int n)
{
int sum = 0;
while (n != 0) {
sum = sum + n % 10;
n = n / 10;
}
return sum;
}
bool isHonakerPrime( int n)
{
int pos = position[n];
if (pos == -1)
return false ;
return getSum(n) == getSum(pos);
}
int main()
{
sieve();
int N = 121;
if (isHonakerPrime(N))
cout << "Yes" ;
else
cout << "No" ;
}
|
Java
class GFG{
static final int limit = 10000000 ;
static int []position = new int [limit + 1 ];
static void sieve()
{
position[ 0 ] = - 1 ;
position[ 1 ] = - 1 ;
int pos = 0 ;
for ( int i = 2 ; i <= limit; i++)
{
if (position[i] == 0 )
{
position[i] = ++pos;
for ( int j = i * 2 ; j <= limit; j += i)
position[j] = - 1 ;
}
}
}
static int getSum( int n)
{
int sum = 0 ;
while (n != 0 )
{
sum = sum + n % 10 ;
n = n / 10 ;
}
return sum;
}
static boolean isHonakerPrime( int n)
{
int pos = position[n];
if (pos == - 1 )
return false ;
return getSum(n) == getSum(pos);
}
public static void main(String[] args)
{
sieve();
int N = 121 ;
if (isHonakerPrime(N))
System.out.print( "Yes\n" );
else
System.out.print( "No\n" );
}
}
|
Python3
limit = 10000000
position = [ 0 ] * (limit + 1 )
def sieve():
position[ 0 ] = - 1
position[ 1 ] = - 1
pos = 0
for i in range ( 2 , limit + 1 ):
if (position[i] = = 0 ):
pos + = 1
position[i] = pos
for j in range (i * 2 , limit + 1 , i):
position[j] = - 1
def getSum(n):
Sum = 0
while (n ! = 0 ):
Sum = Sum + n % 10
n = n / / 10
return Sum
def isHonakerPrime(n):
pos = position[n]
if (pos = = - 1 ):
return False
return bool (getSum(n) = = getSum(pos))
sieve()
N = 121
if (isHonakerPrime(N)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static readonly int limit = 10000000;
static int []position = new int [limit + 1];
static void sieve()
{
position[0] = -1;
position[1] = -1;
int pos = 0;
for ( int i = 2; i <= limit; i++)
{
if (position[i] == 0)
{
position[i] = ++pos;
for ( int j = i * 2; j <= limit; j += i)
position[j] = -1;
}
}
}
static int getSum( int n)
{
int sum = 0;
while (n != 0)
{
sum = sum + n % 10;
n = n / 10;
}
return sum;
}
static bool isHonakerPrime( int n)
{
int pos = position[n];
if (pos == -1)
return false ;
return getSum(n) == getSum(pos);
}
public static void Main(String[] args)
{
sieve();
int N = 121;
if (isHonakerPrime(N))
Console.Write( "Yes\n" );
else
Console.Write( "No\n" );
}
}
|
Javascript
<script>
const limit = 10000000;
let position = Array(limit + 1).fill(0);
function sieve()
{
position[0] = -1;
position[1] = -1;
let pos = 0;
for (let i = 2; i <= limit; i++)
{
if (position[i] == 0)
{
position[i] = ++pos;
for (let j = i * 2; j <= limit; j += i)
position[j] = -1;
}
}
}
function getSum( n) {
let sum = 0;
while (n != 0) {
sum = sum + n % 10;
n = parseInt(n / 10);
}
return sum;
}
function isHonakerPrime( n) {
let pos = position[n];
if (pos == -1)
return false ;
return getSum(n) == getSum(pos);
}
sieve();
let N = 121;
if (isHonakerPrime(N))
document.write( "Yes\n" );
else
document.write( "No\n" );
</script>
|
Reference: https://oeis.org/A033548
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...