DECIMAL BASIC PROGRAM
Code:
! Accelerated Viète by Chan-Wasicki Method
OPTION ARITHMETIC DECIMAL_HIGH
INPUT PROMPT "number of decimal digits: ":nd
LET tm = TIME
LET n = CEIL(4/3*SQR(nd)) ! n = number of factors in Viète's Product
LET m = n ! m = number of coefficients in the correction polynomial
PRINT "n, m: ";n
LET p = 2
LET s = 0
LET v = 1
FOR k = 1 TO n
LET s = SQR(2 + s)
LET v = s*v
LET p = p + p
NEXT k
LET pv = p/v
LET n = 1
LET w = 1/(v*v)
LET x = w
LET c = 1
LET p = 4
FOR k = 1 TO m
LET n = 2*n*(2*k - 1)/k ! n = coefficient numerator
LET d = p*(2*k + 1) ! d = coefficient denominator
LET c = c + n*x/d
LET x = x*w
LET p = 4*p
NEXT k
LET r = TIME - tm
LET r$ = STR$(pv*c - INT(pv*c))
PRINT
PRINT STR$(INT(pv*c));
PRINT r$(0:1);
FOR i = 2 TO nd + 1
PRINT r$(i:i);
IF MOD((i - 1),10) = 0 THEN PRINT " ";
IF MOD((i - 1),50) = 0 THEN
PRINT USING " (%%%%)": i - 1
PRINT REPEAT$(" ",1 + LEN(STR$(INT(pv*c))));
END IF
NEXT i
IF MOD (i - 2,50) <> 0 OR nd = 0 THEN PRINT
PRINT
PRINT "Runtime: ";
PRINT USING "0.##": r;
PRINT " seconds"
END
Code:
number of decimal digits: 1000
n, m: 43
3.1415926535 8979323846 2643383279 5028841971 6939937510 (0050)
5820974944 5923078164 0628620899 8628034825 3421170679 (0100)
8214808651 3282306647 0938446095 5058223172 5359408128 (0150)
4811174502 8410270193 8521105559 6446229489 5493038196 (0200)
4428810975 6659334461 2847564823 3786783165 2712019091 (0250)
4564856692 3460348610 4543266482 1339360726 0249141273 (0300)
7245870066 0631558817 4881520920 9628292540 9171536436 (0350)
7892590360 0113305305 4882046652 1384146951 9415116094 (0400)
3305727036 5759591953 0921861173 8193261179 3105118548 (0450)
0744623799 6274956735 1885752724 8912279381 8301194912 (0500)
9833673362 4406566430 8602139494 6395224737 1907021798 (0550)
6094370277 0539217176 2931767523 8467481846 7669405132 (0600)
0005681271 4526356082 7785771342 7577896091 7363717872 (0650)
1468440901 2249534301 4654958537 1050792279 6892589235 (0700)
4201995611 2129021960 8640344181 5981362977 4771309960 (0750)
5187072113 4999999837 2978049951 0597317328 1609631859 (0800)
5024459455 3469083026 4252230825 3344685035 2619311881 (0850)
7101000313 7838752886 5875332083 8142061717 7669147303 (0900)
5982534904 2875546873 1159562863 8823537875 9375195778 (0950)
1857780532 1712268066 1300192787 6611195909 2164201818 (1000)
Runtime: 0.26 seconds
The last three digits should be 989, but we are here at the limit of DECIMAL BASIC precision.
Like number of iterations for the Viète's Product and the correction polynomial, not sure whether this gives optimal efficiency. Anyway, sublinear convergence rate (the number of iterations depends on the square root of the number of digits).
Edited to fix a typo
----------
For one thousand digits, only 11 iterations of the first loop and 165 of the second loop would suffice. I have added six iterations to the latter as a safety margin for this range. Almost 2.5 times faster. Perhaps not too far from the ideal m/n ratio. The last two digits should be 89.
Code:
! Accelerated Viète by Chan-Wasicki Method
OPTION ARITHMETIC DECIMAL_HIGH
INPUT PROMPT "number of decimal digits: ":nd
LET tm = TIME
LET n = CEIL(9/26*SQR(nd)) ! n = number of factors in Viète's Product
LET m = 15*n + 6 ! m = number of coefficients in the correction polynomial
PRINT "n, m: ";n;m
LET p = 2
LET s = 0
LET v = 1
FOR k = 1 TO n
LET s = SQR(2 + s)
LET v = s*v
LET p = p + p
NEXT k
LET pv = p/v
LET n = 1
LET w = 1/(v*v)
LET x = w
LET c = 1
LET p = 4
FOR k = 1 TO m
LET n = 2*n*(2*k - 1)/k ! n = coefficient numerator
LET d = p*(2*k + 1) ! d = coefficient denominator
LET c = c + n*x/d
LET x = x*w
LET p = 4*p
NEXT k
LET r = TIME - tm
LET r$ = STR$(pv*c - INT(pv*c))
PRINT
PRINT STR$(INT(pv*c));
PRINT r$(0:1);
FOR i = 2 TO nd + 1
PRINT r$(i:i);
IF MOD((i - 1),10) = 0 THEN PRINT " ";
IF MOD((i - 1),50) = 0 THEN
PRINT USING " (%%%%)": i - 1
PRINT REPEAT$(" ",1 + LEN(STR$(INT(pv*c))));
END IF
NEXT i
IF MOD (i - 2,50) <> 0 OR nd = 0 THEN PRINT
PRINT
PRINT "Runtime: ";
PRINT USING "0.##": r;
PRINT " seconds"
END
Code:
number of decimal digits: 1000
n, m: 11 171
3.1415926535 8979323846 2643383279 5028841971 6939937510 (0050)
5820974944 5923078164 0628620899 8628034825 3421170679 (0100)
8214808651 3282306647 0938446095 5058223172 5359408128 (0150)
4811174502 8410270193 8521105559 6446229489 5493038196 (0200)
4428810975 6659334461 2847564823 3786783165 2712019091 (0250)
4564856692 3460348610 4543266482 1339360726 0249141273 (0300)
7245870066 0631558817 4881520920 9628292540 9171536436 (0350)
7892590360 0113305305 4882046652 1384146951 9415116094 (0400)
3305727036 5759591953 0921861173 8193261179 3105118548 (0450)
0744623799 6274956735 1885752724 8912279381 8301194912 (0500)
9833673362 4406566430 8602139494 6395224737 1907021798 (0550)
6094370277 0539217176 2931767523 8467481846 7669405132 (0600)
0005681271 4526356082 7785771342 7577896091 7363717872 (0650)
1468440901 2249534301 4654958537 1050792279 6892589235 (0700)
4201995611 2129021960 8640344181 5981362977 4771309960 (0750)
5187072113 4999999837 2978049951 0597317328 1609631859 (0800)
5024459455 3469083026 4252230825 3344685035 2619311881 (0850)
7101000313 7838752886 5875332083 8142061717 7669147303 (0900)
5982534904 2875546873 1159562863 8823537875 9375195778 (0950)
1857780532 1712268066 1300192787 6611195909 2164201968 (1000)
Runtime: 0.11 seconds
----------
Using a recurrent formula also for the denominator might be better...
Code:
! Accelerated Viète by Chan-Wasicki Method
OPTION ARITHMETIC DECIMAL_HIGH
INPUT PROMPT "number of decimal digits: ":nd
LET n = CEIL(9/26*SQR(nd)) ! n = number of factors in Viète's Product
LET m = 15*n + 6 ! m = number of coefficients in the correction polynomial
PRINT "n, m: ";n;m
LET m = 2*m - 1
LET tm = TIME
LET p = 2
LET s = 0
LET v = 1
FOR k = 1 TO n
LET s = SQR(2 + s)
LET v = s*v
LET p = p + p
NEXT k
LET pv = p/v
LET n = 1 ! n = coefficient numerator
LET d = 1 ! d = coefficient denominator
LET w = 1/(v*v)
LET x = w
LET c = 1
FOR k = 1 TO m STEP 2
LET n = 4*n*k/(k + 1)
LET d = d*(4*k+8)/k
LET c = c + n/d*x
LET x = x*w
NEXT k
LET r = TIME - tm
LET r$ = STR$(pv*c - INT(pv*c))
PRINT
PRINT STR$(INT(pv*c));
PRINT r$(0:1);
FOR i = 2 TO nd + 1
PRINT r$(i:i);
IF MOD((i - 1),10) = 0 THEN PRINT " ";
IF MOD((i - 1),50) = 0 THEN
PRINT USING " (%%%%)": i - 1
PRINT REPEAT$(" ",1 + LEN(STR$(INT(pv*c))));
END IF
NEXT i
IF MOD (i - 2,50) <> 0 OR nd = 0 THEN PRINT
PRINT
PRINT "Runtime: ";
PRINT USING "0.##": r;
PRINT " seconds"
END
...and faster!
Code:
...
1857780532 1712268066 1300192787 6611195909 2164201968 (1000)
Runtime: 0.09 seconds