Discussion: This Should Ring a Bell
Execute the code to understand the output and gain insights into series summation.
We'll cover the following...
Run the code
Now, it's time to execute the code and observe the output.
Press + to interact
#include <stdio.h>int main(){const int limit = 1000;int x;float t;/* divergent */t = 0.0;for(x=1; x<=limit; x++ ) t += 1.0 / (float)x;printf("Divergent: %.4f\n", t);/* convergent */t = 0.0;for(x=1; x<=limit; x*=2 ) t += 1.0 / (float)x;printf("Convergent: %.4f\n", t);return(0);}
Understanding the output
The code calculates the sum of two types of harmonic series, divergent and convergent:
Divergent: 7.4855Convergent: 1.9980
Code output
While these values may not seem frightening, the math behind them is considered terrifying to some.
Harmonic series
Like the Fibonacci sequence, a harmonic sequence consists of the sum of values. But for a harmonic series, these are a series of fractions that follow a pattern. When all the fractions are added together, they either diverge and increase in value, or they converge by getting closer to a specific value.
The first loop in the given code represents a divergent harmonic series. It’s the sum of values