## CS 2401 Assignment #5

Due Date: Wednesday, July 11.

Objective: The goal of this assignment is to practice recursion.

Background. Billions of people watch the Olympics, and who knows, maybe the extraterrestrials are watching them too. For them, the competitions are exciting, but the numerical results may be difficult to understand: for us, with ten fingers on both hands, it is natural to use a decimal system, but creatures from other planets may use different bases. Let us help them by designing a universal translator that, given a positive integer n and a base B, prints the description of the given number in the given base.

Examples:

• Creatures with three hands -- each of which have 4 fingers -- can use 12-based system. In this system, a decimal number 107 has the form 8 11, since 107 = 8 * 121 + 11.
• For a human-hand-like three-hand creature with 3 * 5 = 15 fingers, the same number is represented as 107 = 7 2, since 107 = 7 * 151 + 2.
• For a one-hand creature with 6 fingers, 107 = 2 5 5, since 107 = 2 * 62 + 5 * 61 + 5.
(Note that we always add space between digits, to avoid confusion: e.g., to make sure that 8 11 = 8 * 121 + 11 = 10710 is different from 8 1 1 = 8 * 122 + 1 * 121 + 1 = 1,06510.)

Algorithm. The algorithm for transforming the number into base B is as follows:

• if the number n is smaller than the base B, then this same number is the desired representation;
• otherwise, if n ≥ B, we take the ratio n / B, convert it to base B (by using the same algorithm), and then append the remainder n % B at the end.

Example:

• Since 107 ≥ 6, we compute 107 / 6 = 17. When we transform 17 into base 6, we get 2 5 (see below). To get the desired representation of 107, we need to also print the remainder 107 % 6 = 5. The result will be the desired sequence 2 5 5.
• To convert 17, since 17 ≥ 6, we compute 17 / 6 = 2. When we convert this number into base 6, we get 2 (see below). To get the desired representation of 17, we need to also print the remainder 17 % 6 = 5. The result will be the sequence 2 5.
• To convert 2, since 2 < 6, we simply print 2.
Assignment. Write a recursive method that, given the number n and the base B, prints the result of converting n into base B.

Testing. Test your method on examples where it is easy to manually compute the result: B = 10 and B = 2 (binary code).