CPSC120 Lab 4

Repetition

  1. Fibonacci Number

    Create a function called compute_fibonacci_number in a file called fibonacci.py. The function should take a positive integer, n > 2, and return the nth number in the Fibonacci sequence. The Fibonacci sequence is:

    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, . . . 

  2. Babylonian Square Root

    Create a function called compute_square_root in a file called square_root.py. The function should use the Babylonian iterative method to compute an approximation to the square root of a positive integer. The Babylonian method repeatedly calculates a new, more accurate, approximate value for a square root given an approximate value for the square root. The equation for the new approximation is:

    sʹ = (1 / 2)(s + (n / s))

    Where s is an approximate value for the square root of n and sʹ is a more accurate approximation for the square root of n. The initial value for s can be computed based on the number of digits in n using the following equation:

    s = 4 ⋅ 10(d − 1) / 2

    Where d is the number of digits in n. The parameters to the compute_square_root function should specify the positive integer to compute the square root of and the number of digits in the positive integer. The function should perform a fixed number of iterations to compute the approximate square root.

  3. Triangle of Stars

    Create a function called print_star_triangle in a file called star_triangle.py. The function should print a triangle made of asterisk characters to the command line like the one below.

            *
           * *
          * * *
         * * * *
        * * * * *

    The function should take a positive integer that specifies the number of rows in the triangle. The function should not return anything.

Submission

Submit a zip file of your code on the course Inquire site that uses your last names as the file name.