Skip to content

Instantly share code, notes, and snippets.

@ps-pat
Last active September 18, 2023 15:23
Show Gist options
  • Save ps-pat/7b2472679ee043c92751390570151ccc to your computer and use it in GitHub Desktop.
Save ps-pat/7b2472679ee043c92751390570151ccc to your computer and use it in GitHub Desktop.
MAT8186 -- Cours 3
## Debug
fact <- function(x) {
x < 0 && return(x)
x * fact(x - 1.0)
}
## Profile
cutVector <- function(vec, m) {
n <- length(vec) / m
lapply(0:(m-1),
\(x) vec[seq(n * x + 1, n * (x + 1))])
}
dist0 <- function(m, n = 1e4) {
dat <- sample(0:9,
size = n * m,
replace = TRUE)
dat_chopped <- cutVector(dat, m)
sapply(dat_chopped, \(v) mean(v == 0))
}
## Benchmark
fact_for <- function(x) {
acc = 1
for (k in 1:x)
acc <- acc * k
acc
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment