Explanation of the Code: Java, Python, C & C++ program to check if a input number is twisted prime
The problem defines a "Twisted Prime Number" as a number that:
- Itself is prime.
- The number formed by reversing its digits is also prime.
A number is called a twisted prime number if it is a prime number and reverse of this number is also a prime number.
Example: 31
The logic follows these steps:
- isPrime: This function checks if a number is prime by testing divisibility for all numbers less than
n
. - reverse: This function reverses the digits of the number
m
by repeatedly extracting the last digit and building the reversed number. - isTwistedPrime: This function checks if both the number
a
and its reversed versionb
are prime. If they are, it prints that the number is a "Twisted Prime"; otherwise, it prints that it's not.
Java Code : to check if a input number is twisted prime
import java.util.Scanner;public class twistedPrime {public static boolean isPrime(int n){int i=2;boolean prime=true;while(n>i){if(n%i==0){prime=false;break;}i++;}return prime;}public static int reverse(int m){int digit;int rev=0;while(m!=0){digit=m%10;rev=rev*10+digit;m=m/10;}return rev;}public static void isTwistedPrime(int a){int b=reverse(a);if(isPrime(a)&& isPrime(b)){System.out.println(a+" is a Twisted Prime Number.");}else{System.out.println(a+" is not a Twisted Prime Number.");}}public static void main(String[] args) {Scanner read=new Scanner(System.in);System.out.print("Enter a number: ");int a=read.nextInt();read.close();isTwistedPrime(a);}}// |VICTORY| The code ran successfully !
Python Code: to check if a input number is twisted prime
def is_prime(n):
i = 2
while n > i:
if n % i == 0:
return False
i += 1
return True
def reverse(m):
rev = 0
while m != 0:
digit = m % 10
rev = rev * 10 + digit
m //= 10
return rev
def is_twisted_prime(a):
b = reverse(a)
if is_prime(a) and is_prime(b):
print(f"{a} is a Twisted Prime Number.")
else:
print(f"{a} is not a Twisted Prime Number.")
# Input and output
a = int(input("Enter a number: "))
is_twisted_prime(a)
C code : to check if a input number is twisted prime
#include <stdio.h>
int isPrime(int n) {
int i = 2;
while (n > i) {
if (n % i == 0) {
return 0; // not prime
}
i++;
}
return 1; // prime
}
int reverse(int m) {
int digit, rev = 0;
while (m != 0) {
digit = m % 10;
rev = rev * 10 + digit;
m = m / 10;
}
return rev;
}
void isTwistedPrime(int a) {
int b = reverse(a);
if (isPrime(a) && isPrime(b)) {
printf("%d is a Twisted Prime Number.\n", a);
} else {
printf("%d is not a Twisted Prime Number.\n", a);
}
}
int main() {
int a;
printf("Enter a number: ");
scanf("%d", &a);
isTwistedPrime(a);
return 0;
}
C++ : program to check if a input number is twisted prime
#include <iostream>
using namespace std;
bool isPrime(int n) {
int i = 2;
while (n > i) {
if (n % i == 0) {
return false; // not prime
}
i++;
}
return true; // prime
}
int reverse(int m) {
int digit, rev = 0;
while (m != 0) {
digit = m % 10;
rev = rev * 10 + digit;
m = m / 10;
}
return rev;
}
void isTwistedPrime(int a) {
int b = reverse(a);
if (isPrime(a) && isPrime(b)) {
cout << a << " is a Twisted Prime Number." << endl;
} else {
cout << a << " is not a Twisted Prime Number." << endl;
}
}
int main() {
int a;
cout << "Enter a number: ";
cin >> a;
isTwistedPrime(a);
return 0;
}
Time Complexity:
-
isPrime function:
- The
isPrime
function checks if a numbern
is prime by dividing it from2
ton-1
(or untilsqrt(n)
for optimization, but the current code doesn't include this optimization). The time complexity of this isO(n)
in the worst case.
- The
-
reverse function:
- The
reverse
function processes each digit of the number. The time complexity for reversing a numberm
isO(d)
, whered
is the number of digits inm
. Sinced
is proportional tolog(m)
, the time complexity isO(log m)
.
- The
-
isTwistedPrime function:
- The function calls
isPrime(a)
andisPrime(reverse(a))
. Thus, the total time complexity is the sum of:O(n)
for checking ifa
is prime.O(log a)
for reversinga
(sincereverse(a)
takesO(log a)
time).O(n)
for checking if the reversed number is prime.
- Hence, the total time complexity is
O(n + log a + n) = O(n)
in the worst case.
- The function calls
Overall Time Complexity: O(n)
(for checking primality).
Space Complexity:
The space complexity is O(1)
because the algorithm uses only a few integer variables (i
, rev
, etc.), and no additional memory-dependent data structures are used.
Overall Space Complexity: O(1)
.
Thank You for reading this post. You can check out more articles like this on www.physicswallah.in .
If you have any doubts then you can post a comment below.
No comments:
Post a Comment