Skip to content
Related Articles

Related Articles

Improve Article

Twisted Prime Number

  • Difficulty Level : Easy
  • Last Updated : 28 Apr, 2021

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.

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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 <bits/stdc++.h>
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 nbers 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 nber
bool checkTwistedPrime(int n)
{
    if (isPrime(n) == false)
        return false;
 
    return isPrime(reverse(n));
}
 
// Driver Code
int main(void)
{
    // Printing Twisted Prime nbers upto 200
    cout << "First few Twisted Prime nbers 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.

Python




# Python program to check if a given number
# is Twisted Prime or not
 
def reverse(n) :
    rev = 0
 
    # reversing the nber
    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 nbers 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 nber
def checkTwistedPrime (n) :
    if (isPrime(n) == False):
        return False
 
    return isPrime(reverse(n))
 
# Driver Code
# Printing Twisted Prime nbers upto 200
print "First few Twisted Prime numbers are :- "
i = 2
while i<= 200 :
    if (checkTwistedPrime(i) == True) :
        print i,
    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




<?php
// PHP program to check if a given number
// is Twisted Prime or not
 
function reverse($n)
{
    $rev = 0;
 
    // reversing the nber
    while($n > 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 nbers 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 nber
function checkTwistedPrime ($n)
{
    if (isPrime($n) == false)
        return false;
 
    return isPrime(reverse($n));
}
 
// Driver Code
// Printing Twisted Prime nbers 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




<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 contribute@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.
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :