Jump to content

Arithmetic-geometric mean/Calculate Pi: Difference between revisions

+R
(+R)
Line 1,186:
{{out}}
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067</pre>
 
=={{header|R}}==
 
{{libheader|Rmpfr}}
 
<lang rsplus>agm <- function(n, prec) {
s <- mpfr(0, prec)
a <- mpfr(1, prec)
g <- sqrt(mpfr("0.5", prec))
p <- as.bigz(4)
for (i in seq(n)) {
m <- (a + g) / 2L
g <- sqrt(a * g)
a <- m
s <- s + p * (a * a - g * g)
p <- p + p
}
4L * a * a / (1L - s)
}
 
# Compute pi to more than 300000 decimal digits:
p <- 2^20
roundMpfr(agm(18, p) - Const("pi", p), 64)
# 1 'mpfr' number of precision 64 bits
# [1] -5.34912482587363425768e-315648</lang>
 
=={{header|Racket}}==
1,336

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.