ICS 211 Homework 5

Recursion

0. Goals and Overview

The goal of this assignment is to experience and practice building recursive implementations. All your code must go into a class called HW05.

1. Ternary Numbers (20%)

In class we discussed the recursive printInt method. This variant, instead of printing, creates a string representation of the integer:

static String intToString(int toPrint, int base) {
  if (toPrint < base) {
    return ("" + toPrint);
  } else {
    return intToString(toPrint / base, base) + (toPrint % base);
  }
}

Use this method with a base of 3 to create the string representation for numbers in ternary -- in this case, ternary means base 3.

For this part of the assignment, create a method public static String[] firstTernaryNumbers(int n) that creates and returns an array containing the ternary numbers 0..n-1.

Also have your HW05 main method call your firstTernaryNumbers and print in ternary all the numbers between 0 and 30 (inclusive, so the first 31 numbers). Your output should start with these lines:

0
1
2
10
11
and end with these lines:
220
221
222
1000
1001
1002
1010

2. Hexadecimal Numbers (20%)

Modify the above intToString method so it can correctly print numbers in hexadecimal, that is, base 16. Since there are only 10 digits (0..9), hexadecimal uses the first six letters of the alphabet to represent the values greater than 9 and less than 16 that cannot be represented by a single digit. a then represents the "digit" 10, b 11, c 12, d 13, e 14, and f 15.

For example, the number 123 is 16 * 7 + 11, and so 123 is written 7b in hex. 124 in hex is then 7c, 125 is 7d, 126 is 7e, 127 is 7f, 128 is 80, 129 is 81.

Some people prefer to use the uppercase versions A..F, but for this assignment you must use the lowercase a..f.

Once you have modified intToString to return the hexadecimal representation, again create a method firstHexNumbers analogous to firstTernaryNumbers.

Finally, have your main method print in hexadecimal all the values between 500 and 524 in hexadecimal. To do this, you have to call firstHexNumbers with a parameter of 525, and start printing from the 500th element of the returned array.

The first number your code prints should be 1f4, and the last should be 20c. This is in addition to what is required by part 1, so your main method should first print the numbers in ternary, then the numbers in hexadecimal.

To be clear, you have to implement the hex conversion code yourself, and it must be along the lines of intToString above. Specifically, for this assignment you are not allowed to use Integer.toHexString(), nor any similar method, unless you have implemented it yourself.

3. Triple Fibonacci Numbers

3.1 Intuitive Computation (20%)

The fibonacci sequence is defined as:

We will modify this to add the preceding three such numbers (instead of only the 2 preceding numbers), and starting the sequence at n=0:

Implement a recursive method fib3 exactly modeled on this definition, i.e. do not try to make your code more efficient.

Then in your main method call your recursive fib3 method in your main method to print the first 12 of these numbers, which are

1
1
1
3
5
9
17
31
57
105
193
355

Note how each number after the first three is the sum of the preceding three numbers.

3.2 Efficient Computation (20%)

Implement a non-recursive method fib3WithHelper which calls a recursive helper method fib3Helper which you also implement. fib3WithHelper and fib3Helper are similar to the code on slide 7 of the February 5th presentation.

Also implement a non-recursive method fib3Iterative that uses a loop to efficiently implement the fib3 function, similar to slide 8 of the February 5th presentation.

4. Runtime Analysis (20%)

This part of the assignment requires you to write and turn in a document, analysis.txt. This file must include your O(...) result as well as your measured times.

Create code to run fib3 with larger and larger values of n, until you can accurately measure the runtime of different calls. To measure the runtime, you may use a watch or similar device, or you may have your code call System.currentTimeMillis before and after the call, and print the difference ("milli" is short for millisecond -- there are 1,000 milliseconds in a second).

Give the runtime you measured for calls on at least 5 different values of n which give 5 distinctly different measurements of the runtime. Then try to figure out what O(...) function of n is most appropriate for the runtimes you measured. Show all your work, including your understanding of the O(...) function and the results of your timing measurements, in analysis.txt.

Turning in the Assignment

Once you are done, find your src directory and navigate to edu.ics211.h05, then turn in to Laulima your HW05.java and analysis.txt directly (without zipping or archiving).