Pi: Difference between revisions

2,627 bytes added ,  7 months ago
no edit summary
imported>Chinhouse
No edit summary
Line 3,952:
96442881097566593344612847564823378678316527120190
91456485669234603486104543266482133936072602491412
</pre>
 
=={{header|MiniScript}}==
This implementation is for use with [http://miniscript.org/MiniMicro Mini Micro]. It uses the Rabinowitz-Wagon algorithem.
<syntaxhighlight lang="miniscript">// Calculate pi using the Rabinowitz-Wagon algorithm
digits = input("Enter number of digits to calculate after decimal point: ").val
// I've seen variations of this "precision" calculation from
// 10 * digits
// to
// floor(10 * digits / 3) + 16
// A larger value provides a more precise calculation but also
// takes longer to run. Based on my testing, this calculation
// below for precision produces accurate output for inputs
// from 1 to 4000 - haven't tried larger than this.
precision = floor(10 * digits / 3) + 4
A = [2] * precision
nines = 0
predigit = 0
cnt = 0
while cnt <= digits
carry = 0
for i in range(precision - 1, 1, -1)
temp = 10 * A[i] + carry * i
A[i] = temp % (2 * i - 1)
carry = floor(temp/(2 * i - 1))
end for
A[1] = carry % 10
carry = floor(carry / 10)
current = carry
if current == 9 then
nines += 1
else if current == 10 then
print (predigit+1), ""
cnt += 1
if nines > 0 then
print "9" * nines, ""
cnt += nines
end if
predigit = 0
nines = 0
else
// the first digit produced is always a zero
// don't need to see that
if cnt != 0 then print predigit, ""
cnt += 1
predigit = current
if nines > 0 then
print "9" * nines, ""
cnt += nines
end if
nines = 0
end if
if cnt == 2 then print ".", ""
end while
print str(predigit) * (cnt < digits + 2)</syntaxhighlight>
 
{{out}}
<pre>
Enter number of digits to calculate after decimal point: 1000
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903690113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051329005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710199031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066139019278766111959092164201989
</pre>
 
Anonymous user