Alhiris

Untitled

Mar 23rd, 2021
572
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. generatePoint <- function () {
  2.   pct <- c(runif(1), runif(1))
  3.   return (pct)
  4. }
  5.  
  6. getDistance <- function(p1, p2)
  7. {
  8.   ans <- sqrt(sum((p1 - p2) ^ 2))
  9.  
  10.   return (ans)
  11. }
  12.  
  13.  
  14. getPPoints <- function (n=1000) {
  15.   center = c(1/2,1/2)
  16.   p <- 0
  17.   for (i in 1:n){
  18.     point <- generatePoint()
  19.     distance <- getDistance(center, point)
  20.     if (distance <= 0.5) {
  21.       p <- p + 1
  22.     }
  23.   }
  24.   return (p)
  25. }
  26.  
  27. n <- 1000000
  28. p <- getPPoints(n)
  29.  
  30. my_pi <- (p*4) / n
  31. my_pi
  32.  
  33. pi # real pi
  34. #lucky https://i.imgur.com/2zCZrUp.png
  35.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×