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 lab6 $ cd lab6
Write a Python program which returns the
factorial of a number specified in a variable called
TO_COMPUTE
. The factorial of a number \(n\) can be
computed using the equation:
Write a program which computes computes the Nth power of
2, where N is a constant defined in the top of the file.
You should not use the **
operator for this problem.
The Fibonacci sequence is a sequence of numbers that looks like:
$$1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, \ldots$$
Each number in the sequence, after the first two, is the sum of the previous two numbers in the sequence.
In Emacs create a Python program in a file called fibonacci.py that prints the Fibonacci sequence. The program should prompt the user to enter the length of the series to print.
Your program does not need to worry about special cases where the user enters a number less than or equal to 0.
$ python3 fibonacci.py enter the sequence length: 8 1 1 2 3 5 8 13 21
One quirk about fibonacci numbers is their relation to right triangles. Starting with the number 5, every second fibonacci number represents the hypotenuse of a 3-4-5 right triangle. Three numbers of this form are referred to as a pythagorean triple. Create a new program called triples.py, which is a modification of the above program that asks the user for some number n, and prints the first n pythagorean triples.
Last class, we saw how we could create a spiral by always moving forward by the same amount, and turning after each movement. This ended up with a spiral, which looked a little jagged (or really jagged). One way we can solve this problem is using circles in Turtle, and increasing the radius for each external circle.
In Emacs create a Python program in a file
called spiral.py that draws a circular spiral. The
program should prompt the user the number of spirals to draw. You
should draw a series of half-circles (created using
pen.circle(radius, 180)
). Each
time a half circle is drawn, you should double the radius the next
circle is drawn in.
Your program does not need to worry about special cases where the user enters a number less than or equal to 0.
$ python3 spiral.py Enter number of spirals: 8
Using the fibonacci sequence as the amount that the circle radii increase will result in a very cool looking spiral, as seen below. Modify your code to use the fibonacci sequence.