library(Rmpfr)

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)
}

1e6 * log(10) / log(2)



p <- 3322000
x <- agm(20, p)


roundMpfr(x - Const("pi", p), 64)




f <- file("pi.txt", "w")
writeLines(formatMpfr(x, 1e6), f)
close(f)