Skip to main content

Section 4.6 Examples and questions

These are additional examples for reviewing the topic we have covered. When reading each example, try to find your own solution before clicking “Answer”. There are also questions for reviewing the concepts of this section.

The harmonic series \(1 + \frac{1}{2} + \frac{1}{3} + \frac{1}{4} + \cdots\) diverges, meaning that its partial sums grow without a bound. So, if we add enough terms of this series, we will get a number greater than 10. Use a while loop to determine how many terms are required to get a partial sum that is greater than 10.

Answer
s = 0;
n = 1;
while s <= 10
    s = s + 1/n;
    n = n + 1;
end
fprintf('The partial sum has %d terms\n', n-1)

Explanation: The variable s represents partial sums. We add 1/n to it, and then increase the value of n. When s > 10 the process stops. The output uses formatting code %d for integers, since the answer is an integer. Why does the output have n-1 and not n? Because of the way that the loop is written, the value of n is increased after each addition. So at the end of it, n is the number that is 1 more than the number of terms we added.

The Collatz conjecture, also known as the Syracuse problem, is a famous unsolved problem in mathematics. Its subject is the following process.

  1. Start with a positive integer k.
  2. If k is even, divide it by 2. Otherwise multiply it by 3 and add 1.
  3. Repeat Step 2.

The conjecture is that this process always reaches the number 1 (after 1 it enters infinite loop 1, 4, 2, 1, 4, 2, ...) Write a script that verify this conjecture for initial values of k from 1 to 500, and for each of them record the number of steps that it takes to reach 1. Then plot the number of steps. Note: the condition that k is even can be tested as if mod(k, 2) == 0 where mod(k, 2) computes the remained of dividing k by 2.

Answer
N = 500;
steps = zeros(1, N);
for n = 1:N
    count = 0;
    k = n;
    while k > 1
        if mod(k, 2) == 0
            k = k/2;
        else 
            k = 3*k+1;
        end
        count = count + 1;
    end 
    steps(n) = count;
end
plot(1:N, steps, '.')

Explanation. The array steps is created as a bunch of zeros, to be replaced with actual counts of steps. The initial value of Collatz process is called n and it runs through the numbers from 1 to 500. So the variable k is initially assigned the value n and then modified according to the rules stated above. Every time it is modified, the count of steps is increased by 1. The plot at the end shows each data point as an individual dot, not connecting them by lines.

This example demonstrates the use of both kinds of loops. Note that the variable involved in while loop condition, while k > 1, should be modified within the loop, otherwise it will never end. In contrast, the variable involved in for loop condition for n = 1:N should not be modified since it is automatically incremented with each run of the loop.

If in Example 4.1.2 we did not introduce the variable g and simply wrote y = sqrt(x*y), would the result still be correct? Why or why not?

What exactly would go wrong in Example 4.6.2 if we did not introduce k and simply wrote the same while loop with n, as below?

while n > 1
    if mod(n, 2) == 0
        n = n/2;
    else 
        n = 3*n+1;
    end
    count = count + 1;
end