Creating digits of pi
|
02-15-2018, 11:57 PM
(This post was last modified: 02-16-2018 03:37 AM by Gerson W. Barbosa.)
Post: #35
|
|||
|
|||
RE: Creating digits of pi
(02-14-2018 11:57 PM)Gerson W. Barbosa Wrote: By using this method, a thousand digits can be computed in less than four hours (3h51m09s) on a 49G. This version is slightly faster, 3h44m25s on the 49G (1h58m17s on the 50g, 2 minutes on the emulator). Code:
Formula: \(\frac{\pi }{4}\approx 1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\cdots +\frac{1}{2n-3}-\frac{1}{2n-1}+\frac{1}{4n+\frac{1^{2}}{n+\frac{2^{2}}{4n+\frac{3^{2}}{n+\frac{4 ^{2}}{...+\frac{...}{...+\frac{n^{2}}{4n}}}}}}}\) This gives \(\frac{3n}{2}\) correct digits (for even n). Algorithm: d = 1000 ; desired number of digits n = int(d/3) + 1 ; required number of iterations a = 2*n ; number of terms in the series and in the continued fractions on (n, in the above formula) b = 8*n ; part of the denominators in the cont'ed fraction, alternating with the previous constant (4n and n, in the formula) c = 4*n*n ; rightmost squared integer in the continued fraction (n² in the formula) d = 4*n - 1 ; rightmost denominator in the series (n-1 in the formula) s1 = 0 ; initialization of the first partial sum (terms of series) s2 = 0 ; initialization of the second partial sum (terms of the continued fraction) for i = 1 to n ; start loop s1 = s1 - 1/d + 1/(d - 2) ; both the terms of the series and the terms of the continued fraction are s2 = (c - d)/(a + c/(b + s2)) ; processed two at a time, starting from the respective rightmost terms c = c - 2*(d - 1) ; next squared integers in the continued fraction are computed by simple subtraction, d = d - 4 ; like the the next denominators in the series next i ; end loop s2 = 1/(b + s2) ; add leftmost term of the continued fraction to the respective partial sum pi = 4*(s1 + s2) ; add both partial sums together and multiply the result by 4 to get pi display pi ; display pi to d decimal places This gives \(3\) digits per iteration. Edited to fix a couple of typos Edited again to fix another typo per Mike’s message below |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)