CMPS-224 Lab 6 Recursion with Python

Eddie Rangel
Department of Computer and Electrical Engineering and Computer Science
California State University, Bakersfield

Introduction

Goals: Understand how to implement a recursive function to solve a factorial. In computer science, recursion is a method of problem solving where the solution is dependent of the solution of a smaller instance of the same problem.

Resources: Python 3 Documentation Documentation Python Lab 6 Materials

Files Needed:

Write a program that uses a recursive function to solve for a factorial. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep count of the number of comparisons it makes. Display these values on the screen.

Psuedo Code Implementation to find the factorial of some value n.

        n! = n * (n-1)!
        n! = 1 if n = 0 or n = 1 
        Example:
        5! = 5 * 4 * 3 * 2 * 1 = 120
    

Function Header for factorial function taking an arugment n.

    
        def factorial(n):
    
    

Conditions of Satisfactions

    
        calling your function with the following values.
        factorial(2) should return 2
        factorial(3) should return 6, 
        factorial(4) should return24,
        factorial(5) should return 120.
    
    

What to turn in

Show the programs running to me or our TA.