Check whether N is a Dihedral Prime Number or not - GeeksforGeeks

21 May, 2021
Given an integer N, the task is to check if N is a Dihedral prime number or not. A Dihedral prime is a prime number that can be read as itself or as another prime number when read in a seven-segment display, regardless of different orientation and surface.
Examples: 
Input: N = 108881 Output: Yes
Input: N = 789 Output: No 
Approach: Pre-calculate prime number sieve for primality testing. The Sieves of Eratosthenes can be calculated in n*logn*logn time. Run a primality test for the number and its different orientations. If the number passes the primality tests, check if any digits belong to the exclusion set [3, 4, 6, 7, 9]. The return is true if the number passes both tests.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the above approach#include <bits/stdc++.h>using namespace std; bool isPrime[int(1e5) + 5]; // Function to return the reverse// of a numberint reverse(int n){    int temp = n;    int sum = 0;    while (temp > 0) {        int rem = temp % 10;        sum = sum * 10 + rem;        temp /= 10;    }    return sum;} // Function to generate mirror reflection// of a numberint mirror(int n){    int temp = n;    int sum = 0;    while (temp > 0) {        int rem = temp % 10;        if (rem == 2)            rem = 5;        else if (rem == 5)            rem = 2;        sum = sum * 10 + rem;        temp /= 10;    }    return sum;} // Function to initialize prime number sievebool sieve(){    memset(isPrime, true, sizeof isPrime);     isPrime[0] = isPrime[1] = false;     for (int i = 2; i <= int(1e5); i++) {        for (int j = 2; i * j <= int(1e5); j++) {            isPrime[i * j] = false;        }    }} // Function that returns true if n is// Dihedral Prime numberbool isDihedralPrime(int n){    // Check if the orientations of n    // is also prime    if (!isPrime[n]        || !isPrime[mirror(n)]        || !isPrime[reverse(n)]        || !isPrime[reverse(mirror(n))])        return false;     int temp = n;     while (temp > 0) {        int rem = temp % 10;        if (rem == 3 || rem == 4 || rem == 6            || rem == 7 || rem == 9)            return false;        temp /= 10;    }     return true;} // Driver codeint main(){    sieve();     int n = 18181;    if (isDihedralPrime(n))        cout << "Yes";    else        cout << "No";     return 0;}
// Java implementation of the above approachimport java.util.*; class GFG{     static boolean[] isPrime = new boolean[(int) (1e5) + 5];     // Function to return the reverse    // of a number    static int reverse(int n)    {        int temp = n;        int sum = 0;        while (temp > 0)        {            int rem = temp % 10;            sum = sum * 10 + rem;            temp /= 10;        }        return sum;    }     // Function to generate mirror reflection    // of a number    static int mirror(int n)    {        int temp = n;        int sum = 0;        while (temp > 0)        {            int rem = temp % 10;            if (rem == 2)            {                rem = 5;            }                         else if (rem == 5)            {                rem = 2;            }            sum = sum * 10 + rem;            temp /= 10;        }        return sum;    }     // Function to initialize    // prime number sieve    static void sieve()    {        Arrays.fill(isPrime, true);         isPrime[0] = isPrime[1] = false;         for (int i = 2;                 i <= (int) 1e5; i++)        {            for (int j = 2;                     i * j <= (int) 1e5; j++)            {                isPrime[i * j] = false;            }        }    }     // Function that returns true if n is    // Dihedral Prime number    static boolean isDihedralPrime(int n)    {                 // Check if the orientations of n        // is also prime        if (!isPrime[n] ||            !isPrime[mirror(n)] ||            !isPrime[reverse(n)] ||            !isPrime[reverse(mirror(n))])        {            return false;        }         int temp = n;         while (temp > 0)        {            int rem = temp % 10;            if (rem == 3 || rem == 4 ||                rem == 6 || rem == 7 ||                rem == 9)            {                return false;            }            temp /= 10;        }         return true;    }     // Driver code    public static void main(String[] args)    {        sieve();         int n = 18181;        if (isDihedralPrime(n))        {            System.out.println("Yes");        }        else        {            System.out.println("No");        }    }} // This code is contributed by Rajput-Ji
# Python implementation of the above approachisPrime = (int(1e5)+5)*[True] # Function to return the reverse# of a numberdef reverse(n):    temp = n    sum = 0    while temp>0:        rem = temp % 10        sum = sum * 10 + rem        temp//= 10     return sum # Function to generate mirror reflection# of a numberdef mirror(n):    temp = n    sum = 0    while temp>0:        rem = temp % 10        if rem == 2:            rem = 5        elif rem == 5:            rem = 2        sum = sum * 10 + rem        temp//= 10     return sum # Function to initialize prime number sievedef sieve():     isPrime[0] = isPrime[1] = False     for i in range(2, int(1e5)+1):        j = 2        while i * j<= int(1e5):            isPrime[i * j] = False            j+= 1  # Function that returns true if n is# Dihedral Prime numberdef isDihedralPrime(n):         # Check if the orientations of n is also prime    if (not isPrime[n]) or (not isPrime[mirror(n)]) \        or (not isPrime[reverse(n)]) \        or (not isPrime[reverse(mirror(n))]):        return False     temp = n     while temp>0:        rem = temp % 10;        if rem == 3 or rem == 4 or \            rem == 6 or rem == 7 or rem == 9:            return False        temp//= 10     return True # Driver codeif __name__ == '__main__':     sieve()         n = 18181    if isDihedralPrime(n):        print("Yes")    else :        print("No")
// C# implementation of the above approachusing System;     class GFG{    static Boolean[] isPrime = new Boolean[(int) (1e5) + 5];     // Function to return the reverse    // of a number    static int reverse(int n)    {        int temp = n;        int sum = 0;        while (temp > 0)        {            int rem = temp % 10;            sum = sum * 10 + rem;            temp /= 10;        }        return sum;    }     // Function to generate mirror reflection    // of a number    static int mirror(int n)    {        int temp = n;        int sum = 0;        while (temp > 0)        {            int rem = temp % 10;            if (rem == 2)            {                rem = 5;            }                         else if (rem == 5)            {                rem = 2;            }            sum = sum * 10 + rem;            temp /= 10;        }        return sum;    }     // Function to initialize    // prime number sieve    static void sieve()    {        for(int k = 0; k < isPrime.Length; k++)            isPrime[k] = true;         isPrime[0] = isPrime[1] = false;         for (int i = 2;                 i <= (int) 1e5; i++)        {            for (int j = 2;                     i * j <= (int) 1e5; j++)            {                isPrime[i * j] = false;            }        }    }     // Function that returns true if n is    // Dihedral Prime number    static Boolean isDihedralPrime(int n)    {                 // Check if the orientations of n        // is also prime        if (!isPrime[n] ||            !isPrime[mirror(n)] ||            !isPrime[reverse(n)] ||            !isPrime[reverse(mirror(n))])        {            return false;        }         int temp = n;         while (temp > 0)        {            int rem = temp % 10;            if (rem == 3 || rem == 4 ||                rem == 6 || rem == 7 ||                rem == 9)            {                return false;            }            temp /= 10;        }         return true;    }     // Driver code    public static void Main(String[] args)    {        sieve();         int n = 18181;        if (isDihedralPrime(n))        {            Console.WriteLine("Yes");        }        else        {            Console.WriteLine("No");        }    }} // This code is contributed by Rajput-Ji
<?php// PHP implementation of the above approach$isPrime = array_fill(0, 25000 , true); // Function to return the reverse// of a numberfunction reverse($n){    $temp = $n;    $sum = 0;    while ($temp > 0)    {        $rem = $temp % 10;        $sum = $sum * 10 + $rem;        $temp = floor($temp / 10);    }    return $sum;} // Function to generate mirror reflection// of a numberfunction mirror($n){    $temp = $n;    $sum = 0;    while ($temp > 0)    {        $rem = $temp % 10;        if ($rem == 2)            $rem = 5;        else if ($rem == 5)            $rem = 2;        $sum = $sum * 10 + $rem;        $temp = floor($temp / 10);    }    return $sum;} // Function to initialize prime number sievefunction sieve(){    $GLOBALS['isPrime'][0] = $GLOBALS['isPrime'][1] = false;     for ($i = 2; $i <= floor(1e4); $i++)    {        for ($j = 2; $i * $j <= floor(1e4); $j++)        {            $GLOBALS['isPrime'][$i * $j] = false;        }    }} // Function that returns true if n is// Dihedral Prime numberfunction isDihedralPrime($n){    // Check if the orientations of n    // is also prime    if (!$GLOBALS['isPrime'][$n] ||        !$GLOBALS['isPrime'][mirror($n)] ||        !$GLOBALS['isPrime'][reverse($n)] ||        !$GLOBALS['isPrime'][reverse(mirror($n))])        return false;     $temp = $n;     while ($temp > 0)    {        $rem = $temp % 10;        if ($rem == 3 || $rem == 4 ||            $rem == 6 || $rem == 7 || $rem == 9)            return false;                     $temp = floor($temp / 10);    }     return true;} // Driver codesieve(); $n = 18181;if (isDihedralPrime($n))    echo "Yes";else    echo "No"; // This code is contributed by Ryuga?>
<script>// Javascript implementation of the above approachlet isPrime = new Array(25000).fill(true); // Function to return the reverse// of a numberfunction reverse(n){    let temp = n;    let sum = 0;    while (temp > 0)    {        rem = temp % 10;        sum = sum * 10 + rem;        temp = Math.floor(temp / 10);    }    return sum;} // Function to generate mirror reflection// of a numberfunction mirror(n){    let temp = n;    let sum = 0;    while (temp > 0)    {        let rem = temp % 10;        if (rem == 2)            rem = 5;        else if (rem == 5)            rem = 2;        sum = sum * 10 + rem;        temp = Math.floor(temp / 10);    }    return sum;} // Function to initialize prime number sievefunction sieve(){    isPrime[0] = isPrime[1] = false;     for (let i = 2; i <= Math.floor(1e4); i++)    {        for (let j = 2; i * j <= Math.floor(1e4); j++)        {            isPrime[i * j] = false;        }    }} // Function that returns true if n is// Dihedral Prime numberfunction isDihedralPrime(n){    // Check if the orientations of n    // is also prime    if (!isPrime[n] ||        !isPrime[mirror(n)] ||        !isPrime[reverse(n)] ||        !isPrime[reverse(mirror(n))])        return false;     let temp = n;     while (temp > 0)    {        rem = temp % 10;        if (rem == 3 || rem == 4 ||            rem == 6 || rem == 7 || rem == 9)            return false;                     temp = Math.floor(temp / 10);    }     return true;} // Driver codesieve(); let n = 18181;if (isDihedralPrime(n))    document.write( "Yes");else    document.write( "No"); // This code is contributed by Saurabh Jaiswal</script>
Yes
 
Time Complexity: O(n * log(log n))
Auxiliary Space: O(105)
ankthon
Rajput-Ji
subhammahato348
_saurabh_jaiswal
number-theory
Prime Number
sieve
Algorithms
Combinatorial
number-theory
Prime Number
sieve
Combinatorial
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
DSA Sheet by Love Babbar
Difference between Informed and Uninformed Search in AI
SCAN (Elevator) Disk Scheduling Algorithms
Quadratic Probing in Hashing
K means Clustering - Introduction
Write a program to print all permutations of a given string
Permutation and Combination in Python
itertools.combinations() module in Python to print all possible combinations
Factorial of a large number
Program to calculate value of nCr