|
Post by bencooper on Nov 3, 2023 4:16:46 GMT -5
I have here translated a PASCAL version of Rabinowitz Spigot Formula for calculating Pi to any number of digits. It is quite fast but unfortunately is accurate to only 6 digits. (The code is based on a string of 2's each with a different radix which is converted to decimal from the "back-end") I'm not sure if the code is translated correctly (can't be far wrong ?) or if I'm struggling with rounding-off errors (32-bit integers required). The attached has the LB code and the original PASCAL code Look forward to advice, please ! Attachments:PiRabin.bas (2.84 KB)
|
|
|
Post by bencooper on Nov 3, 2023 5:45:26 GMT -5
'Program Pi_Spigot PASCAL 'const n =1000; 'len =10*n div 3; 'var i, j, k, q, x, nines, predigit : integer; 'a : array[1..len] of longint; 'begin 'for j := 1 to len do a[j] := 2; {Start with 2s} 'nines := 0; predigit := 0 {First predigit is a 0} 'for j := 1 to n do 'begin q := 0; 'for i := len downto 1 do {Work backwards} 'begin 'x := 10*a+q*i; 'a := x mod (2*i-1); 'q := x div (2*i-1); 'end; 'a[1] := q mod 10; q := q div 10; 'if q = 9 then nines := nines + 1 'else if q=10 then 'begin write(predigit+1); 'for k := 1 to nines do write(O); {zeros} 'predigit := O; nines := O 'end 'else begin 'write(predigit), predigit := q; 'if nines <> O then 'begin 'for k := 1 to nines do write(9); 'nines := 0 'end 'end 'end, 'writeln(predigit); 'end.
|
|
|
Post by Rod on Nov 3, 2023 6:34:26 GMT -5
|
|
|
Post by tsh73 on Nov 3, 2023 6:59:44 GMT -5
Found better formatted Pascal code With a C code alongside craftofcoding.wordpress.com/2018/04/26/the-problem-with-pi-ii/So I did tweak it until it run as seemingly should I compared 100 characters it outputs with first part from the page. Got 100% match n =100 ' number of digits of pi L =INT(10*n/3) 'var i, j, k, q, x, nines, predigit : integer; DIM a(L) ' of longint;
FOR j = 1 TO L a(j) = 2 '{Start with 2s} NEXT j nines= 0 predigit = 0 '{First predigit is a 0} FOR j = 1 TO n q = 0 FOR i = L TO 1 STEP -1 '{Work backwards} x= 10*a(i)+q*i a(i) = x MOD (2*i-1) q = INT(x/(2*i-1)) NEXT i a(1) = q MOD 10 q =INT(q/10) IF q = 9 THEN nines = nines + 1 ELSE IF q=10 THEN PRINT (predigit+1); FOR k = 1 to nines PRINT "0"; '{zeros} NEXT k predigit = 0 nines = 0 'END IF ELSE PRINT predigit; predigit =q IF nines <> 0 THEN FOR k = 1 TO nines PRINT "9"; NEXT k nines = 0 END IF END IF END IF NEXT j PRINT predigit ; PRINT 'FOR x=1 TO L ' PRINT a(x); 'NEXT x PRINT "Program Terminated" END
|
|
|
Post by bencooper on Nov 3, 2023 11:49:44 GMT -5
Thanks to Rod and Anatoly for helpful responses ! I checked but couldn't immediately find what Anatoly "tweaked" but it worked a dream ! 1000 digits in 33 seconds, with no demand on memory AFAIK !!
|
|
|
Post by tsh73 on Nov 3, 2023 12:56:00 GMT -5
Basically, moving last "PRINT predigit ;" after last "next j" Uncommenting all prints ('PRINT "0";, 'PRINT predigit) And making sure there is a ( ; ) after every print. Also I moved "Next K" up but it does not matter.
|
|