用编程方法硬解小学奥赛题。

朋友圈看到一道有趣的题目,小学二年级的。

题目: 老师让菲菲从 1 ~ 9 这9个数字中选取 4 个不同的数字,组成一个四位数,使得这个四位数能被所有她没有选中的数整除,但不能被选中的任一个数字整除。那么,菲菲组成的四位数是_____。

for (num in seq(1234, 9876)){
    contained = strsplit(as.character(num), split = "")[[1]]  |>
     unique() |> 
     as.integer()
    if (length(contained) != 4 | 0 %in% contained) next
    if (any(num %% contained == 0)) next
    other = (1:9)[!1:9 %in% contained]
    if (any(num %% other != 0)) next
    print(num)
}
## [1] 5936

作者简介

Chun-Hui Gao is a Research Associate at Huazhong Agricultural University.

重复使用

Text and figures are licensed under Creative Commons Attribution CC BY 4.0. The source code is licensed under MIT. The full source is available at https://github.com/yihui/hugo-prose.

欢迎修订

如果您发现本文里含有任何错误(包括错别字和标点符号),欢迎在本站的 GitHub 项目里提交修订意见。

引用本文

如果您使用了本文的内容,请按照以下方式引用:

gaoch (2023). 一道数学题. BIO-SPRING. /post/2023/12/31/a-math-quiz/

BibTeX citation

@misc{
  title = "一道数学题",
  author = "gaoch",
  year = "2023",
  journal = "BIO-SPRING",
  note = "/post/2023/12/31/a-math-quiz/"
}