Question
SieveOfPrimes (Java) Write a program that creates a Boolean ArrayList with a capacity of 1000. Initialize each value in the ArrayList to true. For each
SieveOfPrimes (Java)
Write a program that creates a Boolean ArrayList with a capacity of 1000.
Initialize each value in the ArrayList to true.
For each index in the ArrayList, if the index is not a prime number set the value of the ArrayList at that index to false.
When you are finished create a loop that prints the following output followed by the total number of primes from 0 to 999.
You can use the previous mechanisms for calculating a prime or the algorithm described below which is known as the Sieve of Eratosthenes. Either way the output should look the same.
Starting with array index 2, determine whether a given element is true. If so, loop through the remainder of the array and set to false every element whose index is a multiple of the index for the element with value true. Then continue the process with the next element with value true. For array index 2, all elements beyond element 2 in the array that have indices which are multiples of 2 (indices 4, 6, 8, 10, etc.) will be set to false; for array index 3, all elements beyond element 3 in the array that have indices which are multiples of 3 (indices 6, 9, 12, 15, etc.) will be set to false; and so on.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started