Site icon R-bloggers

Sumproduct number

[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 #312 — solved in R

Defining the Puzzle:

In current puzzle we need to check if digits of our numbers has some magic properties. Seems easy, but it is not usually the case, when ExcelBI prepare riddle.

List all Sumproduct numbers from column A.
A Sum product number is that number which is perfectly divisible by both the sum of digits and product of digits.
Ex. 135 which is perfectly divisible by both 1+3+5=9 and 1*3*5 = 15

Loading Data from Excel:

Lets start loading data and libraries:

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

input = read_excel(“Sumproduct Number.xlsx”, range = “A1:A10”)
test = read_excel(“Sumproduct Number.xlsx”, range = “B1:B3”)

Approach 1: Tidyverse with purrr

is_sum_product_tv = function(number){
 digits = as.numeric(str_split(as.character(number), “”)[[1]])
 sum = reduce(digits, `+`)
 product = reduce(digits, `*`)
 
 check = number %% sum == 0 & number %% product == 0
 return(check) 
}

 result_tv = input %>%
 mutate(my_answer = map_lgl(Number, is_sum_product_tv)) %>%
 filter(my_answer) %>%
 select(my_answer = Number)

Approach 2: Base R

is_sum_product_base <- function(number) {
 digits <- as.numeric(unlist(strsplit(as.character(number), “”)))
 
 sum_digits <- sum(digits)
 product_digits <- prod(digits)
 
 check <- number %% sum_digits == 0 & number %% product_digits == 0
 return(check)
 }

result_base <- vector(“list”, length = nrow(input))
 for (i in seq_along(input$Number)) {
 result_base[[i]] <- is_sum_product_base(input$Number[i])
 }

result_base = cbind(data.frame(my_answer = unlist(result_base)),input)
result_base = result_base %>% filter(my_answer == TRUE) %>% select(my_answer = Number)

Approach 3: Data.table

input_dt = setDT(input)
 is_sum_product_dt <- function(number) {
 digits <- as.numeric(unlist(strsplit(as.character(number), “”)))
 sum_digits <- sum(digits)
 product_digits <- prod(digits) 
 check <- number %% sum_digits == 0 & number %% product_digits == 0
 return(check)
 }
 
 input_dt[, my_answer := lapply(Number, is_sum_product_dt)]
 result_dt <- input_dt[my_answer == TRUE, .(Number)]

Validating Our Solutions:

identical(test$`Answer Expected`, result_tv$my_answer)
 #[1] TRUE
identical(test$`Answer Expected`, result_base$my_answer) 
 #[1] TRUE
identical(test$`Answer Expected`, result_dt$Number) 
 #[1] TRUE

If you like my publications or have your own ways to solve those puzzles in R, Python or whatever tool you choose, let me know.


Sumproduct number 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.
Exit mobile version