COSC 235

Programming & Problem Solving


Project 4


Due October 6th (Friday) by 11:59PM

Overview

This project will give you experience with writing functions, as well as using looping.

To Do

Write a Python program (call it 'e.py') by completing the following:

  1. Write a function factorial(n) that returns the factorial of a number. Recall that a factorial is defined as n! = 1 x 2 x 3 x .... x (n-1) x n.
  2. Write a function e(n) that approximates the value of the number e. The value e (Euler's number, the base of the natural logarithm) is defined using an infinite sum as e = 1 + (1/(1!)) + (1/(2!)) + (1/(3!)) + ... . The parameter for the function represents how many terms to include from the series (the more terms you add, the more accurate the number becomes). For example, calling e(4) should return the value 1 + (1/1) + (1/2) + (1/6) ≈ 2.667. You should make use of the factorial function from the previous step.
  3. Write a main() function that asks the user for the number of terms to use for the approximation of the number e, then calls the e() function you wrote.
  4. Also in main(), compare the value you computed to the value stored in the math library (math.e). Compare them by computing the absolute error, which can be computed by taking the absolute value of (yourComputedValue - math.e).
  5. Make sure to call your main() function so that your program actually runs.
  6. Make sure to document your program by adding an author and date at the top, as well as adding several comments throughout the code.

Sample Run of the Program

Your program output should match the following exactly (note that some of it is user input that will not be known until the program runs):


        The e Approximation Program

        How many terms do you want to use for the approximation: 3

        The approximated value of e is 2.5, which has an error of 0.2182818284590451 .
    

Submit

To submit, save your Python program in a file named 'e.py', and submit it to Moodle by the due date.