Program for Analyzing alien PI computing

Source: Internet
Author: User

There is a program that uses only four lines of code to calculate pi, known as the alien PI computing program.

Many people discuss and analyze the implementation principle of the program, such as: http://blog.csdn.net/panqiaomu/archive/2006/05/07/711776.aspx

But I always felt that the analysis was not thorough enough, so I analyzed it myself.

1. modify the original program to a more understandable form;

2. Use the same algorithm to calculate pi in an Excel file.

Download (note the modified extension)
 

/*
File: pi800.c
Name: Program for Analyzing alien PI computing
Author: zyl910
Blog: http://blog.csdn.net/zyl910/
Version: V1.0
Updata: 2006-11-5
Program for Analyzing alien PI computing
~~~~~~~~~~~~~~~~~~~~~~
Original program:
Int A = 10000, B, c = 2800, d, e, f [2801], G;
Main (){
For (; B-c ;)
F [B ++] = A/5;
For (; D = 0, G = C * 2; C-= 14, printf ("%. 4D", e + D/A), E = D %)
For (B = C; D + = f [B] * A, F [B] = D % -- g, D/= g --, -- B; D * = B );
}
Pi formula:
1 2 3 K
Pi = 2 + --- * (2 + --- * (2 + --- * (2 +... (2 + ---- * (2 + ...))...)))
3 5 7 2 k + 1
*/
Int main (void)
{
Long A = 10000; // Scaling Factor
Long c = 2800; // number of iterations
Long f [2801]; // intermediate Calculation Result
Long D; // cumulative inner cycle error
Long E = 0; // cumulative error of external circulation
Long B, G; // molecules and denominator. K/(2 k + 1)
Int I, J;
For (I = 0; I <C; I ++) // If you fully follow the formula, the cycle condition should be "for (I = 1; I <= C; I ++) ". Fortunately, F [2800] has a very small contribution, and getting any value will not have a big impact on accuracy.
F [I] = 2 * A/10; // 2 is the coefficient 2 in the formula. A/10 indicates that a decimal bit is retained, because the output starts from 3.
While (C> 0)
{
D = 0;
G = (C * 2 + 1)-2; // denominator. Because 4 bits are output in each loop, we multiply the number of bits in the subsequent operation by a, so here we have to-2
B = C; // molecule
While (B> 0)
{
/* Multiply by the numerator according to the formula */
D * = B;
D + = f [B] * A; // because each External Loop outputs four digits
/* Divide by the denominator according to the formula */
F [B] = D % G; // part of the numerator with a score
D/= g; // an integer with a score
/* Next */
G-= 2;
B --;
}
Printf ("%. 4D", e + D/);
E = D %;
C-= 14; // because the precision is fixed to 800 bits, after every 4 bits are output, it is equivalent to reducing the precision requirement by 4 bits, so 14 items can be counted at a time
}
Printf ("/N ");
Return 0;
}

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.