# Kids, Math and R

Current education at school is more and more reduced. And in the remainder, the emphasis is increasingly placed on the external effect. “Project work”, presentations, wow effects, etc. tinsel. Moreover, it is often unclear to whom these exercises are distributed - to the child or parents? The consequence of all this is the need for additional classes in various circles, within the school or alternatively, if there is a goal to give the child a full education.

In the case of mathematics or physics, you can try to catch two birds with one stone (well, either a hare and a hare). Combine the solution of problems from ext. classes (the school curriculum is not suitable for this) with the parallel education of a culture of working with data and training in algorithmic approaches. We are not talking about chat bots in three lines, but about using a computer as part of the original goal - performing calculations, conducting numerical experiments, modeling and revealing hidden dependencies, on the basis of which new hypotheses can be built.

I note right away that not everyone will be interested in the topic. To whom it is irrelevant - pass by . Who will have extra. ideas, it would be interesting to get acquainted too. Next will be a series of problems that were borrowed from the courses " Metashkola ", mathematics, 3rd grade. Naturally, at first the problem is solved by logical reasoning, then a solution method using a computer is discussed.

Important additions from the discussion below:

1. The material is written from the perspective of a parent, not a teacher. There are no services and PR. Here is just a set of practical considerations.
2. R was used as a tool because it was more convenient.

It is a continuation of previous publications .

## General preamble

``library(tidyverse) library(glue) library(magrittr) library(lubridate) library(hms) library(numbers) library(polynom) library(Ryacas) library(stringi) library(tictoc)` `

The following is a series of tasks that can be extended indefinitely.

## Restore Record

Restore the record: `3∗∗:∗3=3∗` . Find the sum of all the missing numbers.

Solution example
` `#  3ab = c3*3d df <- 0:9 %>% {tidyr::crossing(a = ., b = ., c = ., d = .)} %>% filter(300 + a * 10 + b == (c * 10 + 3) * (30 + d)) df` `

Is it possible to place signs of arithmetic operations instead of `“∗”` in the record `7∗(7∗7∗7)∗7` to get 8?

Solution example
` `ops <- c('*', '/', '+', '-') #      df <- tidyr::crossing(op1 = ops, op2 = ops, op3 = ops, op4 = ops) %>% #        mutate(data = glue::glue("7 {op1} (7 {op2} 7 {op3} 7) {op4} 7"), expr = rlang::parse_exprs(data)) %>% mutate(res = purrr::map_dbl(expr, rlang::eval_bare)) %>% arrange(res) %>% filter(res == 8) print(tbl_df(df), n = 20)` `

All numbers from 1 to 100 are written out. How many times does the number 4 appear in the record?

Solution example
` `#  100   4,      99 #    tidyr::crossing(d1 = 0:9, d2 = 0:9) %>% #   ,   4 filter(d1 == 4 | d2 == 4) %>% arrange(d1, d2)` `

The number 9, with which the three-digit number began, was moved to the end of the number. The result is a number that is 216 less. What is the sum of the digits of the original number.

Solution example
` `#   : abc df <- tidyr::crossing(b = 0:9, c = 0:9) %>% #    9     mutate(delta = (900 + 10*b + c) - (100*b + 10*c + 9)) %>% filter(between(delta, 200, 230)) df` `

The product of 5 consecutive natural numbers is 2520. What is the smaller number?

Solution Example Method 1
` `numbers::primeFactors(2520)` `

Solution Example Method 2
` `# https://joftius.wordpress.com/2015/10/19/finding-multiple-roots-of-univariate-functions-in-r/ f <- function(x) {x * (x+1) * (x+2) * (x+3) * (x+4) - 2520} rootSolve::uniroot.all(f, c(0, 2520))` `

Solution Example Method 3
` `eq <- "x * (x+1) * (x+2) * (x+3) * (x+4) - 2520" yacas(glue("Simplify({eq})")) #            rts <- base::polyroot(c(-2520, 24, 50, 35, 10, 1)) #     n-1 #     # http://www.johnmyleswhite.com/notebook/2009/12/18/using-complex-numbers-in-r/ Re(rts[abs(Im(rts)) < 1^-10])` `

