Computation using the Machin Formula

Source: Internet
Author: User
The Machin formula is used for computation, and the number of digits in the last digit is controlled in a ten-billion hexadecimal notation. Tens of thousands of digits can be calculated here (80 times faster than the simplest Pi). About 40 lines of source code are displayed on this webpage. Calculation Formula Pi = 16 arctg (1/5)-4 arctg (1/239), where arctg (X) = x-x ^ 3/3 + x ^ 5/5-x ^ 7/7 + x ^ 9/9... make x = x ^ 2 and extract the formula: arctg (x) = x (1/3-X (1/5-X (1/7-X (..., Just iterate B = 1/(2n + 1)-B * X, n = N ,..., C ++ or assembly language should be used to calculate hundreds of thousands or millions of digits in B x, that is, arctg (X, to obtain tens of millions of digits, use the Ramanujan formula. To calculate hundreds of millions or billions of digits, use the geometric arithmetic mean algorithm // pi to calculate javascript programs, machin + 10 billion hexadecimal optimization // 2006.12 Xu Jianwei Putian 10 medium function add (A, B, n) {// addition algorithm of multi-precision A to multi-precision B (primary addition) for (VAR I = n-1, f = 0; I> = 0; I --) {A [I] + = B [I] + F; if (A [I] >= 10000000000) A [I]-= 10000000000, F = 1; else f = 0 ;}} function sub0 (a, B, R, n) {// subtraction algorithm (primary subtraction) for (VAR I = n-1, f = 0; I> = 0; I --) {R [I] = A [I]-B [I]-F; If (R [I] <0) R [I] + = 10000000000, F = 1; else f = 0 ;}} function Div (A, B, n) {// multi-precision A and single-precision B division algorithm (Primary Division) for (VAR I = 0, f = 0, C; I <n; I ++) {c = A [I] + F * 10000000000; A [I] = math. floor (C + 0.1)/B); F = C % B;} function Dao (A, F, B, n) {// reciprocal (F/B) A [0] = math. floor (F/B); F = F % B; For (VAR I = 1, C; I <n; I ++) {c = f * 10000000000; A [I] = math. floor (C + 0.1)/B); F = C % B;} function set (A, V, n) {for (VAR I = 0; I <N; I ++) A [I] = 0; A [0] = V;. length = N;} // set 0 to the array and give the initial value at the first position v // calculate the circumference rate below. formula: Machin Pi = 16 arctg (1/5)-4 arctg (1/239) vaR A = new array (), B = new array (), c = new array (); // three working Arrays: a PI, B arctg, C is a temporary Array Function arctg (K, V, ZF, n) {// evaluate v * arctg (K ), ZF indicates the plus and minus sign (VAR I = math. round (N * 23.1/math. log (K * K), n = I, N2; I> = 0; I --) {n2 = math. round (n-I) * n/n) + 1; // The number of digits in the last calculation controls if (N2> N) N2 = N; Dao (C, V, 2 * I + 1, N2); Div (B, K * k, N2); sub0 (C, B, B, N2);} Div (B, K, n); If (ZF> 0) add (A, B, n); else sub0 (A, B, A, n);} function Pi (N) {// n is the number of digits calculated. The last five digits obtained by this program may be set (A, 0, n); Set (B, 0, N ); // PI result array and arctg array. The initial values are 0 arctg (5, 16, 1, n), arctg (239,4,-1, n), and for (VAR I = 1; I <n; I ++) A [I] = string (10000000000 + A [I]). substr (); // complement 10 return. join ("");} function JS () {ca. innerhtml = "the last five digits may be incorrect: Pi =" + Pi (NW. value-0 + 1);} // output on the webpage

Link: http://www.fjptsz.com/xxjs/xjw/rj/112/pi23.htm

Computation using the Machin Formula

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.