Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1.  
  2. public class IterativeToRecursive {
  3.  
  4.     public static void main(String[] args) {
  5.         for (int a = 0; a < 10; a++)
  6.             for (int b = 0; b < 10; b++) {
  7.                 if (iterative(a, b) == recursive(a, b))
  8.                     System.out.println(
  9.                             "iterative(" + a + ", " + b + ") == " +
  10.                             "recursive(" + a + ", " + b + ")");
  11.                 else
  12.                     System.out.println(
  13.                             "iterative(" + a + ", " + b + ") != " +
  14.                             "recursive(" + a + ", " + b + ")");
  15.             }
  16.     }
  17.  
  18.     /**
  19.      * What does this function do?
  20.      */
  21.     public static int iterative(int a, int b) {
  22.         while (b != 0) {
  23.             if (a < b) {
  24.                 int temp = a;
  25.                 a = b;
  26.                 b = temp;
  27.             } else {
  28.                 int aNew = b;
  29.                 int bNew = a % b;
  30.                 a = aNew;
  31.                 b = bNew;
  32.             }
  33.         }
  34.         return a;
  35.     }
  36.  
  37.     /**
  38.      * Implement the body of this function so that it calculates the same
  39.      * results as the function <code>int iterative(int a, int b)</code>.
  40.      * Use recursion and do <b>not</b> use <code>for</code>- or
  41.      * <code>while</code>-loops.
  42.      * @param a
  43.      * @param b
  44.      * @return ?
  45.      */
  46.     public static int recursive(int a, int b) {
  47.        
  48.         // TODO: fill me with code!
  49.  
  50.         return 0;
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement