SIMS 255: Lab 8

Wednesday, October 16, 2002

Recursion

Exercises

  1. Write a recursive Java method that takes an integer as its parameter, computes n! (n factorial), and returns the result as an integer. Recall that n! is 1 * 2 * 3 * ... * n. Note also that:
    0! = 1 and
    1! = 1

  2. Complete the Java code to recursively evaluate the sum: sum = 1 + 1/2 + 1/3 +...+1/n, where n >= 1.
    	 double sum(int n)		// n>=1
    	 {
    		  if (___________)
    			   return _____________;
    
    		  return ______ + sum(______);
    	 }
    
  3. Write a recursive function called letters that takes a lower case letter as its parameter and displays the sequence of letters from 'a' to the given letter.
    For example:
    Letters('a'); // displays a
    Letters('d'); // displays abcd

  4. Write a recursive function called reverse that takes an integer as its parameter and returns an integer that is the result of reversing its digits.
    For example:
    System.out.print(reverse(-12)); // returns -21
    System.out.print(reverse(1234567)); // returns 7654321

  5. Write a recursive Boolean function called powerof3 that takes a single positive integer as its parameter and returns true iff the integer is a perfect power of 3 such as 1, 3, 9, 27, 81, ...
    For example:
         if (powerof3(81))
              System.out.println("81 is a power of 3.");
         else
              System.out.println("81 is not a power of 3.";
    
    displays 81 is a power of 3

  6. [Main, ch. 8, programming project 10] Write a program that asks the user to thnk of an integer between 1 and 1,000,000 and then guesses the number through a series of yes/no questions. To guess the number, the program calls a recursive method guess that has two parameters, low and high. The precondition for the method requires that the user's number lie in the range low...high, so that the program's initial call is to guess(1, 1000000). What is a good stopping case for guess, when it can guess the user's number with little or no work? Answer:
    if (low == high)
    then there is only one possible number, and the method can guess that number. On the other hand,
    if (low < high)
    then the method should calculate a point near the middle of the range:
    midpoint = (low + high) / 2;
    Then the method asks the user whether the midpoint is the correct number. If so, then the method is finished. On the other hand, if the midpoint is not the user's number, then the method asks whether the correct number is larger than midpoint. If so then the method knows that the user's number lies in the range midpoint + 1 to high, and a recursive call can be made to solve the smaller problem of finding a user's number in the range midpoint + 1 to high. On the other hand, if the user's number is not larger than midpoint, then a recursive call can be made to solve the smaller problem of finding a user's number in the range low to midpoint - 1. This method of searching is called binary search, which we will explore further in Chapter 11.


LZ -- last modified 10/16/02