As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.
$ cd ~/cs120/labs $ mkdir lab9 $ cd lab9
Some mathematical expressions are bit harder to evaluate than others. You could probably add numbers all day, but comuting square roots is much harder. Factorials are another good example of this; Factorials require an iterated evaluation of mathematical expressions. While there's no single operator to compute factorials in Python, you can write a function that can compute the value of factorials.
Create the function compute_factorial(operand)
in a file
called factorial.py. This function takes an integer
as a parameter, which is the number the user wants to compute the
factorial of. It should
use this parameter and return the factorial of the input.
Recall that a factorial can be computed in the following manner:
$$ n! = n \times (n - 1) \times (n - 2) \times \ldots \times 1$$
Make sure to test your function by calling the function multiple times with different parameters. Make sure your code follows the course's code conventions.
Function Parameters | Expected Output |
---|---|
0 | 1 |
1 | 1 |
2 | 2 |
5 | 120 |
10 | 3628800 |
Another equation that got a lot of publicity recently is the Ramanujuan Summation. It states that the infinite sum of the positive integers is the value: $$-\frac{1}{12}$$ Depending on how you define your operations this summation is correct. This result is a big deal in String Theory. Does Python follow these rules?
Write a function that uses a for loop to sum the integers in the range [0, n). Try this function for arbitrarily large values. Do you ever get to the specified value?
The mathematical constant π is an irrational number that represents the ratio between a circle's diameter and its circumference. The first algorithm designed to approximate π was developed by Archimedes in 250 BC, and was able to approximate its value within 3 significant figures. Today we know that there are an infinite number of digits in π, so lets use a little more sophisticated approach to estimate its value.
Create the function estimate_pi(duration)
in a file
called leibniz.py. This function takes an integer as a
parameter, which is the number of terms to compute in the infinite series. It should
use this parameter to compute the Leibniz approximation, discussed below.
The Leibniz approximation relies on the fact that:
$$ \frac{\pi}{4} = \frac{1}{1} - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} - \ldots $$
Which can be rewritten to approximate the true value of π:
$$ \pi = \frac{4}{1} - \frac{4}{3} + \frac{4}{5} - \frac{4}{7} - \ldots $$
Make sure to test your function by calling the function multiple times with different parameters. Make sure your code follows the course's code conventions.
Function Parameters | Expected Output |
---|---|
0 | 0 |
1 | 4 |
10 | 3.0418396189294032 |
100 | 3.1315929035585537 |
1000 | 3.140592653839794 |
The value you get from the Leibniz formula will be incredibly
close. However, it will never meet the true definition of π.
To see how close it does get, compute the approximations
for all equations from length 1 to length
1000. For each approximation, subtract the value
of math.pi
from it to compute the error. Sum up all
of these errors, and print the average error.