Twisted Prime

The program must accept an integer N as the input. The program must print YES if N is a twisted prime number. Else the program must print NO as the output. A number is said to be twisted prime if it is a prime number and reverse of the number is also a prime number.

Boundary Condition(s):
1 <= N <= 10^16

Input Format:
The first line contains the value of N.

Output Format:
The first line contains either YES or NO.

Example Input/Output 1:
Input:
97

Output:
YES

Explanation:
97 is a prime number.
The reverse of 97 is 79. So 79 is also a prime number.
Hence the output is YES

Example Input/Output 2:
Input:
34

Output:
NO

def is_prime(num):
    if num <= 1:
        return False
    for i in range(2, int(num**0.5)+1):
        if num % i == 0:
            return False
    return True

n = int(input())
reverse_n = int(str(n)[::-1])

if is_prime(n) and is_prime(reverse_n):
    print("YES")
else:
    print("NO")
#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std;

bool is_prime(long long num) {
    if(num <= 1) return false;
    for(long long i = 2; i <= sqrt(num); i++) {
        if(num % i == 0) return false;
    }
    return true;
}

int main() {
    long long n;
    cin >> n;
    string str_n = to_string(n);
    reverse(str_n.begin(), str_n.end());
    long long reverse_n = stoll(str_n);

    if(is_prime(n) && is_prime(reverse_n)) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }
    return 0;
}
import java.util.Scanner;

public class TwistedPrime {
    public static boolean isPrime(long num) {
        if(num <= 1) return false;
        for(long i = 2; i <= Math.sqrt(num); i++) {
            if(num % i == 0) return false;
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        String strN = new StringBuilder(Long.toString(n)).reverse().toString();
        long reverseN = Long.parseLong(strN);

        if(isPrime(n) && isPrime(reverseN)) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }
}
Previous Article

Sort the String Values Based on Length

Next Article

Toggle Case - Reversal

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *