One of the Spigot algorithms is used to calculate the sum of harmonic series and spigot series.

Source: Internet
Author: User
Tags mathematical functions

One of the Spigot algorithms is used to calculate the sum of harmonic series and spigot series.
Zookeeper

I first noticed Spigot-Algorithm in [1]. This Algorithm was released quite early. For details, see [2]. [1] Several amazing programs are provided. Only a small amount of code can be used to calculate constants such as e, pi, and log (2. The program with four lines of code to calculate the circumference rate was called by netizens as a program written by aliens, but I never had the courage to analyze and learn it. Recently I finally decided to learn this Spigot-Algorithm, I first read the document [3], understood its basic idea, and planned to write various code for calculating constants and write a series of blogs. Starting from this article, I will describe how to use this algorithm to calculate the sum of constants or series.

Series a [n] = {1/1, 1/2, 1/3, 1/4... 1/n} is called a harmonic sequence. The sum of the harmonic series f (n) = 1/1 + 1/2 + 1/3 + 1/4 +... 1/n is called the harmonic series. Harmonic Series is one of the simplest series. Using the Spigot algorithm to calculate this level is also the easiest.

The following code is provided. Make up the details later.

# Define R 10 // hexadecimal, which can be changed to 10000, 10000, # define FMT_STR "% d" // when R is, corresponding, change to "% 02d", "% 03d", "% 04d ", # define N 10 // calculate the first 10 items of the staggered Series # define P 20 // when R = 10 ^ k, print the valid P * k digit int a [N + 1], I, j, x; void main () {for (I = N; I; a [I --] = 1); for (j = 0; j <P; j ++) {x = 0; for (I = N; I --) {x + = (a [I] * R)/I; a [I] = (a [I] * R) % I ;} x + = a [I] * R; printf (FMT_STR, x/R); a [0] = x/R; if (j = 0) printf (". ");}}


Notes:
1. The length of the array is related to the number of calculated items, but not the final precision.
2. a [1] to a [n] indicates the molecules in the j-round computing.
3. In each round of calculation, two digits are obtained in hexadecimal notation, and the highest bit is output directly. The second high bit is cached in a [0].
4. When N is greater than 10, the calculation result is incorrect because the convergence of the staggered series is slow. It is still risky to rush to output the highest bit when the next high point is obtained. The improved method is to output the highest bit when 5th bits are obtained, and store the 2nd-5 bits in a [0]. The modified code is as follows, correctly calculates the value of the first 100 items of the staggered series.


# Define R 10 // hexadecimal, which can be changed to 10000, 10000, # define FMT_STR "% d" // when R is, corresponding, change to "% 02d", "% 03d", "% 04d ", # define N 100 // calculate the first N items of the staggered Series # define P 100 // when R = 10 ^ k, print the valid P * k digit int a [N + 1], I, j, x; void main () {for (I = N; I --) a [I] = 1; for (j = 0; j <P + 1; j ++) {x = 0; for (I = N; I --) {x + = (a [I] * R)/I; a [I] = (a [I] * R) % I ;} a [0] = a [0] * R + x; if (j> 2) {printf (FMT_STR, a [0]/(R * R); a [0] % = (R * R); if (j = 3) printf (". ");}}}


References:

1. Tiny programs for constants, http://numbers.computation.free.fr/Constants/constants.html

1. M. Abramowitz and I. Stegun,Handbook of Mathematical Functions, Dover, New York, (1964)

3. https://en.wikipedia.org/wiki/Spigot_algorithm


Zookeeper

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.