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 lab7 $ cd lab7
Write a python program that prints all multiples of 7 less than 100. Your code for this program should be no longer than 5 lines long.
$ python3 practice1.py 7 14 21 ... omitted for space ... 91 98
Write a python program that counts down from 10. Your
program must use the range
function.
$ python3 practice2.py 10 9 8 7 6 5 4 3 2 1 0
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 program in a file called leibniz.py. This function asks the user to input an integer, which is the number of terms to compute in the infinite series. It should use this value 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 program by running it multiple times with different inputs. Make sure your code follows the course's code conventions.
$python3 leibniz.py How many terms? 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.
Computer displays are made up of a grid of tiny lights. Because the displays are not continuous, curved objects must be broken into many non-continuous pieces. So curves are often simply represented as many straight lines. In this activity you will approximate a curve by drawing many lines.
In Emacs, create a Python program in a file called curve.py. Your program should use the turtle module to draw a curve using overlapping straight lines between the x-axis and the y-axis.
Do not hard code the location of the line segment end points. Your program should have a variable that determines the size of the drawing. Your program should calculate the end point locations based on the size variable. Make sure you use proper variable names and follow the courses code conventions.
$ python3 curve.py
turtle.goto
.turtle.goto
function to move the turtle
to the start point, then use the turtle.goto
function again to move the turtle to the end point.turtle.up
and turtle.down
functions to prevent the turtle
from drawing a line when it moves to the line's start end
point.This same technique can be used to draw a myriad of different curves, by changing the length and angle of the axes. Add variable to your program that specify the length and angle of each of the axes to create curves like the following.