|
| 1 | +package ch_22; |
| 2 | + |
| 3 | +/** |
| 4 | + * *22.6 (Execution time for GCD) Write a program that obtains the execution time for |
| 5 | + * finding the GCD of every two consecutive Fibonacci numbers from the index |
| 6 | + * 40 to index 45 using the algorithms in Listings 22.3 and 22.4. Your program |
| 7 | + * should print a table like this: |
| 8 | + * --------------------| 40 41 42 43 44 45 |
| 9 | + * --------------------------------------------------------------------------------- |
| 10 | + * Listing 22.3 GCD |
| 11 | + * Listing 22.4 GCDEuclid |
| 12 | + * <p> |
| 13 | + * (Hint: You can use the following code template to obtain the execution time.) |
| 14 | + * long startTime = System.currentTimeMillis(); |
| 15 | + * perform the task; |
| 16 | + * long endTime = System.currentTimeMillis(); |
| 17 | + * long executionTime = endTime - startTime; |
| 18 | + */ |
| 19 | +public class Exercise22_06 { |
| 20 | + |
| 21 | + /** |
| 22 | + * Test Driver |
| 23 | + */ |
| 24 | + public static void main(String[] args) { |
| 25 | + /* calculate the Fibonacci numbers from index 40 to index 45 */ |
| 26 | + int[] fibs = calcFibIndexes40to45(); |
| 27 | + System.out.println("\t\t\t\t\t\t40 41 42 43 44 45"); |
| 28 | + System.out.println("---------------------------------------------------------"); |
| 29 | + System.out.print("Listing 22.3 GCD\t\t"); |
| 30 | + for (int i = 0; i < fibs.length - 1; i++) { |
| 31 | + long startTime = System.currentTimeMillis(); |
| 32 | + int gcd = gcd(fibs[i], fibs[i + 1]); |
| 33 | + long endTime = System.currentTimeMillis(); |
| 34 | + long executionTime = endTime - startTime; |
| 35 | + System.out.print(" " + executionTime); |
| 36 | + } |
| 37 | + System.out.print("\nListing 22.4 GCDEuclid "); |
| 38 | + for (int i = 0; i < fibs.length - 1; i++) { |
| 39 | + long startTime = System.currentTimeMillis(); |
| 40 | + int gcd = gcdEuclid(fibs[i], fibs[i + 1]); |
| 41 | + long endTime = System.currentTimeMillis(); |
| 42 | + long executionTime = endTime - startTime; |
| 43 | + System.out.print(" " + executionTime); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Find GCD for integers m and n using Euclid's algorithm |
| 49 | + */ |
| 50 | + public static int gcdEuclid(int m, int n) { |
| 51 | + if (m % n == 0) |
| 52 | + return n; |
| 53 | + else |
| 54 | + return gcdEuclid(n, m % n); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + /** |
| 59 | + * Find GCD for integers m and n |
| 60 | + */ |
| 61 | + public static int gcd(int m, int n) { |
| 62 | + int gcd = 1; |
| 63 | + if (m % n == 0) return n; |
| 64 | + for (int k = n / 2; k >= 1; k--) { |
| 65 | + if (m % k == 0 && n % k == 0) { |
| 66 | + gcd = k; |
| 67 | + break; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return gcd; |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + private static int[] calcFibIndexes40to45() { |
| 76 | + int[] fibs = new int[6]; |
| 77 | + int i = 2; |
| 78 | + int f0 = 0; |
| 79 | + int f1 = 1; |
| 80 | + int f2 = 0; |
| 81 | + // Find fib numbers up to 39 |
| 82 | + while (i < 40) { |
| 83 | + f2 = f0 + f1; |
| 84 | + f0 = f1; |
| 85 | + f1 = f2; |
| 86 | + i++; |
| 87 | + } |
| 88 | + // Next fib will be at index 40 |
| 89 | + for (int j = 0; j < fibs.length; j++) { |
| 90 | + f2 = f0 + f1; |
| 91 | + fibs[j] = f2; |
| 92 | + f0 = f1; |
| 93 | + f1 = f2; |
| 94 | + } |
| 95 | + |
| 96 | + return fibs; |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | +} |
0 commit comments