Skip to main content
Logo image

πŸ§‘πŸ»β€πŸ’» Class Activities πŸ§‘πŸ»β€πŸ’» Class Activities

These activities will help you practice using loops to solve interesting problems. Each activity requires only loops, variables, and basic arithmeticβ€”no arrays needed.

1. The Collatz Conjecture.

(a)

The Collatz conjecture is one of mathematics’ most famous unsolved problems. Start with any positive integer n. If it is even, divide it by 2. If it is odd, multiply by 3 and add 1. Repeat this process until you reach 1.
For example, starting with 6:
\begin{gather*} 6 \to 3 \to 10 \to 5 \to 16 \to 8 \to 4 \to 2 \to 1 \end{gather*}
Write a MATLAB script that:
  1. Starts with a user-chosen positive integer n.
  2. Uses a while-loop to apply the Collatz rules repeatedly until reaching 1.
  3. Displays each number in the sequence.
  4. Reports how many steps it took to reach 1.
Hint.
Use mod(n, 2) == 0 to check if n is even. Keep a counter variable that increments each iteration.

(b)

Challenge: Test your script with different starting values. Can you find a starting number that takes more than 100 steps?

2. Digital Root Calculator.

(a)

The digital root of a number is found by repeatedly summing its digits until only a single digit remains. For example, the digital root of 9875 is found by:
\begin{gather*} 9 + 8 + 7 + 5 = 29\\ 2 + 9 = 11\\ 1 + 1 = 2 \end{gather*}
Write a MATLAB script that computes the digital root of any positive integer n using a while-loop. You can extract the rightmost digit of n using mod(n, 10), and remove it using floor(n/10).
Your script should:
  1. Accept a positive integer as input.
  2. Use an outer loop that repeats until n has only one digit.
  3. Use an inner loop to sum the digits of the current value of n.
  4. Display the digital root.
Hint.
Start with an outer loop: while n >= 10. Inside, use an inner loop to extract and sum digits, then update n to this sum.

(b)

Extension: Test your script with numbers like 38, 99, 123456, and 999999. What patterns do you notice?

3. Approximating Pi with Leibniz’s Formula.

(a)

The Leibniz formula for \(\pi\) states that:
\begin{equation*} \frac{\pi}{4} = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} - \cdots \end{equation*}
This infinite series converges slowly to \(\pi/4\text{,}\) but we can approximate it by adding many terms.
Write a MATLAB script that:
  1. Uses a for-loop to compute the sum of the first 1000 terms.
  2. Multiplies the result by 4 to approximate \(\pi\text{.}\)
  3. Compares your approximation to MATLAB’s built-in pi value.
  4. Reports the error (absolute difference).
Hint: The denominators are odd numbers (1, 3, 5, 7, ...) which follow the pattern \(2k - 1\) when looping with k = 1:1000. Signs alternate, so use (-1)^(k+1) to generate the alternating signs.

(b)

Extension: How many terms are needed to get within 0.001 of pi? Convert your for-loop to a while-loop that stops when the error is small enough.

4. Finding Fibonacci Numbers.

(a)

The Fibonacci sequence begins 0, 1, 1, 2, 3, 5, 8, 13, 21, ... where each number is the sum of the two preceding numbers.
Write a MATLAB script that:
  1. Uses a while-loop to generate Fibonacci numbers until one exceeds 10,000.
  2. Displays each Fibonacci number as it is computed.
  3. Reports which Fibonacci number was the first to exceed 10,000.
Hint: You only need three variables: the current Fibonacci number, the previous one, and the one before that. Use a pattern like:
prev2 = 0;
prev1 = 1;

while prev1 < 10000
    current = prev2 + prev1;
    % display and update variables
end

(b)

Extension: Modify your script to compute the ratio of consecutive Fibonacci numbers (e.g., \(F_n / F_{n-1}\)). What value does this ratio approach as \(n\) increases?

5. Perfect Number Detector.

(a)

A perfect number is a positive integer that equals the sum of its proper divisors (positive divisors excluding itself). For example, 6 is perfect because \(1 + 2 + 3 = 6\text{.}\)
Write a MATLAB script that:
  1. Prompts the user for a positive integer.
  2. Uses a for-loop to find all proper divisors (numbers that divide evenly into the input).
  3. Computes the sum of these divisors.
  4. Determines whether the number is perfect, abundant (sum exceeds the number), or deficient (sum is less than the number).
  5. Displays the classification and the sum of divisors.
Hint: Use mod(n, k) == 0 to test if k divides n evenly. Loop from 1 to n-1.

(b)

Extension: The first four perfect numbers are 6, 28, 496, and 8128. Write a script using nested loops to search for all perfect numbers less than 10,000.