Saturday 20 August 2016

Chapter 6 Exercise 29, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

**6.29 (Twin primes) 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. Write a program to find all twin primes less than 1,000. Display the output as follows:
(3, 5)
(5, 7)
...


public class ProgrammingExercise6_29 {
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  for (int i = 2; i < 1000; i++) {
 
 
   if (isPrime(i)&&isPrime(i+2)) {
    System.out.printf("(%d, %d)\n", i, i+2);
   }
  }
   
 }
  
  
 /** Check whether number is prime */
 public static boolean isPrime(int number) {
  for (int divisor = 2; divisor <= number / 2; divisor++) {
   if (number % divisor == 0) { // If true, number is not prime
    return false; // number is not a prime
   }
  }
 
  return true; // number is prime
 }
 
}

No comments :

Post a Comment