Listed below are question and attempted solution. However there are some errors. Can someone please assist. Thanks
(The Sieve of Eratosthenes) A prime integer is any integer that is evenly divisible only by itself and 1. The Sieve of Eratosthenes is a method of finding prime numbers. It operates as follows:
Create an array with all elements initialized to 1 (true). Array elements with prime subscripts will remain 1. All other array elements will eventually be set to zero. Starting with array subscript 2 (subscript 1 must be prime), every time an array element is found whose value is 1, loop through the remainder of the array and set to zero every element whose subscript is a multiple of the subscript for the element with value 1. For array subscript 2, all elements beyond 2 in the array that are multiples of 2 will be set to zero (subscripts 4, 6, 8, 10, etc.); for array subscript 3, all elements beyond 3 in the array that are multiples of 3 will be set to zero (subscripts 6, 9, 12, 15, etc.); and so on.
// Solution // PrimeNum.java
// Program searches for and prints the prime numbers between 1 and 999 inclusive
// main method begins execution of Java application public static void main( String args[] ) {
// Declare an array: arrayc with 1000 elements boolean arrayc[]; // reference to int array arrayc = new boolean[1000]; // allocate array
// Initialization & Declaration of variables for ( int index = 0; index < arrayc.length; index++ ) arrayc[index] = true; String output = "";
// Call method to search for multiples of index between 2 and 999 primeSearch(arrayc);
} // method main
// ************************************************************ // Method: primeSearch to search for multiples of the current index
public static void primeSearch( boolean arrayNum[] ) {
int count = 0;
// Initiate loop to find multiples of the index between 2 and 999 for ( int i = true; i < arrayNum.length; i++ ) if ( arrayNum = true & i != true) for ( int counter = i; counter < arrayNum.length; counter++ ) if(counter%i = false & counter != i) arrayNum[counter] = false;
// Print prime numbers: String output = "Prime Numbers\n";
for ( int k = 2; k <arrayNum.length; k++ ){ if (arrayNum[k] = true) output += k +"\n"; ++count;
} // close third for
JTextArea outputArea = new JTextArea(); outputArea.setText( output );
JOptionPane.showMessageDialog( null, outputArea, "Prime Numbers between 1 and 999", JOptionPane.INFORMATION_MESSAGE );
public class primeBoolean { // begin int start; int end; boolean[] bolArray; // constructor primeBoolean(){ start = 0; end = 0; } // constructor: initialize the start and primeBoolean(int st, int nd ){ start = st; end = nd; int i = (nd - st) + 1; bolArray = new boolean[i]; } // return the index of array to be used public int getIndex() { int i = (this.end - this.start) + 1; return i; } // initialize array element to defualt value of true public void initArray(){ for (int x = 0;x < bolArray.length ;x++) bolArray[x] = false; } // intialize array element to its argument public void initArray(boolean bln){ for (int x = 0;x < bolArray.length ;x++) bolArray[x] = bln; } // print the array without index public void printArray(){ for (int x = 0;x < bolArray.length ;x++) System.out.println(bolArray[x] + " "); } // print the array with index public void printArrayWithIndex(){ int z = this.start; for (int x = 0;x < bolArray.length ;x++) { System.out.println("Index " + z + " : " + bolArray[x]); z++; } } // print the argument array without index public void printArray(boolean[] arr){ for (int x = 0;x < arr.length ;x++) System.out.println(arr[x] + " "); } // print the argument array with index public void printArrayWithIndex(boolean[] arr){ int z = this.start; for (int x = 0;x < arr.length ;x++){ System.out.println("Index " + z + " : " + arr[x]); z++; } } // determine if the argument number is prime and // return true, otherwise false public boolean isPrime(int number) { int limit = (int)Math.sqrt(number); int denum = 2; boolean prime = true; while (denum <= limit) { // begin while if ((number % denum) == 0) prime = false; denum++; } // end while return prime; } // end // return the prime boolean array public boolean[] determinePrime(){ // begin boolean prime; int count = start; int index = this.getIndex(); boolean[] tmp = new boolean[index]; int indexCounter = 0; while (count <= end) { // begin prime = isPrime(count); if(prime == true) tmp[indexCounter] = true; else tmp[indexCounter] = false; indexCounter++; count++; } // end while bolArray = tmp; return tmp; } // end public static void main(String[] args) { primeBoolean prime = new primeBoolean(100,200); int index = prime.getIndex(); boolean[] anotherArray; anotherArray = new boolean[index]; prime.initArray(true); anotherArray = prime.determinePrime(); prime.printArrayWithIndex(); } } // end class