
% Homework 2 Solutions
% updown.m
% CS 1109, Summer 2011
% July 13

% This script computes the number of steps in a given number's ``up-down'',
% i.e., Hailstone sequence. We assume that the user enters a positive
% integer. 

clc
clear

number = input('Please enter a positive integer: ');

% Make a copy of the input number so we can print it to the screen at the
% end
originalInput = number;

numSteps = 0;

% Until we hit 1, repeat the following step: if number is even, halve it.
% If it is odd, replace it with 3*number + 1. Keep track of how many times
% we have to perform this operation in numSteps.
while (number > 1)
    if (rem(number, 2) == 0)
        number = number / 2;
    else
        number = 3*number + 1;
    end
    
    numSteps = numSteps + 1;
end

fprintf('The sequence for %d consists of %d steps.\n', originalInput, ...
    numSteps);

% Common mistakes / other observations:
%
% 1) Most people successfully solved this problem. Those who did lose 
% points did so for failing to adhere to the code standards
% -- for example, missing comments, not indenting code properly etc.
% Remember to go through the checklist in the problem handout before you
% submit solutions!
