math

Deck of Cards

Years ago, I had to simulate a deck of playing cards in a Python course. Oddly enough, I don’t recall ever carrying out this task in R. Today, I saw a neat code snippet about the crossing() command in tidyr by Tan Ho in the R4DS Slack channel, so let us give it a try. library("tidyverse") suit <- c("D", "H", "C", "S") rank <- c(2:10, "J", "Q", "K", "A") deck <- tidyr::crossing(rank, suit) |> dplyr::mutate(card = paste0(rank, suit)) This yields a data frame with 3 columns: rank, suit, card.

Continue reading

Radically Infinite

In today’s procrastination effort, I want to generate the LaTeX code to produce an infinite radical expansion for the number 3, and generate the code using R. … inspired by math teacher Chris Smith tweet First, let me see if I can simply generate the pattern of integers. I will use the letter capital “S” and later replace it with the square root symbol. stem <- "S 1 + " print(paste(stem, "8")) ## [1] "S 1 + 8" for(a in 2:5){ b <- (a+1)^2-1 print(paste(stem, a, "*", b/a)) print(paste(stem, a, "S", (b/a)^2)) print(paste(stem, a, "S", 1, "+", (b/a)^2- 1)) stem <- paste(stem, a, "S", 1, "+") } ## [1] "S 1 + 2 * 4" ## [1] "S 1 + 2 S 16" ## [1] "S 1 + 2 S 1 + 15" ## [1] "S 1 + 2 S 1 + 3 * 5" ## [1] "S 1 + 2 S 1 + 3 S 25" ## [1] "S 1 + 2 S 1 + 3 S 1 + 24" ## [1] "S 1 + 2 S 1 + 3 S 1 + 4 * 6" ## [1] "S 1 + 2 S 1 + 3 S 1 + 4 S 36" ## [1] "S 1 + 2 S 1 + 3 S 1 + 4 S 1 + 35" ## [1] "S 1 + 2 S 1 + 3 S 1 + 4 S 1 + 5 * 7" ## [1] "S 1 + 2 S 1 + 3 S 1 + 4 S 1 + 5 S 49" ## [1] "S 1 + 2 S 1 + 3 S 1 + 4 S 1 + 5 S 1 + 48" Not only do I need to now insert the square root symbol, but I would also need to close the parentheses.

Continue reading