Skip to content
Advertisement

Find the sum of an infinite series

How can I find the sum of an infinite series?

enter image description here

Series:

My misbehaving example:

seq 0 inf | awk '{sum+=(1/$1)} END {print sum}'

Is it possible to carry out this method correct calculation or how to resolve this problem in other ways?

Advertisement

Answer

This series does not converge. It’s called the harmonic series and it’s known to do not converge.

Moreover, as a comment says, you need to start at 1 instead of 0 because otherwise it’s not defined (division by 0).

Try this:

seq 1 inf | awk '{sum+=(1/$1)} {print sum}'

However, this doesn’t work well for higher iterations, because number format representations are not compatible and the precision of the internally used number representation is not sufficient.

To make more precise calculations for estimating the value of a convergent series, you should use a scientific calculation package of a better suited programming language like Java, Python or C++.

Here is a complete working example in Java:

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

public class SeriesCalculator {

    public static void main(String[] args) {
        MathContext mathContext = new MathContext(100, RoundingMode.HALF_EVEN);

        BigDecimal i = BigDecimal.ONE;
        BigDecimal sum = BigDecimal.ZERO;
        while (true) {
            // calculation for every iteration
            BigDecimal element = BigDecimal.ONE.divide(i, mathContext);
            sum = sum.add(element);

            // show output once for every magnitude
            String iStr = i.toString();
            if (iStr.matches("10*")) {
                int log10_i = iStr.length() - 1;
                System.out.println("i=10^" + log10_i + "; sum = " + sum);
            }

            // preparation of next iteration step
            i = i.add(BigDecimal.ONE);
        }
    }
}

Output:

i=10^0; sum = 1
i=10^1; sum = 2.9289682539682539682539682539682539682539682539682539682539682539682539682539682539682539682539682540
i=10^2; sum = 5.18737751763962026080511767565825315790897212670845165317653395658721955753255049660568776892312041358
i=10^3; sum = 7.485470860550344912656518204333900176521679169708803665773626749957699349165202440959934437411845081421
i=10^4; sum = 9.7876060360443822641784779048516053348592629455776917183894609566816020249431595068001251272900808826142
i=10^5; sum = 12.09014612986342794736321936350421950079369894178220110162752941593818198228230919443164900701935230601448
i=10^6; sum = 14.392726722865723631381127493188587676644800013744311653418433045812958507517995003568298175947219100731214
i=10^7; sum = 16.6953113658598518153991189395404518842498697523730804627851359543562886921742546877116037143701502883133367
i=10^8; sum = 18.99789641385389832441711039422398284185097124497010343881842218865761130260918292544757982666365581248865345
...
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement