Honaker Prime Number | C ++ program for the above approach ; Function to precompute the position of every prime number using Sieve ; 0 and 1 are not prime numbers ; Variable to store the position ; Incrementing the position for every prime number ; Function to get sum of digits ; Function to check whether the given number is Honaker Prime number or not ; Driver Code ; Precompute the prime numbers till 10 ^ 6 ; Given Number ; Function Call
#include <bits/stdc++.h> NEW_LINE #define limit  10000000 NEW_LINE 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 " ; }