10 4 Recursion and Looping Recursion with Two

  • Slides: 15
Download presentation
10. 4 Recursion and Looping, Recursion with Two Parameters

10. 4 Recursion and Looping, Recursion with Two Parameters

Recursion can be Loops � The pool rack example could be implemented using a

Recursion can be Loops � The pool rack example could be implemented using a for loop. � It is also possible to write recursive methods that accomplish things that you might do with a while loop.

Recursion with a Different Base Case �A recursive definition is given below for finding

Recursion with a Different Base Case �A recursive definition is given below for finding how many times the constant value 2 will go evenly into another number. � The value 1 which is added in the recursive case counts how many times recursion occurs. � This count gives the result of the division. � Notice that in the recursive case n is no longer decremented by 1. � It is decremented by 2.

Recursion with a Different Base Case, cont. � The test for the base case

Recursion with a Different Base Case, cont. � The test for the base case is an inequality rather than an equality since the values that n takes on vary depending on whether it is initially odd or even. � Recursive case: n >= 2 � f(n) = 1 + f(n – 2) � Base case: n < 2 � f(n) = 0

Recursion with a Different Base Case, cont. � Here is a sample implementation. �

Recursion with a Different Base Case, cont. � Here is a sample implementation. � The recursive method recursive. Division. By. Two() is typed int and takes an int parameter named dividend. � public static int recursive. Division. By. Two(int dividend) �{ � if(dividend < 2) � return 0; � else � return 1 + recursive. Division. By. Two(dividend – 2); �}

Recursion with Two Parameters � Building on this example, it is possible to write

Recursion with Two Parameters � Building on this example, it is possible to write a recursive definition for the general division function. � This is shown on the next slide. The function has two parameters. � Adding 1 in the recursive case again counts how many times the recursion occurs. � In this definition the first parameter for the recursive case is dividend – divisor. � The amount decremented each time is always the same, but divisor can be any (positive integer) value. � The second parameter is the divisor, which is passed down unchanged through all of the calls.

Recursion with Two Parameters, cont. � Recursive case: dividend >= divisor � f(dividend, divisor)

Recursion with Two Parameters, cont. � Recursive case: dividend >= divisor � f(dividend, divisor) = 1 + f(dividend – divisor, divisor) � � Base case: dividend < divisor � f(dividend, divisor) = 0

Recursion with Two Parameters, cont. � Here is a complete driver program and an

Recursion with Two Parameters, cont. � Here is a complete driver program and an implementation of this function. � As usual, this is not a general implementation, but it works for appropriate input values and illustrates the key points. � Notice that a variable ret. Val is used in the implementation.

Recursion with Two Parameters, cont. � � import java. util. Scanner; public class Gen.

Recursion with Two Parameters, cont. � � import java. util. Scanner; public class Gen. Rec. Div. Prog { � public static void main(String[] args) � { � Scanner in = new Scanner(System. in); � int ret. Val; � int dividend, divisor; � System. out. print("Enter the dividend: "); � dividend = in. next. Int(); � System. out. print("Enter the divisor: "); � divisor = in. next. Int(); � � } } ret. Val = Gen. Rec. Div. Class. gen. Rec. Div(dividend, divisor); System. out. println(ret. Val);

Recursion with Two Parameters, cont. � � � � public class Gen. Rec. Div.

Recursion with Two Parameters, cont. � � � � public class Gen. Rec. Div. Class { public static int gen. Rec. Div(int dividend, int divisor) { int ret. Val = 0; if(dividend < divisor) return ret. Val; else { ret. Val = 1 + gen. Rec. Div(dividend - divisor, divisor); } } } return ret. Val;

Recursion for a logarithm � Just as division can be defined by repeated subtraction,

Recursion for a logarithm � Just as division can be defined by repeated subtraction, finding a logarithm can be defined by repeated division. � For a given number to find the log of, which will be represented by the variable name to. Find. Log. Of, and a given base, a simple recursive definition of the logarithm function would look like this: � Recursive case: to. Find. Log. Of >= base � f(to. Find. Log. Of, base) = 1 + f(to. Find. Log. Of / base, base) � � Base case: to. Find. Log. Of < base � f(to. Find. Log. Of, base) = 0

Recursion for a logarithm, cont. � The following example contains two static methods, both

Recursion for a logarithm, cont. � The following example contains two static methods, both of which find a simple logarithm by doing repeated division. � One does so with looping and the other with recursion. � For positive input consisting of a given double to. Find. Log. Of and a given integer base, the code gives as output the largest integer value less than or equal to the logarithm for that number and base.

Recursion and looping for a logarithm, cont. � � � � � � �

Recursion and looping for a logarithm, cont. � � � � � � � import java. util. Scanner; public class Test. Rec. And. Loop { public static void main(String[] args) { Scanner in = new Scanner(System. in); double to. Find. Log. Of; int base; double answer; System. out. println("What number would you like to find the log of? "); to. Find. Log. Of = in. next. Double(); System. out. println("What should the base of the logarithm be? "); base = in. next. Int(); answer = Recursion. And. Looping. my. Log. Looping(to. Find. Log. Of, base); System. out. println("The looping answer is: " + answer); answer = Recursion. And. Looping. my. Log. Rec(to. Find. Log. Of, base); } } System. out. println("The recursive answer is: " + answer);

Recursion and looping for a logarithm, cont. � � � � � � �

Recursion and looping for a logarithm, cont. � � � � � � � � public class Recursion. And. Looping { public static int my. Log. Looping(double to. Find. Log. Of, int base) { int ret. Val = 0; while(to. Find. Log. Of >= base) { to. Find. Log. Of = to. Find. Log. Of / base; ret. Val++; } return ret. Val; } public static int my. Log. Rec(double to. Find. Log. Of, int base) { int ret. Val = 0; if(to. Find. Log. Of < base) return ret. Val; else { to. Find. Log. Of = to. Find. Log. Of / base; ret. Val = 1 + my. Log. Rec(to. Find. Log. Of, base); return ret. Val; } } }

Different Way for the Recursive Method �Note that the following version of the recursive

Different Way for the Recursive Method �Note that the following version of the recursive method is shorter, but it might also be a little bit harder to understand. � � public static int my. Log. Rec(double to. Find. Log. Of, int base) { int ret. Val = 0; � � � if(to. Find. Log. Of > base) { to. Find. Log. Of = to. Find. Log. Of / base; ret. Val = 1 + my. Log. Rec(to. Find. Log. Of, base); } � � return ret. Val; }