Here's a lesson that uses the for-loop to study a series (a series is a bunch of numbers added together). In pre-calculus or calculus, you might have
learned that $\Sigma\frac{1}{n}$ diverges (or gives an infinite value if you run the sum over an infinite
number of terms). If you take the first few terms in the sum, you'll get $1+\frac{1}{2}+\frac{1}{3}+\frac{1}{4}+...$. Each term
gets smaller and smaller, so does it really diverge? Let's write some code here to test it.
As you work on this, notice the similarities between the $\Sigma$ from math, and the for-loop in programming.
In this case $\Sigma_{n=a}^{b}$ runs $n$ from $a$ to $b$, while for n=a,b do runs n from a to b.
Now you try. Fix the for-loop and sum=sum+ line to see if $\Sigma\frac{1}{n}$ diverges.
Type your code here:
See your results here:
Fix the for-loop to run n over as many terms as you'd like. Start with 50 or 100. Then fix the sum=sum+ line
to add $1/n$ to the sum, with each iteraction of the for-loop.
When done, see if:
$\Sigma\frac{1}{n^2}$ converges or diverges.
How about $\Sigma\frac{(-1)^{n-1}}{n}$?
Share your code
Show a friend, family member, or teacher what you've done!