1〜9までの相異なる二つの数字があり、一方は5桁、他方は4桁で、差が、3333となる数をみつける引き算の覆面算からヒントを受けて、このタイプの問題を考えてみたい。
\[ \begin{array}{cccccc} \mbox{} & A & B & C & D & E\\ -)& & F & G & H & I\\ \hline & 3 & 3 & 3 & 3 & 3 \end{array} \quad\Rightarrow\quad \begin{array}{cccccc} \mbox{} & 4 & 1 & 2 & 6 & 8\\ -)& & 7 & 9 & 3 & 5\\ \hline & 3 & 3 & 3 & 3 & 3 \end{array} \quad \begin{array}{cccccc} \mbox{} & 4 & 1 & 2 & 8 & 6\\ -)& & 7 & 9 & 5 & 3\\ \hline & 3 & 3 & 3 & 3 & 3 \end{array} \]
gtool
package を使う。他にもチョイスはあるだろう。tibble で変形するため、tidyverse
package を使う。
注:この程度の問題なら、Base のみでも可能。R は、Packages が多く、どれを使うのが適切かは難しい。ネット検索の頼ることが多い。
library(gtools)
library(tidyverse)
## ─ Attaching packages ───────────────────────────── tidyverse 1.3.0 ─
## ✓ ggplot2 3.3.0 ✓ purrr 0.3.3
## ✓ tibble 3.0.0 ✓ dplyr 0.8.5
## ✓ tidyr 1.0.2 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.5.0
## ─ Conflicts ─────────────────────────────── tidyverse_conflicts() ─
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
perm <- permutations(9,9,v=1:9)
colnames(perm) <- LETTERS[1:9]
p <- as_tibble(perm)
# dim(perm)
# factorial(9)
p <- p %>% mutate(first = A * 10^4 + B * 10^3 + C * 10^2 + D * 10 + E,
second = F * 10^3 + G * 10^2 + H * 10 + I, diff = first - second)
# head(p)
p %>% group_by(diff) %>% mutate(count = n()) %>% summary()
## A B C D E F
## Min. :1 Min. :1 Min. :1 Min. :1 Min. :1 Min. :1
## 1st Qu.:3 1st Qu.:3 1st Qu.:3 1st Qu.:3 1st Qu.:3 1st Qu.:3
## Median :5 Median :5 Median :5 Median :5 Median :5 Median :5
## Mean :5 Mean :5 Mean :5 Mean :5 Mean :5 Mean :5
## 3rd Qu.:7 3rd Qu.:7 3rd Qu.:7 3rd Qu.:7 3rd Qu.:7 3rd Qu.:7
## Max. :9 Max. :9 Max. :9 Max. :9 Max. :9 Max. :9
## G H I first second
## Min. :1 Min. :1 Min. :1 Min. :12345 Min. :1234
## 1st Qu.:3 1st Qu.:3 1st Qu.:3 1st Qu.:33840 1st Qu.:3384
## Median :5 Median :5 Median :5 Median :55555 Median :5555
## Mean :5 Mean :5 Mean :5 Mean :55555 Mean :5555
## 3rd Qu.:7 3rd Qu.:7 3rd Qu.:7 3rd Qu.:77270 3rd Qu.:7726
## Max. :9 Max. :9 Max. :9 Max. :98765 Max. :9876
## diff count
## Min. : 2469 Min. : 1.000
## 1st Qu.:27212 1st Qu.: 4.000
## Median :50000 Median : 6.000
## Mean :50000 Mean : 7.051
## 3rd Qu.:72788 3rd Qu.: 8.000
## Max. :97531 Max. :54.000
u <- p %>% group_by(diff) %>% mutate(count = n()) %>% arrange(count) %>% ungroup()
head(u)
hist(u$count)
plot(x = u$diff, y = u$count)
library(gridExtra)
ph <- qplot(count,data = u)
# ph <- ggplot(data = u, aes(x = count)) + geom_histogram()
pd <- qplot(diff, count, data = u)
# pd <- ggplot(data = u, aes(x = diff, y = count)) + geom_point()
grid.arrange(ph, pd, ncol=2)
v <- u %>% group_by(diff) %>% arrange(diff) %>% select(diff, count)
check <- function(x){
v$count[x] == v$count[nrow(v)-x+1]
}
all(sapply(1:floor(nrow(v)/2), check))
## [1] TRUE
p %>% filter(diff == 33333)
p %>% filter(diff == 7777)
p %>% filter(diff == 6666)
p %>% filter(diff == 3333)
filter(u, count ==1)
filter(u, count == 2)
filter(u, count == max(count))
この問題も、児童養護施設での学修支援のときに質問を受けたことに起因しているので、問題を作成してみる。それには、難易度も、定めたい。解答が多数あるのは、問題としては、適切とは言えないので、count が、2以下のもののみ考える。
難易度は、解き心地があり、難しいが、繰り下がりの数のみで判断することにする。
w <- u %>% filter(count <= 2) %>%
mutate(lv1 = B < F, lv2 = C < G, lv3 = D < H, lv4 = E < I) %>%
mutate(lvl = lv1 + lv2 + lv3 + lv4) %>%
group_by(diff) %>% mutate(level = max(lvl)) %>% ungroup()
# head(w)
prob <- w %>% select(diff, level) %>% group_by(diff) %>% arrange(desc(level)) %>% distinct()
# head(prob)
table(prob$level)
##
## 0 1 2 3 4
## 287 3143 6886 3501 345
hist(prob$level)
2469 は、答えが一つに決まるものの中で最小、97531 は、最大のものである。 これらは、上の問題4の意味で、対称な位置にあるものである。
prob %>% filter(diff %in% c(33333, 7777, 2469, 97531))
level4 <- prob %>% filter(level == 4) %>% select(diff) %>% as.data.frame()
level_4 <- level4[[1]]
level3 <- prob %>% filter(level == 3) %>% select(diff) %>% as.data.frame()
level_3 <- level3[[1]]
level2 <- prob %>% filter(level == 2) %>% select(diff) %>% as.data.frame()
level_2 <- level2[[1]]
level1 <- prob %>% filter(level == 1) %>% select(diff) %>% as.data.frame()
level_1 <- level1[[1]]
level0 <- prob %>% filter(level == 0) %>% select(diff) %>% as.data.frame()
level_0 <- level0[[1]]
sample_problems <- cbind(sample(level_4, 5), sample(level_3, 5), sample(level_2, 5), sample(level_1, 5), sample(level_0, 5))
colnames(sample_problems) <- c("Level 4", "Level 3", "Level 2", "Level 1", "Level 0")
sample_problems
## Level 4 Level 3 Level 2 Level 1 Level 0
## [1,] 86575 70438 58022 31141 11636
## [2,] 73737 80264 51894 63498 97153
## [3,] 27844 3739 84029 95163 96341
## [4,] 65842 26039 53018 71503 45132
## [5,] 55548 30278 7662 50928 91366
\(\mbox{diff}\) に対する解と、\(1000000-\mbox{diff}\) の解の数は等しい。
count(diff) = count(100000 - diff)
まず、以下のように置く。
\(X = A * 10^4 + B * 10^3 + C * 10^2 + D * 10 + E\)
\(Y = F * 10^3 + G * 10^2 + H * 10 + I\)
\(Z = X - Y\)
\(\bar{A} = 10 - A\), \(\bar{B} = 10 - B\), \(\bar{C} = 10 - C\), \(\bar{D} = 10 - D\), \(\bar{E} = 10 - E\), \(\bar{F} = 10 - F\), \(\bar{G} = 10 - G\), \(\bar{H} = 10 - H\), \(\bar{I} = 10 - I\)
\(\bar{X} = \bar{A} * 10^4 + \bar{B} * 10^3 + \bar{C} * 10^2 + \bar{D} * 10 + \bar{E}\)
\(\bar{Y} = \bar{F} * 10^3 + \bar{G} * 10^2 + \bar{H} * 10 + \bar{I}\)
すると、 \[\begin{eqnarray*} 100000 - Z & = & (111110 - X) - (11110 - Y)\\ & = & (10 - A) * 10^4 + (10 - B) * 10^3 + (10 - C) * 10^2 + (10 - D) * 10 + (10 - E)\\ & & - ((10 - F) * 10*3 + (10 - G) * 10^2 + (10 - H) * 10 + (10 - I))\\ & = & \bar{A} * 10^4 + \bar{B} * 10^3 + \bar{C} * 10^2 + \bar{D} * 10 + \bar{E}\\ & & - (\bar{F} * 10*3 + \bar{G} * 10^2 + \bar{H} * 10 + \bar{I})\\ & = & \bar{X} - \bar{Y} \end{eqnarray*}\]
したがって、\(\bar{X}\), \(\bar{Y}\) が、\(\bar{Z} = 100000 - Z = \bar{Y} - \bar{Z}\) を満たす。
\[\{1, 2, 3, 4, 5, 6, 7, 8, 9\} = \{A, B, C, D, E, F, G, H, I\} = \{\bar{A}, \bar{B}, \bar{C}, \bar{D}, \bar{E}, \bar{F}, \bar{G}, \bar{H}, \bar{I}\}\]
従って、
\[ \begin{array}{cccccc} \mbox{} & A & B & C & D & E\\ -)& & F & G & H & I\\ \hline &&&& Z & \end{array} \quad\Leftrightarrow\quad \begin{array}{cccccc} \mbox{} & \bar{A} & \bar{B} & \bar{C} & \bar{D} & \bar{E}\\ -)& & \bar{F} & \bar{G} & \bar{H} & \bar{I}\\ \hline & & 10^5 & - & Z & \end{array} \]
解の数は、等しい。