Question
Please include comments. Your help would be greatly appreciated.The program is in Java.
You begin with an array P of Booleans with one position for each index from 2 up to n(in Java arrays begin at index 0 but that just means we won't actually use positions 0 and 1). You fill this array with the value 'true' at each position, except for P[0] and P[1] which is set to 'false'. The next step is repeated until we are finished. We look for the next position in the array which has a 'true' value(this will be position 2 initially, of course). Suppose this position is position t. Then we make p[2t]. p[3t], p[4t]... all 'false' and we add t to our list of primes. 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, hte last number is the time taken to run the algorithm.
Explanation / Answer
Please rate with 5 stars :)