public class ch7 { public static void main(String[] args) { int sieveSize = 20; boolean[] mySieve = sieve(sieveSize); int[] someNumbers = {1, 2, 3, 4, 1, 3, }; //System.out.println(indexOfMax(someNumbers)); //printSieve(mySieve); //printHistogram(letterHist("AaaBbBcdeefghIIjkLmmnno")); //System.out.println(isAnagram("Miles", "Smile")); } // #### Question 7.3 #### public static int indexOfMax(int[] nums) { //Assume the largest value is the first value int index = 0; int maxValue = nums[0]; for (int i = 1; i < nums.length; i++) { if (nums[i] > maxValue) { // if we find a bigger value index = i; // record the index maxValue = nums[i]; // and the value } } return index; } // #### Question 7.4 ##### public static boolean[] sieve (int n) { boolean[] sieve = new boolean[n]; //Initialize all elements in the sieve to true. for (int i = 0; i < n; i++) { sieve[i] = true; } //... except 0 and 1, which are not prime numbers by definition. sieve[0] = sieve[1] = false; int i = 0; while (i < n) { // advance forward until we find a value that is true. This is a prime number. while (sieve[i] == false) { i++; } //Now that we have found a prime, we can execute the CORE LOOP of the sieve of Erastothenes: //Mark all the multiples of sieve as not prime. int currentPrime = i; while (i < n) { i = i + currentPrime; //count up by the prime number //ie: if prime is 2, we're going to count 4, 6, 8, etc ... // if prime is 5, we're going to count 10, 15, 20, etc ... if (i < n) sieve[i] = false; //mark the multiple as not a prime. } //return to the place we started to check for the next prime number. i = currentPrime+1; } return sieve; } // #### Question 7.5 #### public static boolean areFactors (int n, int[] factors) { for (int a : factors) { if (n%a != 0 ) return false; // We found a number that isn't a factor! return false. } return true; // If we got to here, then we never found a non-factor. return true. } // #### Question 7.6 #### public static boolean arePrimeFactors (int n, int[] factors) { boolean[] primes = sieve(n); // Let's generate an array to identify prime numbers with the sieve method. int product = 1; for (int a : factors) { if (!primes[a]) return false; // If the number is not a prime number, return false! product *= a; // Otherwise add it to the product of numbers in the array. } return product == n; // if we got here, then we only found prime numbers. // Return true if the product equals n. } // #### Question 7.7 #### public static int[] letterHist (String text) { int[] counts = new int[26]; // make an array of ints to track letter counts text = text.toLowerCase(); // make the String lowercase. for (int i = 0; i < text.length(); i++) { char letter = text.charAt(i); // get the current character int index = letter - 'a'; // convert from char to int to get the index of the counts array counts[index]++; // add one to that index to count that letter } return counts; } // #### Question 7.8 #### public static boolean isAnagram (String a, String b) { int[] aHist = letterHist(a.toLowerCase()); // get the count of all the letters in String a int[] bHist = letterHist(b.toLowerCase()); // ... do the same for String b for (int i = 0; i < aHist.length; i++) { if (aHist[i] != bHist[i]) return false; // If we ever find a count of letters that varries, return false. } return true; // And if we get to this line, we never returned false, so the answer is true } // Utility Method for Testing public static void printSieve (boolean[] sieve) { for (int i = 0; i < sieve.length; i++) { System.out.println(i + ": " + sieve[i]); } } // Utility Method for Testing public static void printHistogram (int[] counts) { for (int i = 0; i < counts.length; i++) { System.out.print((char)(i + 'a')); System.out.print(": "); for (int j = 0; j < counts[i]; j++) { System.out.print("#"); } System.out.println(""); } } }