Recursive Programming Assignment: Solving the Collatz Problem - Prof. D. Buell, Assignments of Computer Science

A homework assignment for writing a recursive program to solve the collatz problem, also known as the syracuse problem or the 3n + 1 problem. The problem and its recursive solution, as well as the need to include a step counter and maximum value tracker in the program. The document also discusses the significance of the collatz problem in mathematics and its computational paradigm.

Typology: Assignments

Pre 2010

Uploaded on 09/02/2009

koofers-user-grz
koofers-user-grz 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Homework Assignment 3
You are to write a recursive program for the Collatz problem, also known as
the Syracuse problem or the 3n+ 1 problem.
The problem is this. Choose any integer n. Apply the following function to
n.
if n is even
n <-- n/2
else
n <-- 3n+1
and keep repeating this function until you reach n= 1.
For example, if we start with 7, then since 7 is odd, we transition from
n= 7 to n= 3 ×7 + 1 = 22. Since 22 is even, we divide by 2 to get 11. This
is now odd, so we have 11 3×11 + 1 = 34. This is even, so 34 becomes
17. Then 17 becomes 52, which becomes 26, which becomes 13, which becomes
40, which becomes 20, which becomes 10, which becomes 5, which becomes 16,
which becomes 8, which becomes 4, which becomes 2, which becomes 1, and
we are done.
You are to write a class containing a method to iterate this function recur-
sively: given an input n, then unless the input value is 1, your method should
compute either n/2 or 3n+ 1 as appropriate and call the method recursively
with the new value as the input parameter.
To be able to study the behavior of this function, you should also include in
your Collatz class a static variable stepCount that counts how many times,
for any given n, the function has to be called before it iterates down to 1. You
should also have a static variable maxFound that keeps track of the maximum
value computed in the process of iterating the function.
Your main program should read a beginning and an ending value for n
and run a loop from the beginning to the ending value. In each iteration you
should print out the number of steps taken in iterating to the value 1. (You
may assume that the function will always get to 1 eventually; if you happen to
find a value for which it seems not to, you’ll be famous.)
The value of maxFound should be printed out each time you discover a new
maximum.
Sample output can be found on the website. Please look at the small values
carefully to make sure that you are counting iteration steps in the same way
that the sample output is counting iteration steps and not off by one.
pf2

Partial preview of the text

Download Recursive Programming Assignment: Solving the Collatz Problem - Prof. D. Buell and more Assignments Computer Science in PDF only on Docsity!

1

Homework Assignment 3

You are to write a recursive program for the Collatz problem, also known as the Syracuse problem or the 3n + 1 problem. The problem is this. Choose any integer n. Apply the following function to n.

if n is even n <-- n/ else n <-- 3n+ and keep repeating this function until you reach n = 1. For example, if we start with 7, then since 7 is odd, we transition from n = 7 to n = 3 × 7 + 1 = 22. Since 22 is even, we divide by 2 to get 11. This is now odd, so we have 11 → 3 × 11 + 1 = 34. This is even, so 34 becomes

  1. Then 17 becomes 52, which becomes 26, which becomes 13, which becomes 40, which becomes 20, which becomes 10, which becomes 5, which becomes 16, which becomes 8, which becomes 4, which becomes 2, which becomes 1, and we are done. You are to write a class containing a method to iterate this function recur- sively: given an input n, then unless the input value is 1, your method should compute either n/2 or 3n + 1 as appropriate and call the method recursively with the new value as the input parameter. To be able to study the behavior of this function, you should also include in your Collatz class a static variable stepCount that counts how many times, for any given n, the function has to be called before it iterates down to 1. You should also have a static variable maxFound that keeps track of the maximum value computed in the process of iterating the function. Your main program should read a beginning and an ending value for n and run a loop from the beginning to the ending value. In each iteration you should print out the number of steps taken in iterating to the value 1. (You may assume that the function will always get to 1 eventually; if you happen to find a value for which it seems not to, you’ll be famous.) The value of maxFound should be printed out each time you discover a new maximum. Sample output can be found on the website. Please look at the small values carefully to make sure that you are counting iteration steps in the same way that the sample output is counting iteration steps and not off by one.

2

Commentary

The Collatz problem has been extensively studied by mathematicians and has been very extensively computed. This is largely because it’s a totally trivial function to describe, and all numbers so far tested eventually iterate down to 1, but no one has been able to prove that this function if called repeatedly will always iterate down to 1. Further, it’s not even well known how many steps are needed to iterate from any given n down to 1. You should also note that 1001063 is the smallest n for which the interme- diate numbers computed can no longer be represented with an int variable. If you use int variables and put this number in, then your program will go off into space recursively calling itself until it runs out of stack space. If you look at the details, what happens is that the overlarge would-be int value is seen as a negative number, and then once you go negative you can’t iterate to a + value, so you iterate forever. This is one of many problems in number theory that are easy to state and yet apparently very hard to prove. More than that, it represents a computational paradigm: we write the code not based on what we can prove (which is nothing) but based on what we “know” (namely, that all numbers iterate down to 1). If it were to happen that we found a number that didn’t work and proved that the alleged theorem was in fact wrong, that that would be sufficiently wonderful that we wouldn’t care that the program hadn’t worked perfectly. Since we expect the theorem to be true, we expect that the program will only verify that any n iterates down to 1, and that’s the way we code. More important than the specifics for this problem, there are often instances in which we cannot verify that things will all work out ok, but writing code to take care of pathologies with a very low probability of happening is just too painful, so we write for the average or expected case and we punt on the pathologies. A number of algorithms work well, for example, on sparse matrices (matrices with a very low density of nonzeros) but not very well on matrices that are not sparse. What is often the case is that we will start a computation with a sparse matrix that gradually fills in and becomes less sparse as the computation proceeds. Since the definition of “sparse” is often not precise, we may well write code that assumes sparsity and then deal with problems if the computation slows down too much in the late going.