Programming 2 with Java

Exercise: Prime Counter

Prime Counter

  1. Implement a Thread class PrimeCounterThread whose constructor takes a minimum and a maximum number and whose run method counts the number of prime numbers in between using the utitlity class Primes.java.
  2. Implement the program PrimeCounter which
    • takes a minimum and a maximum number as well as a number of threads as command line arguments
    • splits the given range into subranges and starts the specified number of threads each of which counts the prime numbers in one subrange
    • displays the total number of found prime numbers and the required runtime
    > java PrimeCounter 1 10000000 4
    664579 prime numbers found in 4.859 seconds
    
    (The table on the Wikipedia page of the prime-counting function can be used to verify the results.)
  3. Run the program with different number of threads and compare the runtimes.

Solution