Question 1

A fair coin is flipped and then a fair six-sided die is rolled. The 12 outcomes are equally likely. Define the random variable \(X\) as \(X = 1\) if the coin shows heads and the die shows an even number, and \(X = 0\) otherwise. Use a Monte Carlo simulation to find \(P(X = 1)\) and \(P(X = 0)\).

coin <- c("Heads","Tails") # coin choices
die <- 1:6 # fair six-sided die
counter1 <- 0 # X = 1 counter
counter2 <- 0 # X = 0 counter
N <- 1e6 # 1 million trials
for (i in 1:N) {
  coin_pick <- sample(x = coin,size = 1,replace = T)
  die_roll <- sample(x = die,size = 1,replace = T)
  if (coin_pick == "Heads" & die_roll %% 2 == 0) {
    counter1 <- counter1 + 1
  } else {
    counter2 <- counter2 + 1
  }
}
probability_X1 <- counter1 / N # P(X = 1)
probability_X2 <- counter2 / N # P(X = 0)
cat("P(X = 1) =",probability_X1,"\n")
## P(X = 1) = 0.250184
cat("P(X = 0) =",probability_X2,"\n")
## P(X = 0) = 0.749816

Question 2

Given the defined data frame below, find the lightest object.

Note: Some of the values in the “Weight” column are in ounces (oz) and some are in pounds (lb).

  1. Define the data frame. The objects with an asterisk (*) means the weight is in ounces. The rest are in pounds.
q2_data <- data.frame(Object = c("Watermelon","Ostrich Egg","Bluefin Tuna","Turnip","Sardine*","Hen's Egg*","Apple*","Shark","Pineapple","Potato*"),
                      Weight = c(20,3,500,2,6,2,5,40000,2,6))
q2_data
##          Object Weight
## 1    Watermelon     20
## 2   Ostrich Egg      3
## 3  Bluefin Tuna    500
## 4        Turnip      2
## 5      Sardine*      6
## 6    Hen's Egg*      2
## 7        Apple*      5
## 8         Shark  40000
## 9     Pineapple      2
## 10      Potato*      6
  1. Find the lightest object.

Note: 1 pound = 16 ounces

# install.packages("tidyverse")
library(tidyverse)
## Warning: package 'lubridate' was built under R version 4.5.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.1     ✔ stringr   1.5.2
## ✔ ggplot2   4.0.0     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.1.0     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
lightest_object <- q2_data %>%
  mutate(Weight = if_else(
    str_detect(Object,"\\*"), # detecting the asterisk
    Weight / 16, # convert ounces to pounds
    Weight # keeping pounds as it is
  )) %>%
  slice_min(Weight,n = 1) %>% # finding the smallest weight
  pull(Object) %>% # obtaining the object with smallest weight
  str_remove("\\*") # removing the asterisk for a clean answer
cat("The lightest object is:",lightest_object,"\n")
## The lightest object is: Hen's Egg

Question 3

Estimate how many of the whole numbers from 1 to 900 are divisible by 5.

# install.packages("comprehenr")
library(comprehenr)
## Warning: package 'comprehenr' was built under R version 4.5.2
numbers_div_5 <- to_vec(for (x in 1:900) if (x %% 5 == 0) x)
cat("There are",length(numbers_div_5),"whole numbers from 1 to 900 that are divisible by 5.","\n")
## There are 180 whole numbers from 1 to 900 that are divisible by 5.

Question 4

Which number in \([3762,4873,5984,7095]\) is divisible by 9?

# install.packages("comprehenr")
library(comprehenr)
div_by_9 <- to_vec(for (y in c(3762,4873,5984,7095)) if (y %% 9 == 0) y)
cat("The number",div_by_9,"is divisible by 9.","\n")
## The number 3762 is divisible by 9.

Question 5

There are 2 dice. Each die has 8 rectangular faces, each with a number 1 to 8, and two octagonal faces at the ends. Ulysses rolls the two dice on a flat surface until they settle with numbers on the top faces. Because the dice are rolled, they cannot land on the octagonal faces. He then multiplies the two numbers on the top face. What is the probability that the product is even? Use a Monte Carlo simulation to aid in this problem.

die1 <- 1:8 # octagonal die 1
die2 <- 1:8 # octagonal die 2
counter <- 0 # number of times the product is even
num_trials <- 1e6 # 1 million trials
for (j in 1:num_trials) {
  die_roll1 <- sample(x = die1,size = 1,replace = T)
  die_roll2 <- sample(x = die2,size = 1,replace = T)
  if ((die_roll1 * die_roll2) %% 2 == 0) {
    counter <- counter + 1
  }
}
probability <- counter / num_trials
cat("The probability that the product is even is:",probability,"\n")
## The probability that the product is even is: 0.749895