In: Computer Science
use java for :
1. Write a method called indexOfMax that takes an array of integers and returns the index of the largest element.
2. The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit”
(https://en.wikipedia. org/wiki/Sieve_of_Eratosthenes).Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n -1, whether the number is prime.
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! =========================================================================== //1. Write a method called indexOfMax that takes an array of integers and returns the index of the largest element. public static int indexOfMax(int arr[]) { int max_index = 0; for (int i = 0; i < arr.length; i++) { if (arr[max_index] < arr[i]) max_index = i; } return max_index; } //2. The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit” public static boolean[] sieveOfEratosthenes(int limit) { boolean[] sieve = new boolean[limit]; java.util.Arrays.fill(sieve, true); for (int i = 2; i <= Math.sqrt(limit); i++) { for (int j = i * 2; j <= limit; j += i) { sieve[j - 1] = false; } } return sieve; }
=====================================================================