Skip to content

Instantly share code, notes, and snippets.

@SimonCoulombe
Created February 7, 2018 16:48
Show Gist options
  • Save SimonCoulombe/19e3ba2ae9c9c52bc8626fd0282bcc8b to your computer and use it in GitHub Desktop.
Save SimonCoulombe/19e3ba2ae9c9c52bc8626fd0282bcc8b to your computer and use it in GitHub Desktop.
fermat's library pick 2.718 numbers implemented in R
library(tidyverse)
set.seed(2718)
picks <- function(){
x <- 0; i <- 0
while(x <1){
i <- i+1
x <- x + runif(n=1,min=0,max=1)
}
return (i)
}
myseq <- seq(1:100000) %>% as_tibble() %>%
rowwise() %>%
mutate(iter = picks()) %>%
ungroup() %>%
mutate(x = row_number(),
cummean = cumsum(iter) / row_number())
myseq %>%
ggplot()+
geom_line(aes(x= x, y = cummean),color="blue")+
geom_line(aes(x=x, y= exp(1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment