I have a assignmnt to find all prime numbers less then 100 ,using a boolean array, set all to true and then set non primes to false and display them. ( got this!!! ) it also askes me to loop through the array and if number is prime (true) launch a thread to set them to false ( as a check) I have the program looping through and get the multiples ( the program is now set to show that they are being found) but not sure how to set the array from the method to false and retrieve that ... tried passing an array to method ( that did not work) not sure how to do this last bit. the final step is showing all 26 prime numbers again. Need help!!
package chapter_5;
public class Discovery3 {
public static void main(String[] args) throws InterruptedException {
int number = 1;
double total;
// creates array of 100
boolean[] array = new boolean[100];
// fills the array with true
java.util.Arrays.fill(array, true);
// sets numbers not prime to false and shows prime numbers
System.out.println("Prime numbers less than " + array.length + ":");
for (int i = 0; i < array.length; i++) {
int adder = 0;
for (double n = 0; n < array.length; n++) {
total = (i + 1) % (n + 1);
if (total == 0) {
adder += 1;
}
}
if (adder > 2) {
array[i] = false;
} else {
System.out.print(number + ", ");
}
number += 1;
}
// blank line
System.out.println("\n");
// goes through array, if true(prime)calculate multiples
// and set numbers to false in array
for (int g = 0; g < array.length; g++) {
if (array[g] == true && (g + 1) > 1) {
int sum = (g + 1);
Thread one = new Multiples(sum, array.length);
//Thread one = new HelloName();
one.start();
}
}
// Finally, shows all the numbers that are set to true (prime)
System.out.println("\nNumbers still set to True: ");
for (int r = 0; r < array.length; r++) {
if (array[r] == true) {
System.out.print((r + 1) + ", ");
}
}
System.out.println("\n");
}
}
// class that performs multiples and assuring they are false
class Multiples extends Thread {
public Multiples(int Sum, int length) {
int sum = Sum;
while (sum < length) {
// would show multiples of each prime #
//System.out.print(sum + " ");
sum = sum + Sum;
if (sum < length) {
System.out.print(sum + " ");
// this would check that multiples were set to false....
// need to get array and set it here and return it
//array[(sum - 1)] = false;
} else {
System.out.println(" Multples of " + Sum + " set to False.......DONE");
}
}
}
}