a2Collatz
Click here to load reader
-
Upload
razanmk961214 -
Category
Documents
-
view
215 -
download
1
description
Transcript of a2Collatz
COMP 208: Computers in Engineering
Fall, 2015
Assignment 2: The Collatz Problem
Due Date
Assignment 2 is due on Thursday, October 8th, at 11:59. The cutoff is
automated and is exactly at this time. Assignments submitted within the next hour will be considered late. After that time they will not be
accepted at all.
These assignments are to be done individually. You can collaborate on understanding the problem but you must write the solution
individually. Your submission might be subject to plagiarism detection software.
Introduction
Given any positive integer, n, we can generate a sequence of integers
as follows. If n is odd and not equal to 1, the next value in the
sequence is 3n+1. If n is even, the next value in the sequence is n/2. If n is 1, the sequence terminates.
There is a conjecture that for any value of n, the sequence eventually
terminates with the value 1. Proving this conjecture is known as the Collatz problem. The problem has a long history and is also known as
the Syracuse problem, Kakutani’s problem, Hasse’s algorithm and Ulam’s problem or just the 3x+1 problem.
To date the conjecture has not been proven and remains an open
problem. See http://www.cecm.sfu.ca/organics/papers/lagarias/ for an extensive discussion of this problem.
Assignment
You are to write and submit two programs to study the Collatz problem
Part 1 (40 marks)
Write a C program that reads a number from the keyboard and
then outputs the sequence of numbers generated as described above. If the Collatz conjecture is false, it is possible that the
sequence does not terminate. Of course this is highly unlikely since no counterexample has been found to date. However, just
to be on the safe side, your program should terminate in case the length of the sequence is very large, say more than 10000.
For example, if the input is 3, the output should be the following
sequence of values: 3, 10, 5, 16, 8, 4, 2, 1
If the input is 7, the output should be: 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1
Part 2 (60 marks)
We would like to investigate some of the properties of these
sequences. One question that comes to mind is “how long can these sequences get?” In this part of the assignment you are to
input a value for the range of numbers to be considered. Then find a number between 1 and the range that has the longest
sequence. Your program should report the initial number that
generates this long sequence. (Of course if the sequence is infinite that would show that the conjecture is false. Use the
method of part 1 to make sure the sequence can’t go on forever).
For example, if the limit is 100000, the number with the longest
sequence is 77031 (and the length of the sequence is 350)
Requirements
The programs must be written in C
Use meaningful variable names Comment and indent your code. It is your responsibility to
make it readable to the grader Submit only the two source files (.c files) and name your files
A2a_123456789 and A2b_123456789 where 123456789 is
replaced by your student ID number.