Created
May 15, 2019 21:34
-
-
Save MartinMSPedersen/f46742325838f330a33001e938fc905e to your computer and use it in GitHub Desktop.
Compute 1 million of PI using R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(gmp) | |
ONE <- as.bigz("1") | |
TWO <- as.bigz("2") | |
THREE <- as.bigz("3") | |
FOUR <- as.bigz("4") | |
SEVEN <- as.bigz("7") | |
TEN <- as.bigz("10") | |
q <- as.bigz("1") | |
r <- as.bigz("0") | |
t <- as.bigz("1") | |
k <- as.bigz("1") | |
n <- as.bigz("3") | |
l <- as.bigz("3") | |
digits <- 1e6 | |
first <- TRUE | |
while (digits > 0) { | |
digits <- digits - 1 | |
if ((FOUR * q + r - t) < (n * t)) { | |
cat(as.integer(n)) | |
if (first) { | |
cat(".") | |
first <- FALSE | |
} | |
nr <- as.bigz(TEN * (r - n * t)) | |
n <- as.bigz(((TEN * (THREE * q + r)) %/% t) - (TEN * n)) | |
q <- as.bigz(q * TEN) | |
r <- as.bigz(nr) | |
} else { | |
nr <- as.bigz((TWO * q + r) * l) | |
nn <- as.bigz((q * (SEVEN * k + TWO) + r * l) %/% (t * l)) | |
q <- as.bigz(q * k) | |
t <- as.bigz(t * l) | |
l <- as.bigz(l + TWO) | |
k <- as.bigz(k + ONE) | |
n <- as.bigz(nn) | |
r <- as.bigz(nr) | |
} | |
} | |
cat("\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment