# Twisted Prime Number

• Difficulty Level : Easy
• Last Updated : 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.```

Recommended Practice

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

 `// C/C++ program to check if a given number``// is Twisted Prime or not``#include ``using` `namespace` `std;` `// Returns reverse of n``int` `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 prime``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 number``bool` `checkTwistedPrime(``int` `n)``{``    ``if` `(isPrime(n) == ``false``)``        ``return` `false``;` `    ``return` `isPrime(reverse(n));``}` `// Driver Code``int` `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

 `// Java program to check if a given number``// is Twisted Prime or not``import` `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.`

## Python3

 `# 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 false``def` `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 number``def` `checkTwistedPrime (n) :``    ``if` `(isPrime(n) ``=``=` `False``):``        ``return` `False` `    ``return` `isPrime(reverse(n))` `# Driver Code``# Printing Twisted Prime numbers upto 200``print` `(``"First few Twisted Prime numbers are :- "``)``i ``=` `2``while` `i<``=` `200` `:``    ``if` `(checkTwistedPrime(i) ``=``=` `True``) :``        ``print``(i,end``=``" "``)``    ``i ``=` `i ``+` `1`  `# This code is contributed by Nikita Tiwari.`

## C#

 `// C# program to check if a given``// number is Twisted Prime or not``using` `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 Number``static` `bool` `checkTwistedPrime(``int` `n)``{``    ``if` `(isPrime(n) == ``false``)``        ``return` `false``;` `    ``return` `isPrime(reverse(n));``}` `// Driver Code``static` `public` `void` `Main ()``{``    ` `// Printing Twisted Prime``// Numbers upto 200``Console.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

 ` 0){``        ``\$r` `= ``\$n` `% 10;``        ``\$rev` `= ``\$rev` `* 10 + ``\$r``;``        ``\$n` `= (int)(``\$n` `/ 10);``        ``}``    ``return` `\$rev``;``}` `// Returns true if n is prime, else false``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 ``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 number``function` `checkTwistedPrime (``\$n``)``{``    ``if` `(isPrime(``\$n``) == false)``        ``return` `false;` `    ``return` `isPrime(reverse(``\$n``));``}` `// Driver Code``// Printing Twisted Prime numbers upto 200``print``(``"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``?>`

## Javascript

 ``

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.