Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class IterativeToRecursive {
- public static void main(String[] args) {
- for (int a = 0; a < 10; a++)
- for (int b = 0; b < 10; b++) {
- if (iterative(a, b) == recursive(a, b))
- System.out.println(
- "iterative(" + a + ", " + b + ") == " +
- "recursive(" + a + ", " + b + ")");
- else
- System.out.println(
- "iterative(" + a + ", " + b + ") != " +
- "recursive(" + a + ", " + b + ")");
- }
- }
- /**
- * What does this function do?
- */
- public static int iterative(int a, int b) {
- while (b != 0) {
- if (a < b) {
- int temp = a;
- a = b;
- b = temp;
- } else {
- int aNew = b;
- int bNew = a % b;
- a = aNew;
- b = bNew;
- }
- }
- return a;
- }
- /**
- * Implement the body of this function so that it calculates the same
- * results as the function <code>int iterative(int a, int b)</code>.
- * Use recursion and do <b>not</b> use <code>for</code>- or
- * <code>while</code>-loops.
- * @param a
- * @param b
- * @return ?
- */
- public static int recursive(int a, int b) {
- // TODO: fill me with code!
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement