All Primes After Removal Single Digits

[This article was first published on Numbers around us - Medium, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Excel BI’s Excel Challenge #303— solved in R

Defining the Puzzle

Our puzzle revolves around identifying numbers that, when any digit is removed, still remain prime. Let’s take a closer look:

Example 1: Consider the number 371.

  • Removing the first digit (3) gives 71, which is prime.
  • Removing the second digit (7) gives 31, which is prime.
  • Removing the third digit (1) gives 37, which is prime.

Example 2: For the number 3311:

  • Removing the first digit (3) results in 311 — prime!
  • Removing the second digit (3) again gives 311, which remains prime.
  • Removing either of the last two digits (1s) results in 331, which is also prime.

Using this logic, our task is to identify all such numbers from an Excel dataset.

Loading Data from Excel

Before diving into our R-based solutions, we need our data. In the Excel file, our data is generally divided into two segments: input (the numbers to check) and test (the expected answers). You can find the Excel file for this exercise here.

library(tidyverse)
library(readxl)
library(primes)
library(data.table)

input = read_excel(“PATH/All Primes After Removal Single Digits.xlsx”, range = “A1:A10”)
test = read_excel(“PATH/All Primes After Removal Single Digits.xlsx”, range = “B1:B5”)

Additional note:
Usually I were writing is_prime function for checking primes, but somebody showed me primes package so:

# Instead of this:
is_prime <- function(n) {
  if (n <= 1) return(FALSE)
  if (n == 2) return(TRUE)
  if (n %% 2 == 0) return(FALSE)
  for (i in 3:sqrt(n)) {
    if (n %% i == 0) return(FALSE)
  }
  return(TRUE)
}

# I use:
primes::is_prime() # without namespaces if package is loaded.

Approach 1: Tidyverse with purrr

primes_inside = function(n){
  vec = str_split(as.character(n), "")[[1]]
  res = map(1:length(vec), ~ as.numeric(paste0(vec[-c(.x)], collapse = "")))
  x = map_lgl(res, is_prime) %>% all()
  }

result = input %>%
  mutate(my_answer = map_lgl(Numbers, primes_inside)) %>%
  filter(my_answer)

Approach 2: Base R

primes_inside_baseR <- function(n) {
  vec <- unlist(strsplit(as.character(n), ""))
  res <- sapply(1:length(vec), function(i) as.numeric(paste(vec[-i], collapse = "")))
  all(sapply(res, is_prime))
}

result_baseR = input %>%
  mutate(my_answer = map_lgl(Numbers, primes_inside_baseR)) %>%
  filter(my_answer)

Approach 3: Data.table

primes_inside_dt <- function(n) {
  dt <- data.table(vec = unlist(strsplit(as.character(n), "")))
  res <- dt[, sapply(1:.N, function(i) as.numeric(paste(vec[-i], collapse = "")))]
  all(sapply(res, is_prime))
}

result_dt = input %>%
  mutate(my_answer = map_lgl(Numbers, primes_inside_dt)) %>%
  filter(my_answer)

Validating Our Solutions

After implementing our methodologies, it’s crucial to validate our results against the expected answers. Cross-checking ensures that regardless of the approach, we consistently arrive at the correct solution.

identical(result$Numbers, test$`Expected Answer`)
#> [1] TRUE

identical(result_baseR$Numbers, test$`Expected Answer`)
#> [1] TRUE

identical(result_dt$Numbers, test$`Expected Answer`)
#> [1] TRUE

I hope you found this deep dive into the Excel puzzle using R methodologies insightful. Which approach did you find the most intuitive? Do you have any other methods or optimizations to share? Feel free to share your thoughts, solutions, or suggestions in the comments below. Happy coding!


All Primes After Removal Single Digits was originally published in Numbers around us on Medium, where people are continuing the conversation by highlighting and responding to this story.

To leave a comment for the author, please follow the link and comment on their blog: Numbers around us - Medium.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)