What number can be substituted for `"∗"` so that the number `543∗` divisible by `4` ? What are all the options?

Solution example
` `tibble(num = 5430 + 0:9, mod = mod(num, 4)) %>% arrange(mod, num)` `

What is twice the third of a quarter of the number 60?

Solution example
` `2*(1/3*(1/4*60)) 60 %>% {./4} %>% {./3} %>% {.*2}` `

Divide the watch dial without hands into 2 parts so that the sum of the numbers available in each section would be the same.
Add. the question is: how many parts can the clock face be further divided so that in each part there are numbers whose sum would be equal to each other?

Solution example
` `#       2  ,   #    ,    =  sum(1:12) / 2 #    for (i in 1:12){ res <- cumsum(i:12) print(glue(" : {i},  : {glue_collapse(res, ', ')}")) val <- which(res == 39) if(! identical(val, integer(0))){ print(glue("  [{i}; {val + i - 1}]")) } }` `

## “Bulls and Cows”

### Variation 1

In the record number various digits from 1 to 5 inclusive.
The number of guessed numbers standing in their places is the number of bulls. The number of guessed numbers that are out of place is the number of cows.

What is a three-digit number if you know:

• 314 - 1 bull and 1 cow;
• 124 - 1 bull and 1 cow;
• 523 - 1 bull and 1 cow.

Solution example
` `library(tidyverse) library(stringi) #   "an_bn_cn". c --    df <- c(1, 2, 3, 4, 5) %>% {tidyr::crossing(an = ., bn = ., cn = .)} %>% #    mutate(comb = purrr::pmap(., c)) %>% #     mutate(val = stri_join(an, bn, cn)) %>% #      # 314 -  1  filter(stri_detect_regex(val, "3[^1][^4]|[^3]1[^4]|[^3][^1]4")) %>% # 124 -  1  filter(stri_detect_regex(val, "1[^2][^4]|[^1]2[^4]|[^1][^2]4")) %>% # 523 -  1  filter(stri_detect_regex(val, "5[^2][^3]|[^5]2[^3]|[^5][^2]3")) %>% #      filter(purrr::map_int(comb, n_distinct) == 3) %>% #      (1   1 ) filter(purrr::map(comb, ~length(base::intersect(.x, c(3, 1, 4)))) == 2) %>% filter(purrr::map(comb, ~length(base::intersect(.x, c(1, 2, 4)))) == 2) %>% filter(purrr::map(comb, ~length(base::intersect(.x, c(5, 2, 3)))) == 2) df` `

### Variation 2

In the record number various digits from 1 to 5 inclusive.
The number of guessed numbers standing in their places is the number of bulls.
The number of guessed numbers that are out of place is the number of cows.
What is a three-digit number if you know:

543 - 1 bull; 0 cows;
235 - 1 bull; 0 cows.

Solution example
` `library(tidyverse) library(stringi) df <- c(1, 2, 3, 4, 5) %>% tidyr::crossing(d3 = ., d2 = ., d1 = .) %>% #    mutate(comb = purrr::pmap(., c)) %>% #     mutate(val = stri_join(d3, d2, d1)) %>% #      # 543 -- 1 ; 0  filter(stri_detect_regex(val, "5..|.4.|..3")) %>% # 235 -- 1 ; 0  filter(stri_detect_regex(val, "2..|.3.|..5")) %>% #      filter(purrr::map_int(comb, n_distinct) == 3) %>% #      filter(purrr::map(comb, ~length(base::intersect(.x, c(5, 4, 3)))) == 1) %>% filter(purrr::map(comb, ~length(base::intersect(.x, c(2, 3, 5)))) == 1) df` `

## Fill the square

Is it possible to arrange ten minuses in the cells of a square table 4 by 4 so that each column has an odd number of minuses, and each row has an even number of minuses?

