Twisted Prime Number

16 Mar, 2022
A number is said to be twisted prime if it is a prime number and reverse of the number is also a prime number.Examples:  
Input : 97
Output : Twisted Prime Number
Explanation: 97 is a prime number
and its reverse 79 is also a prime
number.

Input : 43
Output : Not a Twisted Prime Number
Explanation: 43 is a prime number
but its reverse 34 is not a prime
number.
 
The idea is first check if n is a prime number then reverse the number n and check reversed n is a prime number or not.We use methods of below two articles. 1) Check if a number is prime (School Method) 2) Reverse digits of a number. 
C++
Java
Python3
C#
PHP
Javascript
// C/C++ program to check if a given number// is Twisted Prime or not#include <bits/stdc++.h>using namespace std; // Returns reverse of nint reverse(int n){    int rev = 0, r;    while (n > 0) {        r = n % 10;        rev = rev * 10 + r;        n /= 10;    }    return rev;} // Returns true if n is primebool isPrime(int n){    // Corner cases    if (n <= 1)        return false;    if (n <= 3)        return true;     // This is checked so that we can skip    // middle five numbers in below loop    if (n % 2 == 0 || n % 3 == 0)        return false;     for (int i = 5; i * i <= n; i = i + 6)        if (n % i == 0 || n % (i + 2) == 0)            return false;     return true;} // function to check Twisted Prime numberbool checkTwistedPrime(int n){    if (isPrime(n) == false)        return false;     return isPrime(reverse(n));} // Driver Codeint main(void){    // Printing Twisted Prime numbers upto 200    cout << "First few Twisted Prime numbers are :- n";    for (int i = 2; i <= 200; i++)        if (checkTwistedPrime(i))            cout << i << " ";     return 0;}// This code is contributed by Nikita Tiwari
// Java program to check if a given number// is Twisted Prime or notimport java.io.*;import java.math.*; class GFG{    static int reverse(int n)    {        int rev = 0, r;        while (n > 0)        {            r = n % 10;            rev = rev * 10 + r;            n /= 10;        }        return rev;    }    static boolean isPrime(int n)    {        // Corner cases        if (n <= 1)            return false;        if (n <= 3)            return true;         // This is checked so that we can skip        // middle five numbers in below loop        if (n % 2 == 0 || n % 3 == 0)            return false;         for (int i = 5; i * i <= n; i = i + 6)            if (n % i == 0 || n % (i + 2) == 0)                return false;         return true;    }     // function to check Twisted Prime Number    static boolean checkTwistedPrime(int n)    {        if (isPrime(n) == false)            return false;         return isPrime(reverse(n));    }     // Driver Code    public static void main(String args[])    throws IOException    {        // Printing Twisted Prime Numbers upto 200        System.out.println("First few Twisted Prime" +        " Numbers are :- n");        for (int i = 2; i <= 200; i++)            if (checkTwistedPrime(i))                System.out.print(i + " ");    }}// This code is contributed by Nikita Tiwari.
# Python program to check if a given number# is Twisted Prime or not def reverse(n) :    rev = 0     # reversing the number    while n > 0 :        r = n % 10        rev = rev * 10 + r        n = n // 10    return rev # Returns true if n is prime, else falsedef isPrime(n) :     # Corner cases    if (n <= 1):        return False    if (n <= 3):        return True     # This is checked so that we can skip    # middle five numbers in below loop    if (n % 2 == 0 or n % 3 == 0):        return False     i = 5    while (i * i <= n):        if (n % i == 0 or n % (i + 2) == 0):            return False        i = i + 6     return True; # function to check Twisted Prime numberdef checkTwistedPrime (n) :    if (isPrime(n) == False):        return False     return isPrime(reverse(n)) # Driver Code# Printing Twisted Prime numbers upto 200print ("First few Twisted Prime numbers are :- ")i = 2while i<= 200 :    if (checkTwistedPrime(i) == True) :        print(i,end=" ")    i = i + 1  # This code is contributed by Nikita Tiwari.
// C# program to check if a given// number is Twisted Prime or notusing System; class GFG{static int reverse(int n){    int rev = 0, r;    while (n > 0)    {        r = n % 10;        rev = rev * 10 + r;        n /= 10;    }    return rev;} static bool isPrime(int n){    // Corner cases    if (n <= 1)        return false;    if (n <= 3)        return true;     // This is checked so that    // we can skip middle five    // numbers in below loop    if (n % 2 == 0 || n % 3 == 0)        return false;     for (int i = 5;             i * i <= n; i = i + 6)        if (n % i == 0 ||            n % (i + 2) == 0)            return false;     return true;} // function to check// Twisted Prime Numberstatic bool checkTwistedPrime(int n){    if (isPrime(n) == false)        return false;     return isPrime(reverse(n));} // Driver Codestatic public void Main (){     // Printing Twisted Prime// Numbers upto 200Console.WriteLine("First few Twisted Prime" +                         " Numbers are :- ");                          for (int i = 2; i <= 200; i++)    if (checkTwistedPrime(i))        Console.Write(i + " ");}} // This code is contributed by ajit
<?php// PHP program to check if a given number// is Twisted Prime or not function reverse($n){    $rev = 0;     // reversing the number    while($n > 0){        $r = $n % 10;        $rev = $rev * 10 + $r;        $n = (int)($n / 10);        }    return $rev;} // Returns true if n is prime, else falsefunction isPrime($n){    // Corner cases    if ($n <= 1)        return false;    if ($n <= 3)        return true;     // This is checked so that we can skip    // middle five numbers in below loop    if ($n % 2 == 0 or $n % 3 == 0)        return false;     $i = 5;    while ($i * $i <= $n){        if ($n % $i == 0 or $n % ($i + 2) == 0)            return false;        $i = $i + 6;        }     return true;} // function to check Twisted Prime numberfunction checkTwistedPrime ($n){    if (isPrime($n) == false)        return false;     return isPrime(reverse($n));} // Driver Code// Printing Twisted Prime numbers upto 200print("First few Twisted Prime numbers are :- \n");$i = 2;while($i<= 200){    if (checkTwistedPrime($i) == true)        print($i." ");    $i = $i + 1;}  // This code is contributed by mits?>
<script>    // Javascript program to check if a given    // number is Twisted Prime or not         function reverse(n)    {        let rev = 0, r;        while (n > 0)        {            r = n % 10;            rev = rev * 10 + r;            n = parseInt(n / 10, 10);        }        return rev;    }         function isPrime(n)    {        // Corner cases        if (n <= 1)            return false;        if (n <= 3)            return true;         // This is checked so that        // we can skip middle five        // numbers in below loop        if (n % 2 == 0 || n % 3 == 0)            return false;         for (let i = 5; i * i <= n; i = i + 6)            if (n % i == 0 || n % (i + 2) == 0)                return false;         return true;    }     // function to check    // Twisted Prime Number    function checkTwistedPrime(n)    {        if (isPrime(n) == false)            return false;         return isPrime(reverse(n));    }         // Printing Twisted Prime    // Numbers upto 200    document.write("First few Twisted Prime Numbers are :- " + "</br>");     for (let i = 2; i <= 200; i++)        if (checkTwistedPrime(i))            document.write(i + " ");     </script>
Output:  
First few Twisted Prime Numbers are :-
2 3 5 7 11 13 17 31 37 71 73 79 97 101 107 113 131 149 151 157 167 179 181 191 199
This article is contributed by Nikita Tiwari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
jit_t
Mithun Kumar
rameshtravel07
amartyaghoshgfg
surinderdawra388
simmytarika5
Prime Number
Zoho
Mathematical
Zoho
Mathematical
Prime Number
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Sieve of Eratosthenes
Prime Numbers
Program to find GCD or HCF of two numbers
Find minimum number of coins that make a given value
Minimum number of jumps to reach end
The Knight's tour problem | Backtracking-1
Algorithm to solve Rubik's Cube
Program for Decimal to Binary Conversion