Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A.) In Java write a program to create an array of boolean values all intialized

ID: 3528718 • Letter: A

Question

A.) In Java write a program to create an array of boolean values all intialized as true. As the algorithm progresses array elements with prime subscripts will remain true, while other array locations will end up with flase values. B.) Starting wih array location 2, if a particular location contains true, then loop through the remainder of the array and set to false each location whos subscript is a multiple of the element which is true. So, since location 2 is true, you loop through all multiples of 2, setting those locations to false. Next, since location 3 is also still true, loop through the rest of the array marking every position that is a multiple of 3 to false. Continue this process until you have reached the square root of the array size. Use the Sieve of Eratosthenes to determine how many prime numbers there are that are less than one million. Please help with this, im having a rough time. Full rating to who can write this program!

Explanation / Answer

public class SieveOfEratosthenes {

public static void main(String[] args) {
/* list upto 100 array size, you may also increase this*/
boolean[] array = new boolean[100];

for (int i = 0; i < array.length; i++) {
array[i] = true;
}

for (int i = 2; i <= Math.sqrt(array.length); i++) {
if (array[i] == true) {
for (int j = i + 1; j < array.length; j++) {
if (j % i == 0) {
array[j] = false;
}
}
}
}
System.out.println("List Of prime number upto " + array.length + " : ");
for (int i = 2; i < array.length; i++) {
if (array[i] == true) {
System.out.print(i + " ");
}
}

}

}

Output:

List Of prime number upto 100 :
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote