Arithmetic-geometric mean/Calculate Pi: Difference between revisions

Line 736:
 
{{works with|Julia|1.2}}
 
=={{header|jq}}==
'''Works with gojq, the Go implementation of jq'''
 
This entry presupposes a rational arithmetic module with a fast
`rsqrt` function
for taking square roots of rationals; such a module can be found at [[Arithmetic/Rational]].
<lang jq># include "rational"; # a reminder
 
def pi(precision):
(precision | (. + log) | ceil) as $digits
| def sq: . as $in | rmult($in; $in) | rround($digits);
{an: r(1;1),
bn: (r(1;2) | rsqrt($digits)),
tn: r(1;4),
pn: 1 }
| until (.pn > $digits;
.an as $prevAn
| .an = (rmult(radd(.bn; .an); r(1;2)) | rround($digits) )
| .bn = ([.bn, $prevAn] | rmult | rsqrt($digits) )
| .tn = rminus(.tn; rmult(rminus($prevAn; .an)|sq; .pn))
| .pn *= 2
)
| rdiv( radd(.an; .bn)|sq; rmult(.tn; 4))
| r_to_decimal(precision);
 
pi(500)</lang>
{{out}}
<pre>
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912
</pre>
 
 
=={{header|Julia}}==
2,482

edits