Question: I need to make a Sieve in Java public static boolean[] makeSieve(int size, int[] divisors) Creates a boolean array arr of the given size such
I need to make a Sieve in Java public static boolean[] makeSieve(int size, int[] divisors)
Creates a boolean array arr of the given size such that arr[i] is true if and only if the number i is not divisible by any number in the given array divisors that is strictly smaller than i. It can be assumed that the given array is sorted in ascending (nondecreasing) order and all elements are greater than zero. Remember that 0 is divisible by any nonzero number (leaves a remainder of zero after division).
Example: If size is 10 and divisors is [2, 4], the method returns [F, T, T, T, F, T, F, T, F, T], i.e., only the cells at indices 0, 4, 6, and 8 contain false.
Note: Ideally this should be done without any division or modulus operations: for each number d in the divisors array, just set the cells at indices 2d, 3d, etc., to false. If the cell at index d is already false, then d can be skipped. If the divisor array consists of all numbers from 2 up through (at least) the square root of size, then the returned array will have true only at indices that are prime (also index 1). This is an efficient way to identify all prime numbers up to a given size. (Google "Sieve of Eratosthenes" for more background.)
Parameters:
size - size of the returned array
divisors - numbers to check for divisibility
Returns:
array whose ith cell contains false only if the number i is divisible by at least one element of the given array that is strictly smaller than i
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
