📜  扭曲素数

📅  最后修改于: 2021-05-06 21:06:23             🧑  作者: Mango

如果数字是质数,则该数字被称为扭曲质数,而数字的反数也是质数。

例子:

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.

想法是首先检查n是否为质数,然后反转数字n并检查反转的n是否为质数。

我们使用下面两篇文章的方法。
1)检查数字是否为质数(学校方法)
2)数字的倒数。

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


输出:

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