

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 2
This page cannot be seen from the preview
Don't miss anything!


1
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
2
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.