I was first in [1] to notice the Spigot-algorithm, this algorithm was released quite early, see [2]. [1] Given a few amazing programs, you can calculate constants such as E,pi,log (2) with very little code. One of the 4 lines of code to calculate the PI program is known as the aliens written by the program, but I have not the courage to analyze and learn it, recently decided to learn this spigot-algorithm, first read the literature "3", understand its basic ideas, then plan to try to write various calculation constants of the code, and write a series of blogs. From this beginning, I will tell if you use this algorithm to calculate the sum of various constants or series.
The sequence A[N]={1/1,1/2,1/3,1/4 ... 1/n} is called the harmonic sequence. Harmonic series and f (n) = 1/1 + 1/2 + 1/3 +1/4 + ... 1/n is called the harmonic progression. Harmonic progression is one of the simplest series. It is also easiest to calculate this progression using the spigot algorithm.
The code is given below. Specific instructions to fill in later.
#define R 10//, can be changed to 100,1000,10000, #define FMT_STR "%d"//when r=100,1000,10000, corresponding, need to change to "%02d", "%03d", "%04d", #define N 10//calculates the first 10 items of the staggered series # define P 20//when R=10^k, the pre-p*k digits can be printed with a valid number 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;i--) {x+= (a[i]*r)//; a[i]= (a[i]*r)%i; } X+=a[i]*r; printf (FMT_STR,X/R); A[0]=X/R; if (j==0) printf (".");} }
A few notes:
1. The length of the array is related to the number of items computed, regardless of the final accuracy.
2. a[1] to A[n] stores the numerator of each of the J-round calculations.
3. Each round of calculation, get 2 bits 10 binary number, the highest bit direct output, the second high cache in a[0]
4. When n is greater than 10 o'clock, the result of the calculation is incorrect because the staggered series converges very slowly. The rush to output the highest bit is still a risky way to get a second high. The improved method is to output the highest bit when the 5th bit is obtained, and then store the 2nd to 5th bit in a[0], following the modified code to correctly calculate the value of the first 100 items of the staggered series.
#define R //binary, can be changed to 100,1000,10000, #define FMT_STR "%d"//when r=100,1000,10000, corresponding, need to change to "%02d", "%03d", "%04d", # Define N// calculation of the first n of the Staggered series # # # P //When r=10^k, can be printed before the P*K bit valid number int a[n+1],i,j,x;void main () {for (i=n;i; i--) a[i]=1; for (j=0;j<p+1;j++) { x=0; for (i=n;i;i--) { x+= (a[i]*r)//; a[i]= (a[i]*r)%i; } a[0]= a[0]*r + x; if (j>2) { printf (fmt_str,a[0]/(r*r*r*r)); a[0]%= (r*r*r*r); if (j==3) printf (".");}}
Reference documents:
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
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
One of the spigot algorithms calculates the sum of the harmonic series