Practice Problem 1
Write a function rational_sum(n)
which returns the
sum of the rational numbers 1n:
Practice Problem 2
Write a function factorial(number)
which returns the
factorial
of the specified number. The factorial of a number n can be
computed
using the equation:
NOTE: Do not use the math module in this practice activity.
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.
Details
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:
π4=11−13+15−17−…
Which can be rewritten to approximate the true value of π:
π=41−43+45−47−…
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.
Test Cases
Function Parameters | Expected Output |
---|---|
0 | 0 |
1 | 4 |
10 | 3.0418396189294032 |
100 | 3.1315929035585537 |
1000 | 3.140592653839794 |
Hint
Challenge
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.
Submission
Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.