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.
