1. For-loop Exercises.
(a)
Write a function,
sumSquares that computes
\begin{equation*}
1^2 + 2^2 + \cdots + n^2
\end{equation*}
for a given positive integer \(n\text{.}\)
| Inputs: | n (1x1) double — positive integer
|
| Outputs: |
Test cases:
% Test 1: Small value
n = 3;
% Run your script
% Expected: S = 14 (1^2 + 2^2 + 3^2 = 1 + 4 + 9)
% Test 2: Larger value
n = 5;
% Run your script
% Expected: S = 55 (1 + 4 + 9 + 16 + 25)
% Test 3: Edge case
n = 1;
% Run your script
% Expected: S = 1
% Test 4: Medium value
n = 10;
% Run your script
% Expected: S = 385
(b) Factorial Computation.
Write a script that computes the factorial of a number:
\begin{equation*}
n! = 1 \cdot 2 \cdot \cdots \cdot n\text{.}
\end{equation*}
Use a for loop. Assume \(n\) is a nonnegative integer. Store the result in
F.Test cases:
% Test 1: Small factorial
n = 5;
% Run your script
% Expected: F = 120 (5! = 1*2*3*4*5)
% Test 2: Edge case
n = 0;
% Run your script
% Expected: F = 1 (0! = 1 by definition)
% Test 3: Another edge case
n = 1;
% Run your script
% Expected: F = 1
% Test 4: Larger value
n = 7;
% Run your script
% Expected: F = 5040
(c) Rewrite repeated output: counting by fives.
Rewrite the repeated code below as a
for loop:
fprintf('5\n');
fprintf('10\n');
fprintf('15\n');
fprintf('20\n');
fprintf('25\n');
Your loop should produce the same output in the same order.
(d) Rewrite repeated output: countdown by twos.
Rewrite the repeated code below as a
for loop:
fprintf('12\n');
fprintf('10\n');
fprintf('8\n');
fprintf('6\n');
fprintf('4\n');
fprintf('2\n');
Your loop should produce the same output in the same order.
(e) Rewrite repeated updates: total of 3 through 7.
Rewrite only the repeated update lines below as a
for loop:
total = 0;
total = total + 3;
total = total + 4;
total = total + 5;
total = total + 6;
total = total + 7;
fprintf('total = %i\n', total);
Keep the initialization and final
fprintf line.
(f) Exponential Series Partial Sum.
Let \(x\) be a real number. Use a
for loop to compute the partial sum
\begin{equation*}
\sum_{k=0}^{N} \frac{x^k}{k!} = 1 + \frac{x}{1!} + \frac{x^2}{2!} + \cdots + \frac{x^N}{N!}
\end{equation*}
for a chosen integer \(N\text{.}\) Store the result in
P.| Inputs: | x (1x1) double — real number
|
N (1x1) double — number of terms in the series (positive integer)
|
|
| Outputs: | P (1x1) double — the partial sum
|
Test cases:
% Test 1: Simple case
x = 1;
N = 5;
% Run your script
% Expected: P ≈ 2.7167 (approximates e^1)
% Test 2: Zero input
x = 0;
N = 10;
% Run your script
% Expected: P = 1 (e^0 = 1)
% Test 3: Negative value
x = -1;
N = 10;
% Run your script
% Expected: P ≈ 0.3679 (approximates e^-1)
% Test 4: Larger N for better approximation
x = 2;
N = 20;
% Run your script
% Expected: P ≈ 7.3891 (approximates e^2)
(g) Die Rolling Simulation.
Simulate rolling a fair six-sided die \(N\) times. Use a
for loop to count how many times you roll a 6. Store the count in sixes.
| Inputs: | N (1x1) double — number of die rolls (positive integer)
|
| Outputs: | sixes (1x1) double — count of how many times a 6 was rolled
|
Test cases:
% Test 1: Small number of rolls
rng(42); % Set seed for reproducibility
N = 10;
% Run your script
% Expected: sixes will vary (typically 1-2)
% Test 2: Larger simulation
rng(123);
N = 100;
% Run your script
% Expected: sixes will vary (typically around 16-17)
% Test 3: Many rolls
rng(999);
N = 600;
% Run your script
% Expected: sixes will vary (typically around 100, which is 1/6 of 600)
(h) Powers of Two Display.
Write a script that uses a
for loop to compute and display the first 10 powers of 2 (i.e., \(2^1, 2^2, \ldots, 2^{10}\)). For each iteration, use fprintf to display the exponent and result in the format: 2^n = result.
Test cases:
% Test: Run your script
% Expected output:
% 2^1 = 2
% 2^2 = 4
% 2^3 = 8
% 2^4 = 16
% 2^5 = 32
% 2^6 = 64
% 2^7 = 128
% 2^8 = 256
% 2^9 = 512
% 2^10 = 1024
(i) Running Products.
Write a script that uses a
for loop to compute the running products \(1,\, 1 \cdot 2,\, 1 \cdot 2 \cdot 3,\, \ldots,\, 1 \cdot 2 \cdot \cdots \cdot N\text{.}\) Initialize a variable current_product to 1 before the loop, and create a vector running_product of length \(N\text{.}\) For each iteration \(k = 1, 2, \ldots, N\text{,}\) update current_product by multiplying it by \(k\text{,}\) then store the result in \(\texttt{running\_product(k)}\text{.}\) After the loop, display the contents of running_product.
| Inputs: | N (1x1) double — positive integer for number of terms
|
| Outputs: | running_product (1xN) double — vector of running products
|
Test cases:
% Test 1: Small value
N = 5;
% Run your script
% Expected: running_product = [1, 2, 6, 24, 120]
% Test 2: Edge case
N = 1;
% Run your script
% Expected: running_product = 1
% Test 3: Medium value
N = 7;
% Run your script
% Expected: running_product = [1, 2, 6, 24, 120, 720, 5040]
(j) Primality Test.
Write a script that tests whether a given positive integer
number is prime. A number greater than 1 is prime if it has no divisors other than 1 and itself. By convention, 1 is not prime, so your script should set is_prime to \(false\) when number <= 1 and only run the divisor-testing loop when number > 1. Use a for loop to test all potential divisors from 2 to sqrt(number). Store the result in a logical variable is_prime (true if prime, false otherwise). Use mod(number, divisor) == 0 to check divisibility.
| Inputs: | number (1x1) double — positive integer to test
|
| Outputs: | is_prime (1x1) logical — true if number is prime, false otherwise
|
Test cases:
% Test 1: Prime number
number = 17;
% Run your script
% Expected: is_prime = true
% Test 2: Composite number
number = 15;
% Run your script
% Expected: is_prime = false
% Test 3: Edge case - 1 is not prime
number = 1;
% Run your script
% Expected: is_prime = false
% Test 4: Edge case - 2 is prime
number = 2;
% Run your script
% Expected: is_prime = true
% Test 5: Large prime
number = 97;
% Run your script
% Expected: is_prime = true
(k) Leibniz Pi Approximation.
Write a script that approximates \(\pi\) using the Leibniz formula:
\begin{equation*}
\frac{\pi}{4} = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} - \cdots
\end{equation*}
Use a
for loop to compute the sum of the first 1000 terms, then multiply by 4 to approximate \(\pi\text{.}\) Store the approximation in pi_approx. The denominators follow the pattern \(2k - 1\) and signs alternate (use (-1)^(k+1) for the sign).Test cases:
% Test: Run your script with 1000 terms
% Expected: pi_approx ≈ 3.1406 (exact value depends on 1000 terms)
% Compare with MATLAB's built-in: pi
% Note: This is a slow-converging series, so it won't be very accurate
(l) Perfect Number Test.
Write a script that determines whether a given positive integer
n is a perfect number. A perfect number equals the sum of its proper divisors (divisors excluding itself). Use a for loop to find all divisors from 1 to n-1, sum them, and compare to n. Store the result in a logical variable is_perfect. For example, 6 is perfect because \(1 + 2 + 3 = 6\text{.}\)
| Inputs: | n (1x1) double — positive integer to test
|
| Outputs: | is_perfect (1x1) logical — true if n is perfect, false otherwise
|
Test cases:
% Test 1: First perfect number
n = 6;
% Run your script
% Expected: is_perfect = true (1 + 2 + 3 = 6)
% Test 2: Second perfect number
n = 28;
% Run your script
% Expected: is_perfect = true (1 + 2 + 4 + 7 + 14 = 28)
% Test 3: Not perfect
n = 10;
% Run your script
% Expected: is_perfect = false (1 + 2 + 5 = 8 ≠ 10)
% Test 4: Small non-perfect
n = 12;
% Run your script
% Expected: is_perfect = false
(m) Sum of Odd Numbers.
Write a script that uses a
for loop to compute the sum of all odd numbers from 1 to N (where N is given). Use the loop to iterate through the range 1:2:N, which generates only odd numbers. Store the result in sum_odd.
| Inputs: | N (1x1) double — positive integer upper limit
|
| Outputs: | sum_odd (1x1) double — sum of all odd numbers from 1 to N
|
Test cases:
% Test 1: Small range
N = 9;
% Run your script
% Expected: sum_odd = 25 (1 + 3 + 5 + 7 + 9)
% Test 2: Even upper limit
N = 10;
% Run your script
% Expected: sum_odd = 25 (1 + 3 + 5 + 7 + 9, stops before 10)
% Test 3: Larger range
N = 20;
% Run your script
% Expected: sum_odd = 100 (1 + 3 + 5 + ... + 19)
% Test 4: Edge case
N = 1;
% Run your script
% Expected: sum_odd = 1
(n) Random Search with Break.
Write a script that searches for a target value in a sequence of random numbers. Use a
for loop that runs up to 50 iterations, generating one random integer between -20 and 20 each time using randi([-20 20]). When the target value is found, use break to exit the loop immediately. Store whether the target was found in found (logical) and the iteration number where it was found in location (or -1 if not found).
| Inputs: | target (1x1) double — integer value to search for
|
| Outputs: | found (1x1) logical — true if target was found, false otherwise
|
location (1x1) double — iteration where found (or -1 if not found)
|
Test cases:
% Test 1: Search for 0
rng(42); % Set seed for reproducibility
target = 0;
% Run your script
% Expected: found = true or false (depends on random sequence)
% location = iteration number or -1
% Test 2: Search for a specific value
rng(123);
target = 10;
% Run your script
% Results will vary based on random generation
% Note: Results depend on random number generation
% Set rng seed for reproducible results when testing
