From 9c27df5b323565e3a5bcca4d5949e9eee5b91926 Mon Sep 17 00:00:00 2001 From: Filipe Rodrigues Date: Sat, 17 Jun 2023 15:10:06 +0100 Subject: [PATCH] Fixed 5. --- 5/answer.txt | 1 + 5/script.R | 38 ++++++++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 14 deletions(-) create mode 100644 5/answer.txt diff --git a/5/answer.txt b/5/answer.txt new file mode 100644 index 0000000..84cfc90 --- /dev/null +++ b/5/answer.txt @@ -0,0 +1 @@ +0.1321 diff --git a/5/script.R b/5/script.R index d823584..6e89a56 100755 --- a/5/script.R +++ b/5/script.R @@ -1,7 +1,7 @@ #!/bin/env Rscript # Set the seed for the RNG -set.seed(1966) +# set.seed(1966) p <- 0.4 @@ -23,22 +23,32 @@ dist_fn <- function(x) { } # TODO: This doesn't seem right... +values_length <- 1090 values <- vector() -cur_idx <- 0 -while (length(values) < 1090) { +while (length(values) < values_length) { u <- runif(1, min = 0, max = 1) - min <- dist_fn(cur_idx - 1) - max <- dist_fn(cur_idx) + x <- 0 + while (TRUE) { + min <- dist_fn(x - 1) + max <- dist_fn(x) - message("Trying ", u, " (", min, "..", max, ")") - if (u > min && u <= max) { - message("Successful ", u) - values <- append(values, TRUE) - } else { - values <- append(values, FALSE) + if (u > min && u <= max) { + values <- append(values, x) + break + } + + x <<- x + 1 } - cur_idx <- cur_idx + 1 } -print(cur_idx) -print(values) + +true_mean <- (1 - p) / p +values_mean <- mean(values) +values_sd <- sd(values) + + +values_count <- sum(sapply(values, function(value) { + value >= true_mean + values_sd && value >= values_mean +})) +print(values_count) +print(values_count / values_length)