In Java, WAP to take two numbers as input then display whether they are twin prime or not using the following method prototype : String twin(int m ,int n).
public class Main {
public static String twin(int m, int n) {
if (Math.abs(m - n) != 2) {
return "No";
}
boolean[] primes = new boolean[Math.max(n, m) + 1];
primes[0] = true;
for (int i = 2; i < primes.length; i++) {
if (!primes[i]) {
for (int j = i * i; j < primes.length; j *= i) {
primes[j] = true;
}
}
}
return !primes[n] && !primes[m] ? "Yes" : "No";
}
public static void main(String[] args) {
System.out.println(twin(5, 7));
}
}
Comments
Leave a comment