HARMONIC
This page runs a BASIC program that adds up the first n terms of the harmonic series,
\[ 1 + \frac{1}{2} + \frac{1}{3} + \cdots + \frac{1}{n}. \]
The program is rewritten in Python and runs right here in your browser. Edit the code and press Run to run it again; Reset brings back the original program.
The loop adds 1/i to sum for i = 1 to n and then prints n and the total. For n = 100 the sum is about 5.1874. The terms get smaller and smaller, yet the harmonic series diverges: the partial sums grow without bound. They just grow very slowly, roughly like \(\ln n + 0.5772\). Try n = 1000, 10,000, and 1,000,000 and watch the sum creep up by about 2.3 each time n is multiplied by 10. (Ten million or more takes long enough that the page may stop the program; it allows five seconds.)
Unlike most of the other programs, this one doesn’t start with DEFDBL A-Z, so QBasic would have done the arithmetic in single precision, with about 7 significant digits. Python always uses double precision, about 16 digits, so its last few digits will differ from what QBasic printed.
Notes from the textbook
Add notes here.
1 n = 100
2 sum = 0
3 FOR i = 1 TO n
4 sum = sum + 1/i
5 NEXT i
6 PRINT n, sum
Output
Everything the program prints appears here, one line for each print(...),
like BASIC's PRINT. Numbers printed on the same line are lined up in columns.
Python code
Press Run (or Ctrl/⌘ + Enter) to run the program again. Press Esc, then Tab, to leave the editor.