Queer European MD passionate about IT

Mission409Solutions.Rmd 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. ---
  2. title: "Mission 409 Solutions"
  3. output: html_document
  4. ---
  5. # Developing A Mobile App For Alleviating Lottery Addiction
  6. This RMarkdown file is intended to lay out the logic of a mobile app designed for those addicted to the lottery. By showing a user how to calculate the incredibly small probabilities of winning the lottery, we hope that the app will help them better grasp that buying multiple lottery tickets will do little to help them win. Through this understanding, they will hopefully stop purchasing lottery tickets in an unhealthy manner.
  7. # Core Functions
  8. ```{r}
  9. factorial <- function(n) {
  10. product = 1
  11. for (i in 1:n) {
  12. product = product * i
  13. }
  14. return(product)
  15. }
  16. combinations <- function(n, k) {
  17. numerator <- factorial(n)
  18. denominator <- factorial(k) * factorial(n - k)
  19. return(numerator / denominator)
  20. }
  21. ```
  22. # One-Ticket Probability
  23. ```{r}
  24. one_ticket_probability <- function(nums) {
  25. total_combinations <- combinations(49, 6)
  26. prob <- (1 / total_combinations) * 100
  27. pretty_prob <- sprintf("%1.9f", prob)
  28. s <- paste("You have a ", pretty_prob, "% chance of winning the big prize.", sep = "")
  29. return(s)
  30. }
  31. one_ticket_probability(c(1, 2, 3, 4, 5, 6))
  32. ```
  33. # Historical Data Check for Canada Lottery
  34. ```{r, message = FALSE, warning = FALSE}
  35. library(tidyverse)
  36. lottery649 <- read_csv("649.csv")
  37. print(dim(lottery649))
  38. ```
  39. ```{r}
  40. head(lottery649, 3)
  41. ```
  42. ```{r}
  43. tail(lottery649, 3)
  44. ```
  45. # Function for Historical Data Check
  46. ```{r}
  47. historical_lots <- pmap(
  48. list(
  49. u <- lottery649$`NUMBER DRAWN 1`,
  50. v <- lottery649$`NUMBER DRAWN 2`,
  51. w <- lottery649$`NUMBER DRAWN 3`,
  52. x <- lottery649$`NUMBER DRAWN 4`,
  53. y <- lottery649$`NUMBER DRAWN 5`,
  54. z <- lottery649$`NUMBER DRAWN 6`
  55. ),
  56. .f <- function(u, v, w, x, y, z) { c(u, v, w, x, y, z) }
  57. )
  58. ```
  59. ```{r}
  60. library(sets)
  61. check_historical_occurrences <- function(lot, historical_lots = historical_lots) {
  62. historical_matches <- map(historical_lots, function(x) {setequal(x, lot)})
  63. num_past_matches <- sum(unlist(historical_matches))
  64. s <- paste("The combination you entered has appeared ",
  65. num_past_matches,
  66. " times in the past. ",
  67. "Your chance of winning the big prize in the next drawing using this combination is 0.0000072%", sep = "")
  68. return(s)
  69. }
  70. check_historical_occurrences(c(3, 12, 11, 14, 41, 43))
  71. ```
  72. # Multi-ticket Probability
  73. ```{r}
  74. multi_ticket_probability <- function(n) {
  75. total_combinations <- combinations(49, 6)
  76. prob <- (n / total_combinations) * 100
  77. pretty_prob <- sprintf("%1.9f", prob)
  78. s <- paste("you have a ", pretty_prob, "% chance of winning the big prize.", sep = "")
  79. return(s)
  80. }
  81. ```
  82. ```{r}
  83. test_amounts <- c(1, 10, 100, 10000, 1000000, 6991908, 13983816)
  84. for (n in test_amounts) {
  85. print(paste("For ", n, " tickets, ", multi_ticket_probability(n), sep = ""))
  86. }
  87. ```
  88. # Less Winning Numbers
  89. ```{r}
  90. probability_less_6 <- function(n) {
  91. n_combinations_ticket = combinations(6, n)
  92. n_combinations_remaining = combinations(49 - n, 6 - n)
  93. successful_outcomes = n_combinations_ticket * n_combinations_remaining
  94. n_combinations_total = combinations(49, 6)
  95. prob = (successful_outcomes / n_combinations_total) * 100
  96. pretty_prob <- sprintf("%1.9f", prob)
  97. s <- paste("you have a ", pretty_prob, "% chance of winning the big prize.", sep = "")
  98. return(s)
  99. }
  100. ```
  101. ```{r}
  102. winning_nums <- c(3, 4, 5)
  103. for (n in winning_nums) {
  104. print(paste("For ", n, " tickets, ", probability_less_6(n), sep = ""))
  105. }
  106. ```