Twin primes are a pair of prime numbers that differ by 2.
For example, 3 and 5 are twin primes, 5 and 7 are twin primes, and 11 and 13 are twin primes.
We would like to write a program to find all twin primes less than 1,000. Display the output as follows:
(3, 5) (5, 7) ...
public class Main { public static void main(String[] args) { for (int i = 2; i < 1000; i++) { //your code here }// w ww.j a v a 2s . c om } }
public class Main { public static void main(String[] args) { for (int i = 2; i < 1000; i++) { if (isPrime(i) && isPrime(i + 2)) { System.out.printf("(%d, %d)\n", i, i + 2); } } } public static boolean isPrime(long n) { if (n < 2) return false; for (int i = 2; i <= n / 2; i++) { if (n % i == 0) return false; } return true; } }