More on Loops

The for loop is a very useful tool for doing things over and over again for a certain number of times already known in advance. There are two possibilities that we would like to consider:

  • What if we do not know in advance how many iterations we will need?
  • What if we would like to stop a loop before it is due to end?

An example for the first kind would be a Newton iteration that should run until the value of \(f(x)\) is "small" enough, for example \(10^{-12}\). Before actually performing the iterations we do not know how many steps it will take, so a for loop is not exactly the right type of loop. We could get around this limitation if we introduce a maximum number of allowed iterations and then use the (as-of-yet unknown) mechanism for terminating a loop prematurely once we find a good enough approximate root.

A while loop tells MATLAB® to continue iterating as long as a certain condition (which you specify) is satisfied. The syntax is:

while <condition>    <statements> end

MATLAB evaluates the <condition> and if it is true (or a non-zero number) it performs the <statements>, if not, it continues after the end. After each time it evaluates <statements> MATLAB goes back and evaluates <condition> again, etc. Note that <condition> does not get evaluated in the middle of evaluating <statements> but, rather, only before evaluating them. Here's a simple way of adding two positive integers (very silly):

x=5; y=6; while y>0     x=x+1;     y=y-1; end

Of course, this fails miserably if y is not a positive integer (doesn't do anything, do you understand why?)

Exercise 16. Solve the following problems using a while loop:

  • Show the numbers from 1 to 10
  • Show the numbers from 10 to -10
  • Find out how many divisors 28 has (mod or rem will be useful here)
  • Find out if a number is prime
  • Use an external while and an internal for loop to find the first 100 prime numbers.
  • A perfect number is a number \(n\) whose divisors (including 1 but excluding itself) add up to \(n\) itself. For example, 6 is a perfect number. Check if a number is perfect.
  • Use two nested while loops to find the first 3 perfect numbers.

Homework 5. Consider the following sequence defined completely by the first element \(S_1\):

\begin{equation} S_{n+1}= \begin{cases} S_n/2 & \text{ if } S_n \text{ is even}\\ 3 S_n+1 & \text{ if } S_n \text{ is odd} \end{cases} \end{equation}

A still||open question in mathematics is whether all such sequences always arrive at 1 for large enough \(n\) (the alternatives being that some sequences may rise indefinitely, or that there may be a closed orbit that does not include 1). Compute the number of iterations it takes to arrive at \(1\) given a starting value \(s\) using a while loop. Since we do not know how long it will take to arrive at 1 (though you can assume that it will happen eventually) we might want to construct this sequence using a while-loop. What starting number smaller than 10,000 has the longest trajectory? What's the largest number on that trajectory?

§This is the subject of the Collatz Conjecture.

||Despite a recent "near" solution.