PROBLEM 9 (due Dec 18, 40 points)




Write a VBA function named Sequence that has a single integer parameter, n, which you can assumed to be positive. The function uses n to generate a sequence of integers according to the following rule:

if n = 1 stop
if n is even, replace it with n/2
if n is odd, replace it with 3*n+1


For example, starting with n = 7 produces the sequence of integers

7; 22; 11; 34; 17; 52; 26; 13; 40; 20; 10; 5; 16; 8; 4; 2; 1:
The length of this sequence is 17 (counting the first one n = 7).

As another check to make sure that the functions are written correctly, the input of n = 11 should generate the following sequence of 13 integers:

11; 34; 17; 52; 26; 13; 40; 20; 10; 5; 16; 8; 4; 2; 1;


Many people believe that no matter what n is, as long as it is positive, the above rule always generates a finite (terminating) sequence of integers.

Your function has to generate this sequence for any given positive value of n. You only need to return the length of the sequence, but you do not need to return the sequence itself.

Next, in the active worksheet, write n in cell B1, and write Length in cell D1. Put 2, 3, ...11 in cells B2, B3, ..., B11. These values will be used to generate the various sequences using the Sequence function that you wrote. The corresponding lengths of these sequences returned by the function should be placed respectively in cells D2, D3, ..., D11.