Problem:
Two consecutive odd integers that are both prime are called twin primes. The twin primes
conjecture is that there are infinitely many twin primes. Write a program that finds all the
twin primes that are less than 1000. Use the Primes class from Problem 2.22. Your first five
lines of output should look like this:
conjecture is that there are infinitely many twin primes. Write a program that finds all the
twin primes that are less than 1000. Use the Primes class from Problem 2.22. Your first five
lines of output should look like this:
Output:
3 5
5 7
11 13
17 19
29 31
5 7
11 13
17 19
29 31
Solution:
public class TestTwinPrimes {
public static void main(String[] args) {
Primes.setSize(1000);
int n = Primes.next();
while (n < 0.9*N) {
if (Primes.isPrime(n+2)) {
System.out.println(n + "\t" + (n+2));
}
n = primes.next();
}
}
}
No comments :
Post a Comment