Número primo torcido

Se dice que un número es primo torcido si es un número primo y el reverso del número también es un número primo.
Ejemplos: 
 

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.

La idea es primero verificar si n es un número primo, luego invertir el número n y verificar si n invertido es un número primo o no.
Utilizamos los métodos de los dos artículos siguientes. 
1) Comprobar si un número es primo (Método de la escuela) 
2) Invertir los dígitos de un número .
 

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

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

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

Producción: 
 

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

Este artículo es una contribución de Nikita Tiwari . Si le gusta GeeksforGeeks y le gustaría contribuir, también puede escribir un artículo usando contribuya.geeksforgeeks.org o envíe su artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *