Answer to Question #257514 in Java | JSP | JSF for Nikhil Sahoo

Question #257514

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).


1
Expert's answer
2021-10-30T00:38:26-0400
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));
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS