// File: PrimeMinister2.java // Finds and prints the first n prime numbers, where n is entered // by the user // Version 2 of the PrimeFinder class differs from the previous version // only in boolean method isPrime(), which makes use of this recursive // definition of primehood: // "A positive integer is prime if it is not a product of any smaller // prime number" // isPrime() checks to see if a number being tested for primehood // divides evenly by any of the prime numbers on a list. If so, // then the number is not prime and false is returned; otherwise, // the number is prime so it is added to the list and true is returned. // Note that the first number tested is 2 and the list is initially // empty, so 2 is the first number added to the list // NOTE: The PrimeFinder class "depends on" the Align class to format the // output in columns import javax.swing.JOptionPane ; import java.util.ArrayList ; /** * Finds and prints the first "n" prime numbers */ class PrimeFinder { private int howMany ; // number of primes to be found private ArrayList primesList ; // list of prime numbers /** * Construct a PrimeFinder object * @param howMany the number of primes to be found */ public PrimeFinder(int howMany) { this.howMany = howMany ; primesList = new ArrayList() ; // creates empty list } /** * Prints the first N prime numbers */ public void printPrimes() { int count = 0 ; // counts actual number of primes found int number = 2 ; // each of a series of ints to be tested for // "primehood," beginning with 2 System.out.println("\nThe First " + howMany + " Prime Numbers:\n") ; // find and print primes do // repeat... { if (isPrime(number)) // ...if number is prime ... { // ...print it right-aligned in columns 6 spaces wide System.out.print(Align.right(number,6)) ; count++ ; // .......and count it if (count % 10 == 0) // ...every ten primes ... System.out.println() ; // ...... start new line } number++ ; // ...get next test number } while (count < howMany) ; // until required # of primes found System.out.println() ; } // See if num is a prime number by trying to divide it by smaller // prime numbers stored in primesList private boolean isPrime (int num) { for (int position = 0 ; position < primesList.size() ; position++) { int trialDivisor = primesList.get(position) ; if (num % trialDivisor == 0) // num is not prime... return false ; // ...so return false } primesList.add(num) ; // num is prime, so add to list... return true ; // ...and return true } } // end of PrimeFinder class definition ==================================== /** * A test class for the PrimeFinder class. */ public class PrimeMinister2 { public static void main (String [] args) { JOptionPane.showMessageDialog (null, "I will find and print the first N prime numbers!") ; String input = JOptionPane.showInputDialog ("Enter N (number of primes to find)") ; int howMany = Integer.parseInt(input) ; PrimeFinder primes = new PrimeFinder(howMany) ; primes.printPrimes() ; } } /* sample output: The First 100 Prime Numbers: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 */