Optional Exercise 1
Simple Python Exercises

out: 11/03, due: 11/12, late: 11/15

This assignment is intended to give you some experience in writing and debugging a simple Python program. You can use python on gl or on your own computer, if it is installed there. We will test your code using python on the gl server, so be sure it runs there.

You are to implement a module, pyex1.py, that includes functions to compute the following functions and any others that you find useful. See the modules section of the Python Tutorial for a brief introduction to modules.

You can use the file pyex1stub.py as an example to get you going. Note that this has some Addison functions in it which we add as hints for how you might go about writing your solution. We've used the Python function pass as a place-holder for your code.

1. sum_mult_3_5

There are four natural numbers below 10 that are multiples of 3 or 5, namely 3, 5, 6 and 9. They sum to 23. Write a function sumup_mult_3_5(n) that takes a natural number n and returns the sum of all of the natural numbers less than n that are multiples of three and five. Hints:

  • You will find the modulo operator % useful. Try evaluating 15%5 or 11%3 to see how it works.
  • You can iterate over all the values of i between 1 and N like this.
    for i in range(1,N):
        # your code
        # here
    

2. Hailstone sequence

Consider the following problem: Choose a positive integer and repeatedly do the following: if the number is 1, quit; if the number is even, cut it in half; and if the number is odd, multiply it by 3 and add 1. For example, if you start with the number 17, you get the sequence: 17 52 26 13 40 20 10 5 16 8 4 2 1. Generate the sequence from a starting number and you'll find the numbers go up and down like a hailstone in a cloud before it plummets to earth (e.g., one).

Does this procedure eventually reach one and stop for every choice of starting number? So far, this is an unsolved problem -- no one has yet proved that the process always results in 1, and no one has yet found a counterexample. This problem was first posed by L. Collatz in 1937 and goes under several names: the Collatz conjecture, the '3n+1 conjecture', the Ulam conjecture, and the Syracuse problem. The sequence is also commonly called the hailstone sequence.

John Conway proved that the original Collatz problem has no nontrivial cycles of length less than 400. Lagarias (1985) showed that there are no nontrivial cycles with length less than 275,000. Conway (1972) also proved that Collatz-type problems can be formally undecidable. The conjecture has been check by computer for all start values up to 1.2 × 10**12, but a proof of the conjecture has not been found. Paul Erdös said about the Collatz conjecture: "Mathematics is not yet ready for such problems." He offered $500 for its solution. There are some heuristic, statistical arguments supporting the conjecture: if one considers only the odd numbers in the sequence generated by the Collatz process, then one can argue that on average the next odd number should be about 3/4 of the previous one, which suggests that they eventually hit the bottom.

Write a function hailstone(n) that takes a positive integer n and returns a list of the numbers in its hailstone sequence.

Hints:

  • If you wanted to construct a list of tuples (n,m) such that 0<n<10 and m=n*n, you could do it this way:
    answer = []
    for n in range(10):
        answer.append((n, n*n)

Testing

You can test your pyex1.py module using the file test.py which you can download and place in the same directory that holds your pyex1.py file. Once there, you can run the test file by entering the following unix command line

python test.py

this should produce output like test.out. Note, we generated this output with the command

python test.py > test.out