CS 110 Fall 09 

Lab 5: Series and Sequences

Due: Thursday, October 8th

 

In this lab you'll practice describing numerical sequences and patterns and to create a number of programs to accomplish various tasks using loops. As usual, READ THE LAB CAREFULLY!

Part 1 - Sequences

Problems 1 through 4 contain the initial entries in a few sequences of numbers.  For each of these problems, specify the following three things:

For example, if the given sequence was 1, 3, 5, 7, 9, 11,... then you answers should be:

Write your solutions in a text file called sequences.rtf.

1. 5, 6, 7, 8, 9, 10,...

2. 0, 3, 6, 9, 12, 15,...

3. 0, 1, 4, 9, 16, 25,...

4. 0, 1, 3, 7, 15, 31,...

The Fibonacci numbers are one of the most famous sequences of numbers, and are defined as follows.  The first two Fibonacci numbers are defined to be F(0) = 0 and F(1) = 1.  Each later Fibonacci number is defined to be the sum of the previous two.  More formally, we'd say that F(i) = F(i-1) + F(i-2) for all i > 1.  So the first few Fibonacci numbers are:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, etc.

5. Specify the next three Fibonacci numbers.

6. Let S(x) be the sum of the first x Fibonacci numbers.  So S(0) = 0 (F(0) = 0).  And S(1) = 1 (F(0) + F(1) = 1).  And S(2) = 2 (F(0) + F(1) + F(2) = 2).  Compute the values of S(x) for all values of x from 0 through 8.

7. Conjecture a general formula for the value of S(x) in terms of the Fibonacci numbers (without the summation).

Part 2 - Pattern recognition

Below are a few patterns.  Give the following line in the pattern, and write a description (in English) of what line n of each pattern would look like, for a general n.

For example, if the pattern was

1

12

123

1234

12345

Then a valid answer would be: 123456 is the next line, and in general, line n consists of all integers from 1 to n in increasing order.

8.

111

222

333

444

555

9.

1

22

333

4444

55555

10.

1

2211

333222111

4444333322221111

5555544444333332222211111

Part 3 - Beer!

Many of you may know the childrens' (and sailors'?) song 99 bottles of beer on the wall.  If not, no worries, I'll fill you in.  The first line of the song goes something like this:

99 bottles of beer on the wall, 99 bottles of beer... if one of those bottles should happen to fall, 98 bottles of beer on the wall!

The next line is just like the first, except the numbers have gone down by 1 each; instead of 99... 99... and 98 bottles of beer, we have 98... 98... and 97 bottles of beer.  In the next line it goes 97... 97... 96.  When you get to 0 bottles of beer... actually come to think of it, I have no idea how the song ends, but I sort of doubt anyone has ever gotten that far, so let's not worry about it.  Your task is to write a program that prints out the first 15 lines of this absurdly ridiculous song.  Call your program Beer.py.  Don't use loops.  Copy-paste is your friend.

Now use loops to accomplish the same task, but go until there are 0 bottles of beer on the wall.  Call the resulting program LiteBeer.py .

Part 4 - Basic loop challenges

For this Part, there are a bunch of mini programs you'll need to write.  DO NOT SUBMIT THESE!  They are merely meant to help you figure out the more complicated problems in Part 5.  That said, it will be much harder to complete Part 5 if you do not complete Part 4.  You should save these to your own personal student directory.  Test them to make sure they work. 

  1. Write a program that prints all even integers between -5 and 37 in increasing order, all on a single line with a space between each number.
  2. Write a program that reads in a positive integer x from the user.  Then print on a single line all multiples of x from x to x2.
  3. Write a program that reads in a positive integer x from the user and computes the sum of all integers from 1 to x.
  4. Read in positive integers n and f.  Declare a boolean variable called divides.  Set divides to be true if n is divisible by f, and false otherwise.  Report your result.

Part 5 - Tougher loop challenges

Write programs to solve the following seven problems.  These programs are to be submitted to your assignment drop box.  Be sure to use the specified program name and carefully check that each program actually does what it should.

  1. [Factorial.py]  Read in a positive integer x from the user.  Compute the product of all integers from 1 to x.  The value of this product is known as "x factorial" and is usually written as "x!"  Print the result of your computation.  As usual, you should prompt the user for input and also have text explaining the output.  For example, "Please enter a positive integer" and the output should be something like "6! is 720."
  2. [Powers.py ]  Read in positive integers x and p.  Use a loop to compute and print the value of  xp.
  3. [Prime.py ]  Read in a positive integer n.  We say that n is prime if n is not divisible by any number between 2 and n-1.  Use a loop and ideas from Part 4 to determine whether n is prime.
  4. [Fib.py ]  Read in a positive integer n.  Compute and print the first n Fibonacci numbers.
  5. [Nested9.py ]  Read in a positive integer n.  Use nested loops to generate the first n lines of the pattern given in Part 2, problem 9.

Part 6 - Shades of blue

There are a number of methods (functions) associated with the GraphWin class that are used to interact with individual pixels (rather than drawing larger shapes).  In particular, the method

plotPixel(x, y, Color)

sets the color of the pixel at coordinates (x,y) to have the color given by the string Color. Recall that you can create an appropriate color string by invoking the function:

color_rgb(red, green, blue)

where the parameters red, green, and blue are integers with values from 0 to 255 that specify the intensity of each of the color components. 

Create a program called [Shaded.py] that creates an image which is 256 x 256 pixels large.  Loop over all pixels in the image (nested loops will probably be needed here) and set their color according to the following scheme: set the red value of any given pixel to be the value of its x coordinate; set the blue value to be the value of its y coordinate; and set the green value to be 0.  Then display the image.  Remember, in an image that is x pixels wide, the index of the last column is x-1 (and similarly for the vertical indexing).

Part 7 - Uploading your lab

Upload all your programs to the folder Lab05.  This folder should contain:

  • Sequences.rtf
  • Beer.py
  • LiteBeer.py
  • Factorial.py
  • Powers.py
  • Prime.py
  • Fib.py
  • Nested9.py
  • Shaded.py