Check Whether the given two number are Twin prime | Java, Python, C & C++ program to check if two input numbers are Twin prime | Java, Python, C & C++ program to check if given numbers are Twin prime or not
Explanation of the Code: Check Whether the given two number are Twin prime
The problem defines a "Twin Prime" as two prime numbers where the absolute difference between them is 2.
For example, the pair (41, 43) is a twin prime because both 41 and 43 are prime numbers, and their difference is 2.
- isPrime function:
- This function checks whether a given number
a
is prime. It does so by iterating through numbers from 2 toa-1
. Ifa
is divisible by any number in this range, it is not prime.
- This function checks whether a given number
- Main Program:
- The program reads two integers
a
andb
from the user. - It checks whether both
a
andb
are prime numbers and if their absolute difference is 2 (|a - b| == 2
). - If both conditions are satisfied, it prints that
a
andb
are twin primes. Otherwise, it prints that they are not.
Java Code: Check Whether the given two number are Twin prime
import java.util.*;public class twinPrime {public static boolean isPrime(int a){boolean prime=true;for(int i=2;i<a;i++){if(a%i==0){prime=false;break;}}return prime;}public static void main(String[] args) {Scanner read=new Scanner(System.in);System.out.print("Enter the first number: ");int a=read.nextInt();System.out.print("Enter the second number: ");int b=read.nextInt();read.close();if(isPrime(a) && isPrime(b)&& (Math.abs(a-b)==2)){System.out.println(a+" and "+b+" are Twin Prime");}else{System.out.println(a+" and "+b+" are not Twin Prime");}}}// |VICTORY| The code ran successfully !
Python Code: Check Whether the given two number are Twin prime
def is_prime(a):
for i in range(2, a):
if a % i == 0:
return False
return True
# Input and output
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
if is_prime(a) and is_prime(b) and abs(a - b) == 2:
print(f"{a} and {b} are Twin Primes.")
else:
print(f"{a} and {b} are not Twin Primes.")
C: Check Whether the given two number are Twin prime
#include <stdio.h>
int isPrime(int a) {
for (int i = 2; i < a; i++) {
if (a % i == 0) {
return 0; // not prime
}
}
return 1; // prime
}
int main() {
int a, b;
printf("Enter the first number: ");
scanf("%d", &a);
printf("Enter the second number: ");
scanf("%d", &b);
if (isPrime(a) && isPrime(b) && abs(a - b) == 2) {
printf("%d and %d are Twin Primes.\n", a, b);
} else {
printf("%d and %d are not Twin Primes.\n", a, b);
}
return 0;
}
C++: Check Whether the given two number are Twin prime
#include <iostream>#include <cmath> // for abs() function using namespace std; bool isPrime(int a) { for (int i = 2; i < a; i++) { if (a % i == 0) { return false; // not prime } } return true; // prime } int main() { int a, b; cout << "Enter the first number: "; cin >> a; cout << "Enter the second number: "; cin >> b; if (isPrime(a) && isPrime(b) && abs(a - b) == 2) { cout << a << " and " << b << " are Twin Primes." << endl; } else { cout << a << " and " << b << " are not Twin Primes." << endl; } return 0; }
Time Complexity:
isPrime function:
- The
isPrime
function iterates from 2 toa-1
to check ifa
is divisible by any number in this range. This takesO(a)
time in the worst case for each prime check.
- The
Main Program:
- The program calls
isPrime
twice, once for each numbera
andb
. - Therefore, the time complexity is dominated by the
isPrime
checks, resulting in: - Time Complexity:
O(a + b)
, wherea
andb
are the input numbers.
Space Complexity:
The program only uses a few integer variables and does not use any additional memory-dependent data structures. Therefore, the space complexity is constant.
- Space Complexity:
O(1)
.
Summary: Check Whether the given two number are Twin prime
- Time Complexity:
O(a + b)
, wherea
andb
are the input numbers. - Space Complexity:
O(1)
(constant space usage).
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