Skip to content

ch 22 06 #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions ch_22/Exercise22_06.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package ch_22;

/**
* *22.6 (Execution time for GCD) Write a program that obtains the execution time for
* finding the GCD of every two consecutive Fibonacci numbers from the index
* 40 to index 45 using the algorithms in Listings 22.3 and 22.4. Your program
* should print a table like this:
* --------------------| 40 41 42 43 44 45
* ---------------------------------------------------------------------------------
* Listing 22.3 GCD
* Listing 22.4 GCDEuclid
* <p>
* (Hint: You can use the following code template to obtain the execution time.)
* long startTime = System.currentTimeMillis();
* perform the task;
* long endTime = System.currentTimeMillis();
* long executionTime = endTime - startTime;
*/
public class Exercise22_06 {

/**
* Test Driver
*/
public static void main(String[] args) {
/* calculate the Fibonacci numbers from index 40 to index 45 */
int[] fibs = calcFibIndexes40to45();
System.out.println("\t\t\t\t\t\t40 41 42 43 44 45");
System.out.println("---------------------------------------------------------");
System.out.print("Listing 22.3 GCD\t\t");
for (int i = 0; i < fibs.length - 1; i++) {
long startTime = System.currentTimeMillis();
int gcd = gcd(fibs[i], fibs[i + 1]);
long endTime = System.currentTimeMillis();
long executionTime = endTime - startTime;
System.out.print(" " + executionTime);
}
System.out.print("\nListing 22.4 GCDEuclid ");
for (int i = 0; i < fibs.length - 1; i++) {
long startTime = System.currentTimeMillis();
int gcd = gcdEuclid(fibs[i], fibs[i + 1]);
long endTime = System.currentTimeMillis();
long executionTime = endTime - startTime;
System.out.print(" " + executionTime);
}
}

/**
* Find GCD for integers m and n using Euclid's algorithm
*/
public static int gcdEuclid(int m, int n) {
if (m % n == 0)
return n;
else
return gcdEuclid(n, m % n);
}


/**
* Find GCD for integers m and n
*/
public static int gcd(int m, int n) {
int gcd = 1;
if (m % n == 0) return n;
for (int k = n / 2; k >= 1; k--) {
if (m % k == 0 && n % k == 0) {
gcd = k;
break;
}
}

return gcd;
}


private static int[] calcFibIndexes40to45() {
int[] fibs = new int[6];
int i = 2;
int f0 = 0;
int f1 = 1;
int f2 = 0;
// Find fib numbers up to 39
while (i < 40) {
f2 = f0 + f1;
f0 = f1;
f1 = f2;
i++;
}
// Next fib will be at index 40
for (int j = 0; j < fibs.length; j++) {
f2 = f0 + f1;
fibs[j] = f2;
f0 = f1;
f1 = f2;
}

return fibs;
}


}