/** * File: Prime.java * Project: CMSC 202 Project #0, Fall 2010 * Author: John Doe * Date: Sep 8, 2010 * Section: 14 * E-mail: jdoe@umbc.edu * Class Invariants: * None. */ /** * @author park * */ public class Prime { /** * Compute all the prime numbers within the specified range, * using the Sieve of Eratosthenes algorithm * (@link http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) * PreCondition: * PostCondition: * @param startNum the first number to scan for prime numbers * @param endNum the last number to scan * @return an exactly-sized array filled with all found primes */ public static int[] findPrimes(int startNum, int endNum) { // Make sure you follow all of the requirements outlined in // the project description for your implementation. // It is guaranteed that startNum >= 2, and startNum <= endNum. // You must create a local array of booleans to hold your // "sieve" tracking what numbers have been crossed off. // You must use a "meaningful" for-loop // You must return a fully-filled array of the correct size; // in other words, if you found 7 prime numbers in the requested // range, your array must be 7 elements long. // You must call a helper method (template given below) to // process your working array to cancel out all multiples of // the next prime found. } /** * Helper method for the Sieve of Eratosthenes algorithm. * Given a prime number and an array of booleans, compute all * multiples of the prime up to the highest index of the array, * and cancel those entries out by setting those array elements * to boolean "false". * @param prime the next prime number, to cancel all multiples of * @param sieveArray the array to cancel multiples from */ public static void cancelMultiples(int prime, boolean[] sieveArray) { } /** * Test harness for Project 0 * PreCondition: * PostCondition: * @param args startNum endNum */ public static void main(String[] args) { if (args.length != 2) { System.err.println("args: startNum endNum"); } else { int startNum = Integer.parseInt(args[0]); int endNum = Integer.parseInt(args[1]); int[] foundPrimes = findPrimes(startNum, endNum); for (int i = 0; i < foundPrimes.length; i++) { System.out.println(foundPrimes[i]); } } } }