Solution example, option 1
` `#      ,    10.  : 3, 3, 3, 1 library(arrangements) library(foreach) #   : # 1 2 3 4 # 5 6 7 8 # 9 10 11 12 # 13 14 15 16 cmb <- arrangements::combinations(1:16, k = 10, replace = FALSE) pryr::object_size(cmb) #   ,   ,   .  icomb <- icombinations(1:16, k = 10, replace = FALSE) foreach(x = icomb, .combine=c) %do% { # x -    ,    "" #       #  isOdd <- function(x, set){ #   length(base::intersect(x, set)) %% 2 == 1 } isEven <- function(x, set){ #   length(base::intersect(x, set)) %% 2 == 0 } col_flag <- all( isOdd(x, c(1, 5, 9, 13)), isOdd(x, c(2, 6, 10, 14)), isOdd(x, c(3, 7, 11, 15)), isOdd(x, c(4, 8, 12, 16)) ) row_flag <- all( isEven(x, c(1, 2, 3, 4)), isEven(x, c(5, 6, 7, 8)), isEven(x, c(9, 10, 11, 12)), isEven(x, c(13, 14, 15, 16)) ) if(col_flag && row_flag) print(x) }` `

Solution example, option 2
` `#      ,    10.  : 3, 3, 3, 1 library(tidyverse) library(magrittr) library(arrangements) library(foreach) #   : # 1 2 3 4 # 5 6 7 8 # 9 10 11 12 # 13 14 15 16 #   ,   ,   .  icomb <- icombinations(1:16, k = 10, replace = FALSE) df <- foreach(x = icomb, .combine = rbind) %do% { # x -    ,    "" #       #   v <- rep(0, 16) #   ,   # browser() v[x] <- 1 m <- matrix(v, nrow = 4, ncol = 4, byrow = TRUE) #  :      , #        if (all(colSums(m) %% 2 == 1) && all(rowSums(m) %% 2 == 0)) x else NULL } df %<>% as_tibble(.name_repair = "minimal") #       v <- rep(0, 16) #   ,   v[purrr::flatten_int(df[5, ])] <- 1 matrix(v, nrow = 4, ncol = 4, byrow = TRUE)` `

## Combinatorics

How many different combinations of letters, in which two identical letters do not stand side by side, can be made by rearranging the letters K, A, W and A?

Take a look at the various considerations. Generating all distinct permutations of a list in R

Solution example
` `#     # https://stackoverflow.com/questions/44918645/split-a-string-into-character-efficiently lset <- unlist(base::strsplit("", split = "", fixed = TRUE)) #       ,      lset <- c("", "1", "", "2") # library(permutations) df <- tidyr::crossing(p1 = lset, p2 = lset, p3 = lset, p4 = lset) ff <- function(...){ vals <- rlang::list2(...) # browser() n_distinct(unlist(vals)) } df %>% # mutate(u = purrr::pmap_int(., ~n_distinct(.x))) mutate(u = purrr::pmap_int(., ff)) %>% filter(u == 4) %>% select(-u) %>% #      mutate(s = purrr::pmap_chr(., stri_join)) %>% mutate_at(vars(s), stri_replace_all_regex, pattern = "(\\d+)", replacement = "") %>% distinct(s) %>% #     filter(!stri_detect_fixed(s, ""))` `

How many Sundays can be in a year? What is the largest number possible?

Solution example
` `#   ,     366 %/% 7 #   .         366 %% 7` `

### The task of pirates

Pirates: A, B, C
Saying 1
A: B has 2 eyes
B: C has 2 eyes
C: A has 2 eyes

Saying 2
A: We all have 2 eyes
B: We all have 3 eyes
C: We all have 4 eyes

Each pirate lied as many times as his eyes. How many eyes does each pirate have?

Solution example
` `df <- tidyr::crossing(a = 0:2, b = 0:2, c = 0:2) %>% mutate(total = a + b + c) %>% mutate(a_lie = (b != 2) + (total != 2), b_lie = (c != 2) + (total != 3), c_lie = (a != 2) + (total != 4)) %>% filter(a_lie == a, b_lie == b, c_lie == c) df` `

Etc. etc.

For some, this may be an occasion to spend time with your child.

All Articles