5.3 Given two positive integers i and j, the greatest common divisor of i and j, written gcd (i, j) is the largest integer k such that (i % k = 0) and (j % k = 0). For example, gcd (35, 21) = 7 and gcd (8, 15) = 1. Test and develop a wrapper method and a wrapped recursive method that return the greatest common divisor of i and j. Here is the method specification for the wrapper method: /** * Finds the greatest common divisor of two given positive integers * * @param i - one of the given positive integers. * @param j - the other given positive integer. * @return the greatest common divisor of iand j. * * @throws IllegalArgumentException - if either i or j is not a positive integer. * */ public static int gcd (int i, int j) Big hint: According to Euclid's algorithm, the greatest common divisor of i and j is j if i % j = 0. Otherwise, the greatest common divisor of i and j is the greatest common divisor of j and (i % j). | |
| View Solution | |
| << Back | Next >> |