I want to add a loop to the code below so that it runs four times for
n <- c(1000, 10000, 100000, 1000000)
And to return a matrix that contains n
values and its solution pi
? Thanks!
Here is my code for a single value of n
:
n <- 1000
x <- c(runif(n, -1,1))
y <-c(runif(n, -1,1))
points <- data.frame(cbind(x,y))
z <- points$x^2 + points$y^2
pi <- function(n,points){
y <- 4*length(z[z<=1])/n
return(y)
}
pi(n, points)