This problem set covers Chapter 11 (Part 1).

Factorial (5 points)

(Programming Projects #3, Page 837) One of the most common examples of recursion is an algorithm to calculate the factorial of an integer. The notation n! is used for the factorial of the integer n and is defined as follows:

  • 0! is equal to 1
  • 1! is equal to 1
  • 2! is equal to 2 * 1 = 2
  • 3! is equal to 3 * 2 * 1 = 6
  • 4! is equal to 4 * 3 * 2 * 1 = 24
  • ...
  • n! is equal to n * (n - 1) * (n - 2) * ... * 3 * 2 * 1
  • An alternative way to describe the calculation of n! is the recursive formula n * (n-1)!, plus a base case of 0!, which is 1. Write a static method that implements this recursive formula for factorials. Place the method is a test program that allows the user to enter values for n until signaling an end to execution.

    Name this class Factorial and include it in the edu.cuny.lehman.cmp326.ps9 package

    Geometric & Harmonic Progression (5 points)

    (Programming Projects #6, Page 838) A Geometric Progression is defined as the product of the first n integers and is denoted as:

    n
    Geometric(n) = i
    n=1

    Where this notation means to multiply the integers from 1 to n. A Harmonic Progression is defined as the product of the inverses of the first n integers and is denoted by:

    n
    Harmonic(n) = (1/i)
    n=1

    Both types of progressions have an equivalent recursive definition:

    n - 1
    Geometric(n) = n * i
    n=1
    n - 1
    Harmonic(n) = (1/n) * (1/i)
    n=1

    Write static methods that implement these recursive formulas to compute geometric(n) and harmonic(n). Do not forget to include the base case, which is not given in these formulas, but which you must determine. Place the methods in a test program that allows the user to compute both geometric(n) and harmonic(n) for an input integer n. Your program should allow the user to enter another value for n and repeat the calculation until signaling an end to the program. Neither of your methods should use a loop to multiply n numbers.

    Name this class Geomonic and include it in the edu.cuny.lehman.cmp326.ps9 package

    Fibonacci Sequence (5 points)

    (Programming Projects #7, Page 839) The Fibonacci Sequence occurs frequently in nature as the growth rate for cetain idealized animal populations. The sequence begins with 0 and 1, and each successive Fibonacci number is the sum of the two previous Fibonacci numbers. Hence, the first ten Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21 and 34. The third number in the series is 0 + 1, which is 1; the fourth number is 1 + 1, which is 2; the fifth number is 1 + 2, which is 3; and so on.

    Besides describing population growth, the sequence can be used to define the form of a spiral. In addition, the ratios of successive Fibonacci numbers in the sequence approach a constant, approximately 1.618, called the "golden mean." Humans find this ratio so aesthetically pleasing that it is often used to select the length and width ratios of rooms and postcards.

    Use a recursive formula to define a static method to compute the nth Fibonacci number, given n as an argument. Your method should not use a loop to compute all the Fibonacci numbers up to the desired one, but should be a simple recursive method. Place this static recursive method in a program that demonstrates how the ratio of succesive Fibonacci numbers converges. Your program will ask the user to specify how many Fibonacci numbers it should calculate. It will then display the Fiboanacci numbers it should calculate. It will also display the ratio of the current and previous Fibonacci numbers on each line. (The initial ratios do not make sense, and should, therefore, be excluded.) The output should look something like the following if the user enters 5:

  • Fibonacci #1 = 0
  • Fibonacci #2 = 1
  • Fibonacci #3 = 1; 1/1 = 1
  • Fibonacci #4 = 2; 2/1 = 2
  • Fibonacci #5 = 3; 3/2 = 1.5
  • Name this class Fibonacci and include it in the edu.cuny.lehman.cmp326.ps9 package