Question
Please include comments.Your help would be greatly appreciated.
MODIFY this algorithm, to make the test for primality more efficient: Algorithm to find all prime numbers up to some limit N. Consider every number from 2 up to N. If it is a prime, you include it in the list. To test whether a number m is a prime number you simply divide it by each of the numbers 2, 3 ... m - 1; if any of these divisions is without remainder, then m is not prime, otherwise it is. You must MODIFY this algorithm to make it more efficient, NOT write this algorithm. Your program should take two command line arguments (the algorithm number and N) and output the primes using System.out.println and the timing data using System.err.println - so you could run your program like this: java -jar Primes.jar 1 17 and get an output like this: 2 3 5 7 11 13 17 1 The first 7 numbers are prime numbers less than/equal to 17, the last number is the time taken to run the algorithm.
Explanation / Answer
import java.util.*; public class Prime { public static void main( String[] args ){ } public static void prime( int initialCapacity){ int index=0; ArrayList listOfPrimeNumbers = new ArrayList( ); boolean[] isPrimeNumber = new boolean[initialCapacity + 1]; // boolean defaults to // false while ( index